Skip to content

Commit

Permalink
Pass ts.Program to getCustomTransformers()
Browse files Browse the repository at this point in the history
A lot of transformers requires a Program instance so we pass ts.Program
to getCustomTransformers() to be able to pass it on to transformers
  • Loading branch information
andersekdahl committed Dec 28, 2018
1 parent 2562908 commit 54d8f05
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -252,7 +252,7 @@ This will ensure that the plugin checks for both syntactic errors (eg `const arr

Also, if you are using `thread-loader` in watch mode, remember to set `poolTimeout: Infinity` so workers don't die.

#### getCustomTransformers _( () => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; } )_
#### getCustomTransformers _( (program: Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; } )_

Provide custom transformers - only compatible with TypeScript 2.3+ (and 2.4 if using `transpileOnly` mode). For example usage take a look at [typescript-plugin-styled-components](https://github.com/Igorbek/typescript-plugin-styled-components) or our [test](test/comparison-tests/customTransformer).

Expand Down
8 changes: 6 additions & 2 deletions src/instances.ts
Expand Up @@ -188,7 +188,7 @@ function successfulTypeScriptInstance(
program,
dependencyGraph: {},
reverseDependencyGraph: {},
transformers: getCustomTransformers(),
transformers: getCustomTransformers(program),
colors
};

Expand Down Expand Up @@ -235,7 +235,7 @@ function successfulTypeScriptInstance(
otherFiles,
languageService: null,
version: 0,
transformers: getCustomTransformers(),
transformers: {} as typescript.CustomTransformers, // this is only set temporarily, custom transformers are created further down
dependencyGraph: {},
reverseDependencyGraph: {},
modifiedFiles: null,
Expand Down Expand Up @@ -267,6 +267,8 @@ function successfulTypeScriptInstance(
instance.program = instance.watchOfFilesAndCompilerOptions
.getProgram()
.getProgram();

instance.transformers = getCustomTransformers(instance.program);
} else {
const servicesHost = makeServicesHost(
scriptRegex,
Expand All @@ -285,6 +287,8 @@ function successfulTypeScriptInstance(
if (servicesHost.clearCache !== null) {
loader._compiler.hooks.watchRun.tap('ts-loader', servicesHost.clearCache);
}

instance.transformers = getCustomTransformers(instance.languageService!.getProgram());
}

loader._compiler.hooks.afterCompile.tapAsync(
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Expand Up @@ -330,7 +330,7 @@ export interface LoaderOptions {
happyPackMode: boolean;
getCustomTransformers?:
| string
| (() => typescript.CustomTransformers | undefined);
| ((program: typescript.Program) => typescript.CustomTransformers | undefined);
experimentalWatchApi: boolean;
allowTsInNodeModules: boolean;
experimentalFileCaching: boolean;
Expand Down
2 changes: 1 addition & 1 deletion test/comparison-tests/customTransformer/webpack.config.js
Expand Up @@ -17,7 +17,7 @@ module.exports = {
test: /\.ts$/,
loader: 'ts-loader',
options: {
getCustomTransformers: () => ({
getCustomTransformers: (program) => ({
before: [uppercaseStringLiteralTransformer]
})
}
Expand Down
@@ -1,5 +1,5 @@
var uppercaseStringLiteralTransformer = require('./uppercaseStringLiteralTransformer').default;

module.exports = () => ({
module.exports = (program) => ({
before: [uppercaseStringLiteralTransformer]
});

0 comments on commit 54d8f05

Please sign in to comment.