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

Commit

Permalink
feat: allow setting getCustomTransformers as a path to a module (#531)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guncha authored and s-panferov committed Feb 24, 2018
1 parent edc8ed9 commit ad7cfad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/checker/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ad7cfad

Please sign in to comment.