# Prettier for Svelte components Format your Svelte components using Prettier. ## Features - 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 ## How to use in your IDE This plugin comes bundled with the [Svelte for VS Code](https://github.com/sveltejs/language-tools). If you only format through the editor, you therefore don't need to do anything in addition. If you want to - customize some formatting behavior - use the official VS Code Prettier extension to format Svelte files instead - use a different editor - also want to use the command line to format then you need to install the plugin and setup a Prettier configuration file as described in the next section. Some of the extensions let you define options through extension-specific configuration. These settings are ignored however if there's any configuration file (`.prettierrc` for example) present. ## How to install manually First install Prettier and the plugin as a dev depenendency: ```bash npm i --save-dev prettier-plugin-svelte prettier ``` Then create a `.prettierrc` file to tell Prettier about the plugin: ```json { "plugins": ["prettier-plugin-svelte"] } ``` If you're using `prettier-plugin-svelte` version 2 with `pnpm` and have problems getting it to work, you may need to use a `.prettierrc.cjs` file instead to point Prettier to the exact location of the plugin using `require.resolve`: ```js module.exports = { pluginSearchDirs: false, // you can omit this when using Prettier version 3 plugins: [require('prettier-plugin-svelte')], overrides: [{ files: '*.svelte', options: { parser: 'svelte' } }], // Other prettier options here }; ``` > Do NOT use the above with version 3 of the plugin If you want to customize some formatting behavior, see section "Options" below. ## How to use (CLI) Format your code using Prettier CLI. As a one-time run: ``` npx prettier --write --plugin prettier-plugin-svelte . ``` As part of your scripts in `package.json`: ``` "format": "prettier --write --plugin prettier-plugin-svelte ." ``` > There's currently [an issue with Prettier 3](https://github.com/prettier/prettier/issues/15079) which requires the seemingly redundant `--plugin` setting If you want to customize some formatting behavior, see section "Options" below. ## Options `Configurations are optional` Make a `.prettierrc` file in your project directory and add your preferred [options](https://prettier.io/docs/en/options.html) to [configure Prettier](https://prettier.io/docs/en/configuration.html). When using Prettier through the CLI, you can also pass options through CLI flags, but a `.prettierrc` file is recommended. ### Svelte Sort Order 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`). ### Svelte Strict Mode 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. Example: ```html
``` | Default | CLI Override | API Override | | ------- | ----------------------------- | -------------------------- | | `false` | `--svelte-strict-mode ` | `svelteStrictMode: ` | ### Svelte Allow Shorthand 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: ` | ### Svelte Self Closing Elements Whether or not empty elements (such as `div`s) should be self-closed or not. Example: ```html
``` | Default | CLI Override | API Override | | | --------- | ------------------------------------ | ---------------------------------- | -------- | | `"never"` | `--svelte-self-close-elements ` | `svelteSelfCloseElements: "always" | "never"` | ### Svelte Self Closing Components Whether or not empty components should be self-closed or not. Example: ```html ``` | Default | CLI Override | API Override | | | ---------- | -------------------------------------- | ------------------------------------ | -------- | | `"always"` | `--svelte-self-close-components ` | `svelteSelfCloseComponents: "always" | "never"` | ### Svelte Bracket New Line > Deprecated since 2.5.0. Use Prettier 2.4.0 and [bracketSameLine](https://prettier.io/docs/en/options.html#bracket-line) instead. Put the `>` of a multiline element on a new line. Roughly the Svelte equivalent of the [jsxBracketSameLine](https://prettier.io/docs/en/options.html#jsx-brackets) 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](https://prettier.io/blog/2018/11/07/1.15.0.html#whitespace-sensitive-formatting). You can adjust whitespace sensitivity through [this setting](https://prettier.io/docs/en/options.html#html-whitespace-sensitivity). Example: ```html
foo
bar
content
foo
bar
content
foo
bar
content
``` | Default | CLI Override | API Override | | ------- | ---------------------------------- | ------------------------------ | | `true` | `--svelte-bracket-new-line ` | `svelteBracketNewLine: ` | ### Svelte Indent Script And Style Whether or not to indent the code inside `