Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ yarn add --dev @trivago/prettier-plugin-sort-imports

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

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

### Usage

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

### Used by

Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,32 @@
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/core": "7.26.0",
"@babel/core": "^7.26.0",
"@types/chai": "5.0.1",
"@types/jest": "29.5.14",
"@types/lodash": "4.17.13",
"@types/node": "22.9.0",
"@vue/compiler-sfc": "^3.5.12",
"jest": "29.7.0",
"prettier": "3.3.3",
"prettier-plugin-svelte": "^3.2.6",
"ts-jest": "29.2.5",
"typescript": "5.6.3"
},
"peerDependencies": {
"@vue/compiler-sfc": "3.x",
"prettier": "2.x - 3.x"
"prettier": "2.x - 3.x",
"prettier-plugin-svelte": "3.x"
},
"engines": {
"node": ">18.12"
},
"peerDependenciesMeta": {
"@vue/compiler-sfc": {
"optional": true
},
"prettier-plugin-svelte": {
"optional": true
}
}
}
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import { parsers as typescriptParsers } from 'prettier/plugins/typescript';
import { defaultPreprocessor } from './preprocessors/default-processor';
import { vuePreprocessor } from './preprocessors/vue-preprocessor';

const svelteParsers = createSvelteParsers();

function createSvelteParsers() {
try {
var { parsers } = require('prettier-plugin-svelte');
var { sveltePreprocessor } = require('./preprocessors/svelte-preprocessor');
} catch {
return {};
}
return {
svelte: {
...parsers.svelte,
preprocess: sveltePreprocessor,
},
};
}

const options = {
importOrder: {
type: 'path',
Expand Down Expand Up @@ -67,6 +84,7 @@ module.exports = {
...htmlParsers.vue,
preprocess: vuePreprocessor,
},
...svelteParsers,
},
options,
};
17 changes: 17 additions & 0 deletions src/preprocessors/svelte-preprocessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor';

const { parsers } = require('prettier-plugin-svelte');
const scriptRegex =
/<!--[^]*?-->|<script((?:\s+[^=>'"\/\s]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"\/\s]+)*\s*)>([^]*?)<\/script>/g;

export function sveltePreprocessor(code: string, options: PrettierOptions) {
code = code.replace(scriptRegex, (match, attributes, content, index) => {
if (match.startsWith('<!--')) {
return match;
}
content = preprocessor(content, options);
return `<script${attributes}>${content}</script>`;
});
return parsers.svelte.preprocess(code, options);
}
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.26.2.tgz#278b6b13664557de95b8f35b90d96785850bb56e"
integrity sha512-Z0WgzSEa+aUcdiJuCIqgujCshpMWgUpgOxXotrYPSA53hA3qopNaqcJpyr0hVb1FeWdnqFA35/fUtXgBK8srQg==

"@babel/core@7.26.0", "@babel/core@^7.11.6", "@babel/core@^7.12.3", "@babel/core@^7.23.9":
"@babel/core@^7.11.6", "@babel/core@^7.12.3", "@babel/core@^7.23.9", "@babel/core@^7.26.0":
version "7.26.0"
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.26.0.tgz#d78b6023cc8f3114ccf049eb219613f74a747b40"
integrity sha512-i1SLeK+DzNnQ3LL/CswPCa/E5u4lh1k6IAEphON8F+cXt0t9euTshDru0q7/IqMa1PMPz5RnHuHscF8/ZJsStg==
Expand Down Expand Up @@ -1940,6 +1940,11 @@ postcss@^8.4.48:
picocolors "^1.1.1"
source-map-js "^1.2.1"

prettier-plugin-svelte@^3.2.6:
version "3.2.7"
resolved "https://registry.yarnpkg.com/prettier-plugin-svelte/-/prettier-plugin-svelte-3.2.7.tgz#10db2d553b48c6ed412e2d00688f8d2eaa274f8a"
integrity sha512-/Dswx/ea0lV34If1eDcG3nulQ63YNr5KPDfMsjbdtpSWOxKKJ7nAc2qlVuYwEvCr4raIuredNoR7K4JCkmTGaQ==

prettier@3.3.3:
version "3.3.3"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.3.tgz#30c54fe0be0d8d12e6ae61dbb10109ea00d53105"
Expand Down