• Stars
    star
    726
  • Rank 62,418 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Format your svelte components using prettier.

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. 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:

npm i --save-dev prettier-plugin-svelte prettier

Then create a .prettierrc file to tell Prettier about the plugin:

{
    "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:

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 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 to configure Prettier. 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 <string> svelteSortOrder: <string>

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:

<!-- svelteStrictMode: true -->
<div foo="{bar}"></div>

<!-- svelteStrictMode: false -->
<div foo={bar} />
Default CLI Override API Override
false --svelte-strict-mode <bool> svelteStrictMode: <bool>

Svelte Allow Shorthand

Option to enable/disable component attribute shorthand if attribute name and expression are same.

Example:

<!-- allowShorthand: true -->
<input type="text" {value} />

<!-- allowShorthand: false -->
<input type="text" value={value} />
Default CLI Override API Override
true --svelte-allow-shorthand <bool> svelteAllowShorthand: <bool>

Svelte Bracket New Line

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:

<!-- before formatting -->
<span><div>foo</div><span>bar</span></span>
<div pretend break>content</div>

<!-- after formatting, svelteBracketNewLine true -->
<span
    ><div>foo</div>
    <span>bar</span></span
>
<div
     pretend
     break
>
    content
</div>

<!-- after formatting, svelteBracketNewLine false -->
<span
    ><div>foo</div>
    <span>bar</span></span>
<div
     pretend
     break>
    content
</div>
Default CLI Override API Override
true --svelte-bracket-new-line <bool> svelteBracketNewLine: <bool>

Svelte Indent Script And Style

Whether or not to indent the code inside <script> and <style> tags in Svelte files. This saves an indentation level, but might break code folding in your editor.

Default CLI Override API Override
true --svelte-indent-script-and-style <bool> svelteIndentScriptAndStyle: <bool>

.prettierrc example

{
    "svelteSortOrder": "options-styles-scripts-markup",
    "svelteStrictMode": true,
    "svelteBracketNewLine": false,
    "svelteAllowShorthand": false,
    "svelteIndentScriptAndStyle": false
}

Usage with Tailwind Prettier Plugin

There is a Tailwind Prettier Plugin 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.

// .prettierrc
{
    // ..
    plugins: [
        'prettier-plugin-svelte',
        'prettier-plugin-tailwindcss', // MUST come last
    ],
    pluginSearchDirs: false, // you can omit this when using Prettier version 3
}

More info: https://github.com/tailwindlabs/prettier-plugin-tailwindcss#compatibility-with-other-prettier-plugins

Since we are using configuration overrides to handle svelte files, you might also have to configure the prettier.documentselectors in your VS Code settings.json, to tell Prettier extension to handle svelte files, like this:

// settings.json
{
    // ..
    'prettier.documentSelectors': ['**/*.svelte'],
}

FAQ

Why is the closing or opening tag (> or <) hugging the inner tag or text?

If you are wondering why this code

<span><span>assume very long text</span></span>

becomes this

<span
      ><span>assume very long text</span
    ></span
>

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. You can read more about HTML whitespace sensitivity here.

Which versions are compatibly with which Prettier version?

prettier-plugin-svelte v2 is compatible with Prettier v2 and incompatible with Prettier v3. prettier-plugin-svelte v3 is compatible with Prettier v3 and incompatible with lower Prettier versions.

How to migrate from version 2 to 3?

Version 3 contains the following breaking changes:

  • 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)
  • 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
  • Some deprecated svelteSortOrder options were removed, see the the options section above for which values are valid for that options

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:

  • 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
  • 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

More Repositories

1

svelte

Cybernetically enhanced web apps
JavaScript
77,898
star
2

kit

web development, streamlined
JavaScript
18,346
star
3

sapper

The next small thing in web development, powered by Svelte
TypeScript
7,007
star
4

realworld

SvelteKit implementation of the RealWorld app
Svelte
2,212
star
5

template

Template for building basic applications with Svelte
JavaScript
1,743
star
6

svelte-preprocess

A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
TypeScript
1,743
star
7

svelte-devtools

A browser extension to inspect Svelte application by extending your browser devtools capabilities
Svelte
1,457
star
8

language-tools

The Svelte Language Server, and official extensions which use it
TypeScript
1,199
star
9

vite-plugin-svelte

Svelte plugin for http://vitejs.dev/
JavaScript
838
star
10

sapper-template

Starter template for Sapper apps
JavaScript
703
star
11

svelte-virtual-list

A virtual list component for Svelte apps
JavaScript
673
star
12

integrations

