diff --git a/src/checker/runtime.ts b/src/checker/runtime.ts index a6d509c..5e52574 100644 --- a/src/checker/runtime.ts +++ b/src/checker/runtime.ts @@ -164,10 +164,28 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re class Host implements ts.LanguageServiceHost { filesRegex: RegExp; - getCustomTransformers = loaderConfig.getCustomTransformers; + getCustomTransformers?: () => ts.CustomTransformers | undefined; constructor(filesRegex: RegExp) { this.filesRegex = filesRegex; + + let {getCustomTransformers} = loaderConfig; + + if (typeof getCustomTransformers === "function") { + this.getCustomTransformers = getCustomTransformers; + } else if (typeof getCustomTransformers === "string") { + try { + getCustomTransformers = require(getCustomTransformers); + } catch (err) { + throw new Error(`Failed to load customTransformers from "${loaderConfig.getCustomTransformers}": ${err.message}`) + }; + + if (typeof getCustomTransformers !== "function") { + throw new Error(`Custom transformers in "${loaderConfig.getCustomTransformers}" should export a function, got ${typeof getCustomTransformers}`) + }; + + this.getCustomTransformers = getCustomTransformers; + }; } getProjectVersion() { return projectVersion.toString(); } @@ -400,7 +418,8 @@ function createChecker(receive: (cb: (msg: Req) => void) => void, send: (msg: Re const trans = compiler.transpileModule(files.get(fileName).text, { compilerOptions: compilerOptions, fileName, - reportDiagnostics: false + reportDiagnostics: false, + transformers: host.getCustomTransformers ? host.getCustomTransformers() : undefined, }); return { diff --git a/src/interfaces.ts b/src/interfaces.ts index 91ff2fe..04ea92f 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -28,7 +28,7 @@ export interface LoaderConfig { debug?: boolean; reportFiles?: string[]; context?: string; - getCustomTransformers?(): ts.CustomTransformers | undefined; + getCustomTransformers?: string | (() => ts.CustomTransformers | undefined); } export interface OutputFile {