Skip to content

Commit

Permalink
Pass ts.Program to getCustomTransformers() (#889)
Browse files Browse the repository at this point in the history
* Pass ts.Program to getCustomTransformers()

A lot of transformers requires a Program instance so we pass ts.Program
to getCustomTransformers() to be able to pass it on to transformers

* Update CHANGELOG.md

* Update package.json
  • Loading branch information
andersekdahl authored and johnnyreilly committed Jan 5, 2019
1 parent 465345f commit e3d6367
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v5.3.3

* [fix: Pass ts.Program to getCustomTransformers](https://github.com/TypeStrong/ts-loader/pull/889) (#860) - thanks @andersekdahl!

## v5.3.2

* [feat: enable experimentalFileCaching by default](https://github.com/TypeStrong/ts-loader/pull/885) (#868) - thanks @timocov!
Expand Down
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-loader",
"version": "5.3.2",
"version": "5.3.3",
"description": "TypeScript loader for webpack",
"main": "index.js",
"types": "dist/types/index.d.ts",
Expand Down
8 changes: 6 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 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
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 e3d6367

Please sign in to comment.