如何在 prettier-plugin-tailwindcss 中使用 prettier-plugin-svelte。

huangapple go评论75阅读模式
英文:

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"

如何在 prettier-plugin-tailwindcss 中使用 prettier-plugin-svelte。

And when I try to configure it the only available option is:

如何在 prettier-plugin-tailwindcss 中使用 prettier-plugin-svelte。

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

{
	&quot;useTabs&quot;: true,
	&quot;singleQuote&quot;: true,
	&quot;trailingComma&quot;: &quot;none&quot;,
	&quot;printWidth&quot;: 100,
	&quot;plugins&quot;: [&quot;prettier-plugin-svelte&quot;, &quot;prettier-plugin-tailwindcss&quot;],
	&quot;pluginSearchDirs&quot;: false,
	&quot;overrides&quot;: [{ &quot;files&quot;: &quot;*.svelte&quot;, &quot;options&quot;: { &quot;parser&quot;: &quot;svelte&quot; } }]
}

and this is my ./vscode/settings.json file

{
	&quot;editor.formatOnSave&quot;: true,
	&quot;[svelte]&quot;: {
		// &quot;editor.defaultFormatter&quot;: &quot;svelte.svelte-vscode&quot; // this works, but it doesn&#39;t use tailwindcss plugin!
		&quot;editor.defaultFormatter&quot;: &quot;esbenp.prettier-vscode&quot; // this doesn&#39;t work
	}
}

Then I create this +page.svelte page

&lt;h1 class=&quot;text-sm bg-fuchsia-200&quot;&gt;Tailwind will reformat this (text-sm goes last)&lt;/h1&gt;

&lt;p&gt;Visit &lt;a href=&quot;https://kit.svelte.dev&quot;&gt;kit.svelte.dev&lt;/a&gt; to read the documentation&lt;/p&gt;

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 &quot;prettier.documentSelectors&quot;: [&quot;**/*.svelte&quot;] 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: [&#39;*.svelte&#39;], options: { parser: &#39;svelte&#39; } }) we also need to let the prettier vscode extension know that it can handle svelte files.

This is how my settings.json ended up:

{
	&quot;prettier.documentSelectors&quot;: [&quot;**/*.svelte&quot;],
	&quot;editor.defaultFormatter&quot;: &quot;esbenp.prettier-vscode&quot;,
	&quot;svelte.enable-ts-plugin&quot;: true
}

And this is the .prettierrc file:

{
	&quot;useTabs&quot;: true,
	&quot;singleQuote&quot;: true,
	&quot;trailingComma&quot;: &quot;none&quot;,
	&quot;printWidth&quot;: 100,
	&quot;plugins&quot;: [&quot;prettier-plugin-svelte&quot;, &quot;prettier-plugin-tailwindcss&quot;],
	&quot;pluginSearchDirs&quot;: false,
	&quot;overrides&quot;: [{ &quot;files&quot;: &quot;*.svelte&quot;, &quot;options&quot;: { &quot;parser&quot;: &quot;svelte&quot; } }]
}
英文:

I had to add &quot;prettier.documentSelectors&quot;: [&quot;**/*.svelte&quot;] 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: [&#39;*.svelte&#39;], options: { parser: &#39;svelte&#39; } }) we also need to let the prettier vscode extension know that it can handle svelte files.

this is how my settings.json ended up:

{
	&quot;prettier.documentSelectors&quot;: [&quot;**/*.svelte&quot;],
	&quot;editor.defaultFormatter&quot;: &quot;esbenp.prettier-vscode&quot;,
	&quot;svelte.enable-ts-plugin&quot;: true
}

and this is the .prettierrc file

{
	&quot;useTabs&quot;: true,
	&quot;singleQuote&quot;: true,
	&quot;trailingComma&quot;: &quot;none&quot;,
	&quot;printWidth&quot;: 100,
	&quot;plugins&quot;: [&quot;prettier-plugin-svelte&quot;, &quot;prettier-plugin-tailwindcss&quot;],
	&quot;pluginSearchDirs&quot;: false,
	&quot;overrides&quot;: [{ &quot;files&quot;: &quot;*.svelte&quot;, &quot;options&quot;: { &quot;parser&quot;: &quot;svelte&quot; } }]
}

huangapple
  • 本文由 发表于 2023年6月13日 06:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定