Skip to content

Commit

Permalink
chore: support function for compilerOptions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dummdidumm committed Dec 9, 2024
1 parent 02db54d commit 2faad30
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/language-server/src/lib/documents/configLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions packages/language-server/src/plugins/svelte/SvelteDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,15 @@ export class SvelteDocument {
return this.compileResult;
}

async getCompiledWith(options: CompileOptions = {}): Promise<SvelteCompileResult> {
async getCompiledWith(options: SvelteConfig['compilerOptions']): Promise<SvelteCompileResult> {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit 2faad30

Please sign in to comment.