Skip to content

Commit 9010a44

Browse files
authored
chore: provide option to pass Svelte compiler path (#471)
Tooling can use this to work around some Svelte major version differences
1 parent 10d387a commit 9010a44

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface PluginConfig {
66
svelteBracketNewLine?: boolean;
77
svelteAllowShorthand?: boolean;
88
svelteIndentScriptAndStyle?: boolean;
9+
svelte5CompilerPath?: string;
910
}
1011

1112
export type PrettierConfig = PluginConfig & Config;

src/index.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,22 @@ export const languages: Partial<SupportLanguage>[] = [
3131
export const parsers: Record<string, Parser> = {
3232
svelte: {
3333
hasPragma,
34-
parse: (text) => {
34+
parse: async (text, options: ParserOptions) => {
3535
try {
36-
return <ASTNode>{ ...parse(text), __isRoot: true };
36+
let _parse = parse;
37+
if (options.svelte5CompilerPath) {
38+
try {
39+
_parse = (await import(options.svelte5CompilerPath)).parse;
40+
} catch (e) {
41+
console.warn(
42+
`Failed to load Svelte 5 compiler from ${options.svelte5CompilerPath}`,
43+
);
44+
console.warn(e);
45+
options.svelte5CompilerPath = undefined;
46+
}
47+
}
48+
49+
return <ASTNode>{ ..._parse(text), __isRoot: true };
3750
} catch (err: any) {
3851
if (err.start != null && err.end != null) {
3952
// Prettier expects error objects to have loc.start and loc.end fields.
@@ -57,8 +70,9 @@ export const parsers: Record<string, Parser> = {
5770
// Therefore we do it ourselves here.
5871
options.originalText = text;
5972
// Only Svelte 5 can have TS in the template
60-
options._svelte_ts = isSvelte5Plus && result.isTypescript;
61-
options._svelte_is5Plus = isSvelte5Plus;
73+
const is = !!options.svelte5CompilerPath || isSvelte5Plus;
74+
options._svelte_ts = is && result.isTypescript;
75+
options._svelte_is5Plus = is;
6276
return text;
6377
},
6478
locStart,

src/options.ts

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ function makeChoice(choice: string) {
1919
}
2020

2121
export const options: Record<keyof PluginConfig, SupportOption> = {
22+
svelte5CompilerPath: {
23+
category: 'Svelte',
24+
type: 'string',
25+
default: '',
26+
description: 'Only set this when using Svelte 5! Path to the Svelte 5 compiler',
27+
},
2228
svelteSortOrder: {
2329
category: 'Svelte',
2430
type: 'choice',

0 commit comments

Comments
 (0)