diff --git a/index.d.ts b/index.d.ts index 9c8975a..1adfd0e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,6 +6,7 @@ export interface PluginConfig { svelteBracketNewLine?: boolean; svelteAllowShorthand?: boolean; svelteIndentScriptAndStyle?: boolean; + svelte5CompilerPath?: string; } export type PrettierConfig = PluginConfig & Config; diff --git a/src/index.ts b/src/index.ts index e661d5f..dea7ba6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,9 +31,22 @@ export const languages: Partial[] = [ export const parsers: Record = { svelte: { hasPragma, - parse: (text) => { + parse: async (text, options: ParserOptions) => { try { - return { ...parse(text), __isRoot: true }; + let _parse = parse; + if (options.svelte5CompilerPath) { + try { + _parse = (await import(options.svelte5CompilerPath)).parse; + } catch (e) { + console.warn( + `Failed to load Svelte 5 compiler from ${options.svelte5CompilerPath}`, + ); + console.warn(e); + options.svelte5CompilerPath = undefined; + } + } + + return { ..._parse(text), __isRoot: true }; } catch (err: any) { if (err.start != null && err.end != null) { // Prettier expects error objects to have loc.start and loc.end fields. @@ -57,8 +70,9 @@ export const parsers: Record = { // Therefore we do it ourselves here. options.originalText = text; // Only Svelte 5 can have TS in the template - options._svelte_ts = isSvelte5Plus && result.isTypescript; - options._svelte_is5Plus = isSvelte5Plus; + const is = !!options.svelte5CompilerPath || isSvelte5Plus; + options._svelte_ts = is && result.isTypescript; + options._svelte_is5Plus = is; return text; }, locStart, diff --git a/src/options.ts b/src/options.ts index 18d65fd..f5c504b 100644 --- a/src/options.ts +++ b/src/options.ts @@ -19,6 +19,12 @@ function makeChoice(choice: string) { } export const options: Record = { + svelte5CompilerPath: { + category: 'Svelte', + type: 'string', + default: '', + description: 'Only set this when using Svelte 5! Path to the Svelte 5 compiler', + }, svelteSortOrder: { category: 'Svelte', type: 'choice',