Ways to incorporate Svelte into your stack
632
star
13

gl

A (very experimental) project to bring WebGL to Svelte
JavaScript
603
star
14

svelte-loader

Webpack loader for svelte components.
JavaScript
596
star
15

community-legacy

Svelte community meetups, packages, resources, recipes, showcase websites, and more
Svelte
563
star
16

component-template

A base for building shareable Svelte components
JavaScript
554
star
17

learn.svelte.dev

A soup-to-nuts interactive tutorial on how to build apps with Svelte
Svelte
522
star
18

rollup-plugin-svelte

Compile Svelte components with Rollup
JavaScript
500
star
19

eslint-plugin-svelte3

An ESLint plugin for Svelte v3 components.
JavaScript
372
star
20

svelte-scroller

A <Scroller> component for Svelte apps
Svelte
345
star
21

template-webpack

Template for building basic Svelte applications with webpack
JavaScript
303
star
22

sites

Monorepo for the sites in the Svelte ecosystem
Svelte
291
star
23

eslint-plugin-svelte

ESLint plugin for Svelte using AST
TypeScript
287
star
24

svelte-repl

The <Repl> component used on the Svelte website
Svelte
274
star
25

rfcs

RFCs for changes to Svelte
273
star
26

sapper-studio

An electron app for building Sapper projects
HTML
220
star
27

v2.svelte.dev

The Svelte v2 website
HTML
209
star
28

svelte-hmr

HMR commons for Svelte 3
JavaScript
202
star
29

site-kit

Svelte
196
star
30

hn.svelte.dev

Hacker News clone built with Svelte and Sapper
Svelte
164
star
31

svelte-todomvc

TodoMVC implemented in Svelte
Svelte
139
star
32

svelte-subdivide

A component for building Blender-style layouts in Svelte apps
JavaScript
130
star
33

svelte-cli

Command line interface for Svelte
JavaScript
104
star
34

svelte-eslint-parser

Svelte parser for ESLint
TypeScript
92
star
35

gestures

Svelte actions for cross-platform gesture detection
TypeScript
89
star
36

svelte-hackernews

WIP Hacker News clone written in Svelte
JavaScript
78
star
37

svelte-custom-elements

Turn Svelte components into web components
JavaScript
49
star
38

svelte-extras

Extra methods for Svelte components
JavaScript
43
star
39

sapper-template-rollup

Starter Rollup template for Sapper apps
JavaScript
40
star
40

kit-template-default

The default SvelteKit template, generated with create-svelte
Svelte
36
star
41

svelte-upgrade

Upgrade your Svelte templates for version 2
JavaScript
36
star
42

examples

A collection of Svelte(Kit) examples
35
star
43

branding

Logos etc for Svelte and related projects
35
star
44

hn.svelte.technology

Hacker News, built with Sapper
HTML
35
star
45

svelte-transitions

Officially supported transition plugins for Svelte
JavaScript
33
star
46

sapper-template-webpack

Starter webpack template for Sapper apps
JavaScript
32
star
47

discord-bot

TypeScript
32
star
48

svelte-atom

Syntax, diagnostics, and other smarts for Svelte in Atom
JavaScript
30
star
49

community

28
star
50

svelte-dbmonster

Svelte implementation of DBMonster
JavaScript
27
star
51

template-custom-element

Template for building basic applications with Svelte and custom elements
HTML
21
star
52

generate-ssr

Server-side rendering for Svelte
JavaScript
21
star
53

kit-sandbox

A sandbox for maintainers
JavaScript
18
star
54

template-store

Demonstrating the use of svelte/store
JavaScript
17
star
55

svelte-transitions-draw

Draw transition plugin for Svelte
JavaScript
16
star
56

api.svelte.dev

The API worker source for https://api.svelte.dev
TypeScript
14
star
57

sapper-legacy.svelte.dev

Old docs site for Sapper
HTML
13
star
58

action-deploy-docs

github action for the svelte org to deploy documentation to the svelte api
TypeScript
12
star
59

svelte-bench

Benchmarks for Svelte
JavaScript
10
star
60

sveltegram

Sapper/Svelte remix of nextgram
JavaScript
8
star
61

svelte-transitions-fade

Fade transition plugin for Svelte
JavaScript
8
star
62

assets

Large static files used on the Svelte website
7
star
63

svelte-transitions-slide

Slide transition plugin for Svelte
JavaScript
5
star
64

svelte-transitions-fly

Fly transition plugin for Svelte
JavaScript
4
star
65

redirects

Redirect old Svelte websites to their shiny new equivalents
JavaScript
3
star