英文:
how to use prettier-plugin-svelte with prettier-plugin-tailwindcss
问题
我正在尝试使用 prettier 来格式化 Svelte 文件,以便自动按照 tailwind 推荐的顺序对 tailwind 实用程序类进行排序。我遵循了这份文档:
但是,当我尝试格式化任何 Svelte 文件时,我收到了一个错误消息:“Extension 'Prettier - Code Formatter' 被配置为格式化器,但无法格式化 'Svelte' 文件”。
当我尝试配置它时,唯一可用的选项是:
这是我执行的步骤:
pnpn create svelte@latest svelte-prettier-tailwindcss
◇ Which Svelte app template?
│ Skeleton project
│
◇ Add type checking with TypeScript?
│ Yes, using TypeScript syntax
│
◇ Select additional options (use arrow keys/space bar)
│ Add Prettier for code formatting
pnpn exec svelte-add@latest tailwindcss
pnpm install -D prettier prettier-plugin-tailwindcss
这是我的 .prettierrc
文件
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte", "prettier-plugin-tailwindcss"],
"pluginSearchDirs": false,
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
以及我的 ./vscode/settings.json
文件
{
"editor.formatOnSave": true,
"[svelte]": {
// "editor.defaultFormatter": "svelte.svelte-vscode" // this works, but it doesn't use tailwindcss plugin!
"editor.defaultFormatter": "esbenp.prettier-vscode" // this doesn't work
}
}
然后我创建了这个 +page.svelte
页面
<h1 class="text-sm bg-fuchsia-200">Tailwind will reformat this (text-sm goes last)</h1>
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
如果我运行 pnpm run format
,prettier tailwindcss 会使用 tailwind css 插件,但如果我按 ctrl-shift-I(或 ctrl-shift-P 格式化文档),我会收到关于 prettier 代码无法格式化 svelte 文件的错误。有什么想法吗?
英文:
I'm trying to use prettier to format svelte files with tailwind classes, so that tailwind utilities classes get automatically sorted following tailwind's recommended order
I followed this docs:
but when I try to format any svelte file I get a "Extension 'Prettier - Code Formatter' is configured as formatter but it cannot format 'Svelte' files"
And when I try to configure it the only available option is:
these are the steps I took:
pnpn create svelte@latest svelte-prettier-tailwindcss
◇ Which Svelte app template?
│ Skeleton project
│
◇ Add type checking with TypeScript?
│ Yes, using TypeScript syntax
│
◇ Select additional options (use arrow keys/space bar)
│ Add Prettier for code formatting
pnpn exec svelte-add@latest tailwindcss
pnpm install -D prettier prettier-plugin-tailwindcss
this is my .prettierrc
file
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte", "prettier-plugin-tailwindcss"],
"pluginSearchDirs": false,
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
and this is my ./vscode/settings.json
file
{
"editor.formatOnSave": true,
"[svelte]": {
// "editor.defaultFormatter": "svelte.svelte-vscode" // this works, but it doesn't use tailwindcss plugin!
"editor.defaultFormatter": "esbenp.prettier-vscode" // this doesn't work
}
}
Then I create this +page.svelte
page
<h1 class="text-sm bg-fuchsia-200">Tailwind will reformat this (text-sm goes last)</h1>
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
If I run pnpm run format
the prettier tailwindcss uses the tailwind css pluing
but if I press ctrl-shift-I (or ctrl-shift-P format document) I get the error about prettier code not being able to format svelte files.
Any idea?
答案1
得分: 2
I had to add "prettier.documentSelectors": ["**/*.svelte"]
to my vscode settings file.
The reason for this option is explained here.
Since prettier-plugin-svelte
is overriding svelte files from the .prettierrc
file (with { files: ['*.svelte'], options: { parser: 'svelte' } }
) we also need to let the prettier vscode extension know that it can handle svelte files.
This is how my settings.json
ended up:
{
"prettier.documentSelectors": ["**/*.svelte"],
"editor.defaultFormatter": "esbenp.prettier-vscode",
"svelte.enable-ts-plugin": true
}
And this is the .prettierrc
file:
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte", "prettier-plugin-tailwindcss"],
"pluginSearchDirs": false,
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
英文:
I had to add "prettier.documentSelectors": ["**/*.svelte"]
to my vscode settings file
The reason for this option is explained here
Since prettier-plugin-svelte
is overriding svelte files from the .prietterrc
file (with { files: ['*.svelte'], options: { parser: 'svelte' } }
) we also need to let the prettier vscode extension know that it can handle svelte files.
this is how my settings.json
ended up:
{
"prettier.documentSelectors": ["**/*.svelte"],
"editor.defaultFormatter": "esbenp.prettier-vscode",
"svelte.enable-ts-plugin": true
}
and this is the .prettierrc
file
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte", "prettier-plugin-tailwindcss"],
"pluginSearchDirs": false,
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论