diff --git a/packages/language-server/src/lib/documents/configLoader.ts b/packages/language-server/src/lib/documents/configLoader.ts index 49413a6f8..4429e8ce4 100644 --- a/packages/language-server/src/lib/documents/configLoader.ts +++ b/packages/language-server/src/lib/documents/configLoader.ts @@ -23,7 +23,9 @@ export type InternalPreprocessorGroup = PreprocessorGroup & { }; export interface SvelteConfig { - compilerOptions?: CompileOptions; + compilerOptions?: + | CompileOptions + | ((input: { filename: string; code: string }) => CompileOptions); preprocess?: InternalPreprocessorGroup | InternalPreprocessorGroup[]; loadConfigError?: any; isFallbackConfig?: boolean; diff --git a/packages/language-server/src/plugins/svelte/SvelteDocument.ts b/packages/language-server/src/plugins/svelte/SvelteDocument.ts index 65aa1146e..986593d1d 100644 --- a/packages/language-server/src/plugins/svelte/SvelteDocument.ts +++ b/packages/language-server/src/plugins/svelte/SvelteDocument.ts @@ -98,9 +98,15 @@ export class SvelteDocument { return this.compileResult; } - async getCompiledWith(options: CompileOptions = {}): Promise { + async getCompiledWith(options: SvelteConfig['compilerOptions']): Promise { const svelte = importSvelte(this.getFilePath()); - return svelte.compile((await this.getTranspiled()).getText(), options); + const code = (await this.getTranspiled()).getText(); + return svelte.compile( + code, + typeof options === 'function' + ? options({ filename: this.getFilePath(), code }) + : (options ?? {}) + ); } private getSvelteVersion() { diff --git a/packages/language-server/src/plugins/typescript/DocumentSnapshot.ts b/packages/language-server/src/plugins/typescript/DocumentSnapshot.ts index aaf2a11e6..d869a5b59 100644 --- a/packages/language-server/src/plugins/typescript/DocumentSnapshot.ts +++ b/packages/language-server/src/plugins/typescript/DocumentSnapshot.ts @@ -200,6 +200,16 @@ function preprocessSvelteFile(document: Document, options: SvelteSnapshotOptions : ts.ScriptKind.JS; try { + const compilerOptions = + typeof document.config?.compilerOptions === 'function' + ? document.config.compilerOptions({ + filename: document.getFilePath() ?? '', + // Ideally we could pass the preprocessed text here, but we have to be synchronous here + // and the preprocessed text is only available asynchronously. Most people will only + // branch on filename anyway. + code: text + }) + : document.config?.compilerOptions; const tsx = svelte2tsx(text, { parse: options.parse, version: options.version, @@ -208,10 +218,8 @@ function preprocessSvelteFile(document: Document, options: SvelteSnapshotOptions mode: 'ts', typingsNamespace: options.typingsNamespace, emitOnTemplateError: options.transformOnTemplateError, - namespace: document.config?.compilerOptions?.namespace, - accessors: - document.config?.compilerOptions?.accessors ?? - document.config?.compilerOptions?.customElement + namespace: compilerOptions?.namespace, + accessors: compilerOptions?.accessors ?? compilerOptions?.customElement }); text = tsx.code; tsxMap = tsx.map as EncodedSourceMap;