Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to get the latest program #1352

Merged
merged 1 commit into from Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
johnnyreilly marked this conversation as resolved.
Show resolved Hide resolved
) => typescript.CustomTransformers | undefined);
experimentalWatchApi: boolean;
allowTsInNodeModules: boolean;
Expand Down