Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit ad7cfad

Browse files
gunchas-panferov
authored andcommitted
feat: allow setting getCustomTransformers as a path to a module (#531)
When using forked checker mode, it's impossible to pass a custom function to the checker via the options as it's a different process. To get around this, we can pass a path to the module that is then exceuted once the checker is initialized.
1 parent edc8ed9 commit ad7cfad

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/checker/runtime.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,28 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
164164

165165
class Host implements ts.LanguageServiceHost {
166166
filesRegex: RegExp;
167-
getCustomTransformers = loaderConfig.getCustomTransformers;
167+
getCustomTransformers?: () => ts.CustomTransformers | undefined;
168168

169169
constructor(filesRegex: RegExp) {
170170
this.filesRegex = filesRegex;
171+
172+
let {getCustomTransformers} = loaderConfig;
173+
174+
if (typeof getCustomTransformers === "function") {
175+
this.getCustomTransformers = getCustomTransformers;
176+
} else if (typeof getCustomTransformers === "string") {
177+
try {
178+
getCustomTransformers = require(getCustomTransformers);
179+
} catch (err) {
180+
throw new Error(`Failed to load customTransformers from "${loaderConfig.getCustomTransformers}": ${err.message}`)
181+
};
182+
183+
if (typeof getCustomTransformers !== "function") {
184+
throw new Error(`Custom transformers in "${loaderConfig.getCustomTransformers}" should export a function, got ${typeof getCustomTransformers}`)
185+
};
186+
187+
this.getCustomTransformers = getCustomTransformers;
188+
};
171189
}
172190

173191
getProjectVersion() { return projectVersion.toString(); }
@@ -400,7 +418,8 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re
400418
const trans = compiler.transpileModule(files.get(fileName).text, {
401419
compilerOptions: compilerOptions,
402420
fileName,
403-
reportDiagnostics: false
421+
reportDiagnostics: false,
422+
transformers: host.getCustomTransformers ? host.getCustomTransformers() : undefined,
404423
});
405424

406425
return {

src/interfaces.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface LoaderConfig {
2828
debug?: boolean;
2929
reportFiles?: string[];
3030
context?: string;
31-
getCustomTransformers?(): ts.CustomTransformers | undefined;
31+
getCustomTransformers?: string | (() => ts.CustomTransformers | undefined);
3232
}
3333

3434
export interface OutputFile {

0 commit comments

Comments
 (0)