Skip to content

Commit 5ca881f

Browse files
committed
Add support for svelte files.
1 parent 61d0697 commit 5ca881f

5 files changed

Lines changed: 49 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ yarn add --dev @trivago/prettier-plugin-sort-imports
6969

7070
**Note: If formatting `.vue` sfc files please install `@vue/compiler-sfc` if not in your dependency tree - this normally is within Vue projects.**
7171

72+
**Note: If formatting `.svelte` sfc files please install `prettier-plugin-svelte` and configure it in the Prettier plugin before this plugin - this normally is within Svelte projects.**
73+
7274
### Usage
7375

7476
Add an order in prettier config file.
@@ -222,7 +224,7 @@ Having some trouble or an issue ? You can check [FAQ / Troubleshooting section](
222224
| React | ✅ Everything | - |
223225
| Angular | ✅ Everything | Supported through `importOrderParserPlugins` API |
224226
| Vue | ✅ Everything | `@vue/compiler-sfc` is required |
225-
| Svelte | ⚠️ Soon to be supported. | Any contribution is welcome. |
227+
| Svelte | ✅ Everything | `prettier-plugin-svelte` is required |
226228

227229
### Used by
228230

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,21 @@
4949
"@vue/compiler-sfc": "^3.2.41",
5050
"jest": "26.6.3",
5151
"prettier": "2.8",
52+
"prettier-plugin-svelte": "^3.2.6",
5253
"ts-jest": "26.5.3",
5354
"typescript": "4.9.4"
5455
},
5556
"peerDependencies": {
5657
"@vue/compiler-sfc": "3.x",
57-
"prettier": "2.x - 3.x"
58+
"prettier": "2.x - 3.x",
59+
"prettier-plugin-svelte": "3.x"
5860
},
5961
"peerDependenciesMeta": {
6062
"@vue/compiler-sfc": {
6163
"optional": true
64+
},
65+
"prettier-plugin-svelte": {
66+
"optional": true
6267
}
6368
}
6469
}

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ import { parsers as typescriptParsers } from 'prettier/parser-typescript';
66
import { defaultPreprocessor } from './preprocessors/default-processor';
77
import { vuePreprocessor } from './preprocessors/vue-preprocessor';
88

9+
const svelteParsers = createSvelteParsers();
10+
11+
function createSvelteParsers() {
12+
try {
13+
var { parsers } = require('prettier-plugin-svelte');
14+
var { sveltePreprocessor } = require('./preprocessors/svelte-preprocessor');
15+
} catch {
16+
return {};
17+
}
18+
return {
19+
svelte: {
20+
...parsers.svelte,
21+
preprocess: sveltePreprocessor,
22+
},
23+
};
24+
}
25+
926
const options = {
1027
importOrder: {
1128
type: 'path',
@@ -68,6 +85,7 @@ module.exports = {
6885
...htmlParsers.vue,
6986
preprocess: vuePreprocessor,
7087
},
88+
...svelteParsers,
7189
},
7290
options,
7391
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { PrettierOptions } from '../types';
2+
import { preprocessor } from './preprocessor';
3+
4+
const { parsers } = require('prettier-plugin-svelte');
5+
const scriptRegex =
6+
/<!--[^]*?-->|<script((?:\s+[^=>'"\/\s]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"\/\s]+)*\s*)>([^]*?)<\/script>/g;
7+
8+
export function sveltePreprocessor(code: string, options: PrettierOptions) {
9+
code = code.replace(scriptRegex, (match, attributes, content, index) => {
10+
if (match.startsWith('<!--')) {
11+
return match;
12+
}
13+
content = preprocessor(content, options);
14+
return `<script${attributes}>${content}</script>`;
15+
});
16+
return parsers.svelte.preprocess(code, options);
17+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,6 +2955,11 @@ prelude-ls@~1.1.2:
29552955
resolved "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz"
29562956
integrity "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w== sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w=="
29572957

2958+
prettier-plugin-svelte@^3.2.6:
2959+
version "3.2.7"
2960+
resolved "https://registry.yarnpkg.com/prettier-plugin-svelte/-/prettier-plugin-svelte-3.2.7.tgz#10db2d553b48c6ed412e2d00688f8d2eaa274f8a"
2961+
integrity sha512-/Dswx/ea0lV34If1eDcG3nulQ63YNr5KPDfMsjbdtpSWOxKKJ7nAc2qlVuYwEvCr4raIuredNoR7K4JCkmTGaQ==
2962+
29582963
prettier@2.8:
29592964
version "2.8.0"
29602965
resolved "https://registry.npmjs.org/prettier/-/prettier-2.8.0.tgz"

0 commit comments

Comments
 (0)