From c3c6ce0957bb869db7df45ad96531c5650b047bd Mon Sep 17 00:00:00 2001 From: Behrang Yarahmadi Date: Fri, 6 Dec 2024 14:28:17 +0100 Subject: [PATCH] Fix svelte packages being required all the time (#327) * update example * Fix svelte packages being required all the time --- examples/.prettierrc | 5 ++-- examples/example.svelte | 48 ++++++++++++++++++++++++++++++ src/index.ts | 10 ++++--- src/utils/create-svelte-parsers.ts | 8 +++++ 4 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 examples/example.svelte create mode 100644 src/utils/create-svelte-parsers.ts diff --git a/examples/.prettierrc b/examples/.prettierrc index 1745ccd2..b40f64a2 100644 --- a/examples/.prettierrc +++ b/examples/.prettierrc @@ -3,9 +3,10 @@ "tabWidth": 4, "trailingComma": "all", "singleQuote": true, - "jsxBracketSameLine": true, + "bracketSameLine": true, "semi": true, "importOrder": ["^@server/(.*)$", "^@core/(.*)$", "^@ui/(.*)$", "^[./]"], "importOrderSeparation": true, - "importOrderSortSpecifiers": true + "importOrderSortSpecifiers": true, + "plugins": ["../lib/src/index.js"] } diff --git a/examples/example.svelte b/examples/example.svelte new file mode 100644 index 00000000..526fb1b3 --- /dev/null +++ b/examples/example.svelte @@ -0,0 +1,48 @@ + + +
+

Hello Svelte!

+

The count is {count}

+ +
+ + diff --git a/src/index.ts b/src/index.ts index 0afcba60..46476192 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,10 +6,12 @@ import { parsers as typescriptParsers } from 'prettier/plugins/typescript'; import { defaultPreprocessor } from './preprocessors/default-processor'; import { sveltePreprocessor } from './preprocessors/svelte-preprocessor'; import { vuePreprocessor } from './preprocessors/vue-preprocessor'; +import type { Options } from 'prettier'; +import { createSvelteParsers } from './utils/create-svelte-parsers'; -const { parsers: svelteParsers } = require('prettier-plugin-svelte'); +const svelteParsers = createSvelteParsers(); -const options = { +const options: Options = { importOrder: { type: 'path', category: 'Global', @@ -62,7 +64,7 @@ const options = { category: 'Global', default: 'with', description: 'Provide a keyword for import attributes', - } + }, }; module.exports = { @@ -84,7 +86,7 @@ module.exports = { preprocess: vuePreprocessor, }, svelte: { - ...svelteParsers.svelte, + ...svelteParsers.parsers.svelte, preprocess: sveltePreprocessor, }, }, diff --git a/src/utils/create-svelte-parsers.ts b/src/utils/create-svelte-parsers.ts new file mode 100644 index 00000000..1c72863d --- /dev/null +++ b/src/utils/create-svelte-parsers.ts @@ -0,0 +1,8 @@ +export function createSvelteParsers() { + try { + var { parsers } = require('prettier-plugin-svelte'); + } catch { + return {}; + } + return { parsers }; +} \ No newline at end of file