From f37dacb9f02a76b0f05c4c0abceae75e677a6662 Mon Sep 17 00:00:00 2001 From: Guntars Asmanis-Graham Date: Tue, 10 Oct 2017 17:42:55 -0400 Subject: [PATCH] feat: allow setting getCustomTransformers as a path to a module 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. --- src/checker/runtime.ts | 23 +++++++++++++++++++++-- src/interfaces.ts | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) 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 {