Svelte plugin for prettier
npm install prettier-plugin-svelteFormat your Svelte components using Prettier.
- Format your HTML, CSS, and JavaScript using prettier
- Format Svelte syntax, e.g. each loops, if statements, await blocks, etc.
- Format the JavaScript expressions embedded in the Svelte syntax
- e.g. expressions inside of {}, event bindings on:click="", and more
This plugin is bundled in the Svelte for VS Code extension. If you only format through the editor, you therefore don't need to do anything in addition.
The extension lets you define options through extension-specific configuration. These settings are ignored however if there's any configuration file (.prettierrc for example) present.
Installing the plugin as a package allows:
- customizing the formatting behavior
- using the command line to format
- using a different IDE
- using the official VS Code Prettier extension to format Svelte files
- prettier-plugin-svelte@3 only works with prettier@3
- prettier-plugin-svelte@2 only works with prettier@2
Install Prettier and the plugin as a dev dependency:
``bash`
npm i --save-dev prettier-plugin-svelte prettier
Then create a .prettierrc configuration file:
`jsonc`
// .prettierrc
{
// ..
"plugins": ["prettier-plugin-svelte"],
"pluginSearchDirs": ["."], // should be removed in v3
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
If you want to customize some formatting behavior, see section Options.
Format your code using the Prettier CLI.
`bash`
npx prettier --write . # v3
npx prettier --write --plugin prettier-plugin-svelte . # v2
As part of your scripts in package.json:
`jsonc`
// package.json
{
// ..
"scripts": {
"format": "prettier --write .", // v3
"format": "prettier --write --plugin prettier-plugin-svelte ." // v2
}
}
If you want to customize some formatting behavior, see section Options.
Configurations are optional
Make a .prettierrc file in your project directory and add your preferred options to configure Prettier. When using Prettier through the CLI, you can also pass options through CLI flags, but a .prettierrc file is recommended.
Sort order for svelte:options, scripts, markup, and styles.
Format: join the keywords options, scripts, markup, styles with a - in the order you want; or none if you don't want Prettier to reorder anything.
| Default | CLI Override | API Override |
| ------------------------------- | ------------------------------ | --------------------------- |
| options-scripts-markup-styles | --svelte-sort-order | svelteSortOrder: |
> The options order option only exists since version 2. If you use version 1 of prettier-plugin-svelte, omit that option (so for example only write scripts-markup-styles).
More strict HTML syntax: Quotes in attributes and no self-closing DOM elements (except void elements).
> In version 2 this overruled svelteAllowShorthand, which is no longer the case.
> In Svelte 5, attributes are never quoted, because this will mean "stringify this attribute value" in a future Svelte version
Example:
`html
| Default | CLI Override | API Override |
| ------- | ----------------------------- | -------------------------- |
|
false | --svelte-strict-mode | svelteStrictMode: |$3
Option to enable/disable component attribute shorthand if attribute name and expression are same.
Example:
`html
`| Default | CLI Override | API Override |
| ------- | --------------------------------- | ------------------------------ |
|
true | --svelte-allow-shorthand | svelteAllowShorthand: |$3
> Deprecated since 2.5.0. Use Prettier 2.4.0 and bracketSameLine instead.
Put the
> of a multiline element on a new line. Roughly the Svelte equivalent of the jsxBracketSameLine rule. Setting this to false will have no effect for whitespace-sensitive tags (inline elements) when there's no whitespace between the > of the start tag and the inner content, or when there's no whitespace after the > of the end tag. You can read more about HTML whitespace sensitivity here. You can adjust whitespace sensitivity through this setting.Example:
`html
foobar
content
>
foo
bar>
pretend
break
>
content
>
| Default | CLI Override | API Override |
| ------- | ---------------------------------- | ------------------------------ |
|
true | --svelte-bracket-new-line | svelteBracketNewLine: |$3
Whether or not to indent the code inside