Skip to content

Commit 2faad30

Browse files
committed
chore: support function for compilerOptions
For svelte2tsx we gotta make a compromise - since everything has to be sync, we can only pass the unpreprocessed file. Should be fine in practise because a preprocessor influencing the code in such a way that it in turn influences the compiler options is probably vanishingly rare. sveltejs/svelte#12415
1 parent 02db54d commit 2faad30

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

packages/language-server/src/lib/documents/configLoader.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export type InternalPreprocessorGroup = PreprocessorGroup & {
2323
};
2424

2525
export interface SvelteConfig {
26-
compilerOptions?: CompileOptions;
26+
compilerOptions?:
27+
| CompileOptions
28+
| ((input: { filename: string; code: string }) => CompileOptions);
2729
preprocess?: InternalPreprocessorGroup | InternalPreprocessorGroup[];
2830
loadConfigError?: any;
2931
isFallbackConfig?: boolean;

packages/language-server/src/plugins/svelte/SvelteDocument.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,15 @@ export class SvelteDocument {
9898
return this.compileResult;
9999
}
100100

101-
async getCompiledWith(options: CompileOptions = {}): Promise<SvelteCompileResult> {
101+
async getCompiledWith(options: SvelteConfig['compilerOptions']): Promise<SvelteCompileResult> {
102102
const svelte = importSvelte(this.getFilePath());
103-
return svelte.compile((await this.getTranspiled()).getText(), options);
103+
const code = (await this.getTranspiled()).getText();
104+
return svelte.compile(
105+
code,
106+
typeof options === 'function'
107+
? options({ filename: this.getFilePath(), code })
108+
: (options ?? {})
109+
);
104110
}
105111

106112
private getSvelteVersion() {

packages/language-server/src/plugins/typescript/DocumentSnapshot.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ function preprocessSvelteFile(document: Document, options: SvelteSnapshotOptions
200200
: ts.ScriptKind.JS;
201201

202202
try {
203+
const compilerOptions =
204+
typeof document.config?.compilerOptions === 'function'
205+
? document.config.compilerOptions({
206+
filename: document.getFilePath() ?? '',
207+
// Ideally we could pass the preprocessed text here, but we have to be synchronous here
208+
// and the preprocessed text is only available asynchronously. Most people will only
209+
// branch on filename anyway.
210+
code: text
211+
})
212+
: document.config?.compilerOptions;
203213
const tsx = svelte2tsx(text, {
204214
parse: options.parse,
205215
version: options.version,
@@ -208,10 +218,8 @@ function preprocessSvelteFile(document: Document, options: SvelteSnapshotOptions
208218
mode: 'ts',
209219
typingsNamespace: options.typingsNamespace,
210220
emitOnTemplateError: options.transformOnTemplateError,
211-
namespace: document.config?.compilerOptions?.namespace,
212-
accessors:
213-
document.config?.compilerOptions?.accessors ??
214-
document.config?.compilerOptions?.customElement
221+
namespace: compilerOptions?.namespace,
222+
accessors: compilerOptions?.accessors ?? compilerOptions?.customElement
215223
});
216224
text = tsx.code;
217225
tsxMap = tsx.map as EncodedSourceMap;

0 commit comments

Comments
 (0)