Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: support function for compilerOptions #2623

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading