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 27, 2018
1 parent 2562908 commit 8a1af42
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
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
6 changes: 4 additions & 2 deletions src/instances.ts
Original file line number Diff line number Diff line change
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 Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
test: /\.ts$/,
loader: 'ts-loader',
options: {
getCustomTransformers: () => ({
getCustomTransformers: (program) => ({
before: [uppercaseStringLiteralTransformer]
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var uppercaseStringLiteralTransformer = require('./uppercaseStringLiteralTransformer').default;

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

0 comments on commit 8a1af42

Please sign in to comment.