Skip to content

Commit

Permalink
Add function to get the latest program (#1352)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zn4rK committed Aug 3, 2021
1 parent 55d2a85 commit e585556
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## v9.2.5

* [Add function to get the latest program](https://github.com/TypeStrong/ts-loader/pull/1352) - thanks @Zn4rK

## v9.2.4

* [Fix undefined configPath now falls back to default](https://github.com/TypeStrong/ts-loader/pull/1346) - thanks @johnnyreilly
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -333,7 +333,7 @@ These options should be functions which will be used to resolve the import state
#### getCustomTransformers
| Type |
|------|
| ` (program: Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; afterDeclarations?: TransformerFactory<SourceFile>[]; } ` |
| ` (program: Program, getProgram: () => Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; afterDeclarations?: 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
@@ -1,6 +1,6 @@
{
"name": "ts-loader",
"version": "9.2.4",
"version": "9.2.5",
"description": "TypeScript loader for webpack",
"main": "index.js",
"types": "dist",
Expand Down
16 changes: 10 additions & 6 deletions src/instances.ts
Expand Up @@ -377,7 +377,8 @@ export function initializeInstance(
})
: instance.compiler.createProgram([], instance.compilerOptions));

instance.transformers = getCustomTransformers(program);
const getProgram = () => program;
instance.transformers = getCustomTransformers(program, getProgram);
// Setup watch run for solution building
if (instance.solutionBuilderHost) {
addAssetHooks(loader, instance);
Expand Down Expand Up @@ -407,9 +408,13 @@ export function initializeInstance(
instance.compiler.createWatchProgram(instance.watchHost);
instance.builderProgram =
instance.watchOfFilesAndCompilerOptions.getProgram();
instance.program = instance.builderProgram.getProgram();

instance.transformers = getCustomTransformers(instance.program);
const getProgram = () => instance.builderProgram?.getProgram();
instance.program = getProgram();
instance.transformers = getCustomTransformers(
instance.program,
getProgram
);
} else {
instance.servicesHost = makeServicesHost(
getScriptRegexp(instance),
Expand All @@ -423,9 +428,8 @@ export function initializeInstance(
instance.compiler.createDocumentRegistry()
);

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

addAssetHooks(loader, instance);
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces.ts
Expand Up @@ -270,7 +270,8 @@ export interface LoaderOptions {
getCustomTransformers:
| string
| ((
program: typescript.Program
program: typescript.Program,
getProgram: () => typescript.Program
) => typescript.CustomTransformers | undefined);
experimentalWatchApi: boolean;
allowTsInNodeModules: boolean;
Expand Down

0 comments on commit e585556

Please sign in to comment.