Skip to content

Commit 8d77785

Browse files
authored
docs: update readme (#386)
closes #385 closes #368
1 parent b07b7cf commit 8d77785

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

README.md

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,71 @@ Format your Svelte components using Prettier.
99
- Format the JavaScript expressions embedded in the Svelte syntax
1010
- e.g. expressions inside of `{}`, event bindings `on:click=""`, and more
1111

12-
## How to use in VS Code and Atom
12+
## How to use in your IDE
1313

14-
This plugin comes with [Svelte for VS Code](https://github.com/sveltejs/language-tools) and [Svelte for Atom](https://github.com/UnwrittenFun/svelte-atom) so just install the extension for your favorite editor and enjoy.
14+
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.
1515

16-
If you want to customize some formatting behavior, see section "Options" below.
16+
If you want to
17+
18+
- customize some formatting behavior
19+
- use the official VS Code Prettier extension to format Svelte files instead
20+
- use a different editor
21+
- also want to use the command line to format
22+
23+
then you need to install the plugin and setup a Prettier configuration file as described in the next section.
1724

1825
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.
1926

2027
## How to install manually
2128

29+
First install Prettier and the plugin as a dev depenendency:
30+
2231
```bash
2332
npm i --save-dev prettier-plugin-svelte prettier
2433
```
2534

26-
## How to use (CLI)
35+
Then create a `.prettierrc` file to tell Prettier about the plugin:
36+
37+
```json
38+
{
39+
"plugins": ["prettier-plugin-svelte"]
40+
}
41+
```
42+
43+
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`:
44+
45+
```js
46+
module.exports = {
47+
pluginSearchDirs: false, // you can omit this when using Prettier version 3
48+
plugins: [require('prettier-plugin-svelte')],
49+
overrides: [{ files: '*.svelte', options: { parser: 'svelte' } }],
50+
51+
// Other prettier options here
52+
};
53+
```
54+
55+
> Do NOT use the above with version 3 of the plugin
2756
28-
Install `prettier` and `prettier-plugin-svelte` as dev dependencies in your project.
57+
If you want to customize some formatting behavior, see section "Options" below.
2958

30-
Then format your code using Prettier CLI. You may need to add `--plugin-search-dir=.`
59+
## How to use (CLI)
60+
61+
Format your code using Prettier CLI.
3162

3263
As a one-time run:
3364

3465
```
35-
npx prettier --write --plugin-search-dir=. ./**/*.html
66+
npx prettier --write --plugin prettier-plugin-svelte .
3667
```
3768

3869
As part of your scripts in `package.json`:
3970

4071
```
41-
"format": "prettier --write --plugin-search-dir=. ./**/*.html"
72+
"format": "prettier --write --plugin prettier-plugin-svelte ."
4273
```
4374

75+
> There's currently [an issue with Prettier 3](https://github.com/prettier/prettier/issues/15079) which requires the seemingly redundant `--plugin` setting
76+
4477
If you want to customize some formatting behavior, see section "Options" below.
4578

4679
## Options
@@ -212,7 +245,7 @@ There is a [Tailwind Prettier Plugin](https://github.com/tailwindlabs/prettier-p
212245
'prettier-plugin-svelte',
213246
'prettier-plugin-tailwindcss', // MUST come last
214247
],
215-
pluginSearchDirs: false,
248+
pluginSearchDirs: false, // you can omit this when using Prettier version 3
216249
}
217250
```
218251

@@ -250,3 +283,21 @@ becomes this
250283
```
251284

252285
it's because of whitespsace sensitivity. For inline elements (`span`, `a`, etc) it makes a difference when rendered if there's a space (or newline) between them. Since we don't know if your slot inside your Svelte component is surrounded by inline elements, Svelte components are treated as such, too. You can adjust this whitespace sensitivity through [this setting](https://prettier.io/docs/en/options.html#html-whitespace-sensitivity). You can read more about HTML whitespace sensitivity [here](https://prettier.io/blog/2018/11/07/1.15.0.html#whitespace-sensitive-formatting).
286+
287+
### Which versions are compatibly with which Prettier version?
288+
289+
`prettier-plugin-svelte` v2 is compatible with Prettier v2 and incompatible with Prettier v3.
290+
`prettier-plugin-svelte` v3 is compatible with Prettier v3 and incompatible with lower Prettier versions.
291+
292+
### How to migrate from version 2 to 3?
293+
294+
Version 3 contains the following breaking changes:
295+
296+
- Whether or not empty elements/components should self-close is now left to the user - in other words, if you write `<div />` or `<Component />` that stays as is, and so does `<div></div>`/`<Component></Component>`. If `svelteStrictMode` is turned on, it will still only allow `<div></div>` notation for elements (but it will leave your components alone)
297+
- `svelteAllowShorthand` now takes precedence over `svelteStrictMode`, which no longer has any effect on that behavior. Set `svelteAllowShorthand` to `false` to get back the v2 behavior
298+
- Some deprecated `svelteSortOrder` options were removed, see the the options section above for which values are valid for that options
299+
300+
Version 3 of this plugin requires Prettier version 3, it won't work with lower versions. Prettier version 3 contains some changes to how it loads plugins which may require you to adjust your configuration file:
301+
302+
- Prettier no longer searches for plugins in the directory automatically, you need to tell Prettier specifically which plugins to use. This means you need to add `"plugins": ["prettier-plugin-svelte"]` to your config if you haven't already. Also remove the deprecated option `pluginSearchDirs`. When invoking Prettier from the command line, you currently need to pass `--plugin prettier-plugin-svelte` in order to format Svelte files [due to a bug in Prettier](https://github.com/prettier/prettier/issues/15079)
303+
- Prettier loads plugins from the plugin array differently. If you have used `require.resolve("prettier-plugin-svelte")` in your `.prettierrc.cjs` to tell Prettier where to find the plugin, you may need to remove that and just write `"prettier-plugin-svelte"` instead

0 commit comments

Comments
 (0)