Skip to content

Commit f284d1b

Browse files
authored
docs: Rewrite README and migration guide (#409)
1 parent dd66514 commit f284d1b

File tree

2 files changed

+92
-68
lines changed

2 files changed

+92
-68
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
## 3.0.0
2121

22-
- (breaking) requires `prettier` version 3. This may require adjustments to your configuration file, see [the migration guide for more info](https://github.com/sveltejs/prettier-plugin-svelte#how-to-migrate-from-version-2-to-3)
22+
- (breaking) requires `prettier` version 3. This may require adjustments to your configuration file, see [the migration guide for more info](https://github.com/sveltejs/prettier-plugin-svelte#migration)
2323
- (breaking) requires node version 14 or higher
2424
- (breaking) 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)
2525
- (breaking) `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

README.md

Lines changed: 91 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +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 your IDE
12+
## VS Code Extension
1313

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.
14+
This plugin is bundled in the [Svelte for VS Code](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode) extension. If you only format through the editor, you therefore don't need to do anything in addition.
1515

16-
If you want to
16+
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.
1717

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
18+
## Prettier Plugin
2219

23-
then you need to install the plugin and setup a Prettier configuration file as described in the next section.
20+
Installing the plugin as a package allows:
2421

25-
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.
22+
- customizing the formatting behavior
23+
- using the command line to format
24+
- using a different IDE
25+
- using the official VS Code [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) extension to format Svelte files
2626

27-
## How to install manually
27+
### Compatibility
2828

29-
First install Prettier and the plugin as a dev dependency:
29+
- `prettier-plugin-svelte@3` only works with `prettier@3`
30+
- `prettier-plugin-svelte@2` only works with `prettier@2`
31+
32+
### Setup
33+
34+
Install Prettier and the plugin as a dev dependency:
3035

3136
```bash
3237
npm i --save-dev prettier-plugin-svelte prettier
3338
```
3439

35-
Then create a `.prettierrc` file to tell Prettier about the plugin:
40+
Then create a `.prettierrc` [configuration file](https://prettier.io/docs/en/configuration.html):
3641

37-
```json
42+
```jsonc
43+
// .prettierrc
3844
{
39-
"plugins": ["prettier-plugin-svelte"]
45+
// ..
46+
"plugins": ["prettier-plugin-svelte"],
47+
"pluginSearchDirs": ["."], // should be removed in v3
48+
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
4049
}
4150
```
4251

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
52+
If you want to customize some formatting behavior, see section [Options](#options).
5653

57-
If you want to customize some formatting behavior, see section "Options" below.
54+
### CLI Usage
5855

59-
## How to use (CLI)
56+
Format your code using the Prettier CLI.
6057

61-
Format your code using Prettier CLI.
62-
63-
As a one-time run:
64-
65-
```
66-
npx prettier --write --plugin prettier-plugin-svelte .
58+
```bash
59+
npx prettier --write . # v3
60+
npx prettier --write --plugin prettier-plugin-svelte . # v2
6761
```
6862

6963
As part of your scripts in `package.json`:
7064

65+
```jsonc
66+
// package.json
67+
{
68+
// ..
69+
"scripts": {
70+
"format": "prettier --write .", // v3
71+
"format": "prettier --write --plugin prettier-plugin-svelte ." // v2
72+
}
73+
}
7174
```
72-
"format": "prettier --write --plugin prettier-plugin-svelte ."
73-
```
74-
75-
> There's currently [an issue with Prettier 3](https://github.com/prettier/prettier/issues/15079) which requires the seemingly redundant `--plugin` setting
7675

77-
If you want to customize some formatting behavior, see section "Options" below.
76+
If you want to customize some formatting behavior, see section [Options](#options).
7877

7978
## Options
8079

@@ -197,32 +196,63 @@ Whether or not to indent the code inside `<script>` and `<style>` tags in Svelte
197196

198197
## Usage with Tailwind Prettier Plugin
199198

200-
There is a [Tailwind Prettier Plugin](https://github.com/tailwindlabs/prettier-plugin-tailwindcss) to format classes in a certain way. This plugin must be loaded last, so if you want to use the Tailwind plugin, disable Prettier auto-loading and place `prettier-plugin-tailwindcss` in the end of the plugins array. If you are using VS Code, make sure to have the Prettier extension installed and switch the default formatter for Svelte files to it.
199+
- VS Code Extension: Use it as the default formatter for Svelte files
200+
- Prettier Plugin: Load the Tailwind plugin in the end - [Tailwind docs](https://github.com/tailwindlabs/prettier-plugin-tailwindcss#compatibility-with-other-prettier-plugins)
201201

202-
```json5
202+
```jsonc
203203
// .prettierrc
204204
{
205205
// ..
206-
plugins: [
207-
'prettier-plugin-svelte',
208-
'prettier-plugin-tailwindcss', // MUST come last
209-
],
210-
pluginSearchDirs: false, // you can omit this when using Prettier version 3
206+
"plugins": [
207+
"prettier-plugin-svelte",
208+
"prettier-plugin-tailwindcss" // MUST come last
209+
]
211210
}
212211
```
213212

214-
More info: https://github.com/tailwindlabs/prettier-plugin-tailwindcss#compatibility-with-other-prettier-plugins
215-
216213
Since we are using configuration overrides to handle svelte files, you might also have to configure the [prettier.documentselectors](https://github.com/prettier/prettier-vscode#prettierdocumentselectors) in your VS Code `settings.json`, to tell Prettier extension to handle svelte files, like this:
217214

218-
```json5
215+
```jsonc
219216
// settings.json
220217
{
221218
// ..
222-
'prettier.documentSelectors': ['**/*.svelte'],
219+
"prettier.documentSelectors": ["**/*.svelte"]
223220
}
224221
```
225222

223+
## Migration
224+
225+
```diff
226+
# package.json
227+
- "format": "prettier --plugin-search-dir . --write ."
228+
+ "format": "prettier --write ."
229+
```
230+
231+
```diff
232+
# package.json
233+
- "prettier": "^2.8.8",
234+
+ "prettier": "^3.1.0",
235+
- "prettier-plugin-svelte": "^2.10.1",
236+
+ "prettier-plugin-svelte": "^3.1.0",
237+
```
238+
239+
```diff
240+
# .prettierrc
241+
- "pluginSearchDirs": ["."],
242+
+ "plugins": ["prettier-plugin-svelte"]
243+
```
244+
245+
Version 3 contains the following breaking changes:
246+
247+
- 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)
248+
- `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
249+
- Some deprecated `svelteSortOrder` options were removed, see the the options section above for which values are valid for that options
250+
251+
Version 3 of this plugin only works with Prettier version 3. Prettier version 3 contains some changes to how it loads plugins which may require you to adjust your configuration file:
252+
253+
- 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`.
254+
- 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
255+
226256
## FAQ
227257

228258
### Why is the closing or opening tag (`>` or `<`) hugging the inner tag or text?
@@ -244,22 +274,16 @@ becomes this
244274
>
245275
```
246276

247-
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).
248-
249-
### Which versions are compatibly with which Prettier version?
277+
it's because of whitespace 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).
250278

251-
`prettier-plugin-svelte` v2 is compatible with Prettier v2 and incompatible with Prettier v3.
252-
`prettier-plugin-svelte` v3 is compatible with Prettier v3 and incompatible with lower Prettier versions.
279+
### Version 2 does not work in `pnpm`
253280

254-
### How to migrate from version 2 to 3?
281+
You may need to use a `.prettierrc.cjs` file instead to point Prettier to the exact location of the plugin using `require.resolve`:
255282

256-
Version 3 contains the following breaking changes:
257-
258-
- 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)
259-
- `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
260-
- Some deprecated `svelteSortOrder` options were removed, see the the options section above for which values are valid for that options
261-
262-
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:
263-
264-
- 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)
265-
- 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
283+
```js
284+
module.exports = {
285+
pluginSearchDirs: false,
286+
plugins: [require('prettier-plugin-svelte')],
287+
overrides: [{ files: '*.svelte', options: { parser: 'svelte' } }],
288+
};
289+
```

0 commit comments

Comments
 (0)