Skip to content

Commit b8a70f9

Browse files
authored
Update definition files in watch mode in webpack@5 (#1249)
* Add afterDeclarations to getCustomTransformers in README.md * Emit d.ts file in subsequent runs in watch mode * Update package.json and changelog.md
1 parent 6816735 commit b8a70f9

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Changelog
2+
3+
## v8.0.15
4+
* [Update definition files in watch mode in webpack@5](https://github.com/TypeStrong/ts-loader/pull/1249) - thanks @appzuka,@JonWallsten,@alexander-akait
5+
* [Add afterDeclarations to getCustomTransformers in README.md](https://github.com/TypeStrong/ts-loader/pull/1248) - thanks @appzuka
6+
27
## v8.0.14
38
* [Upgrade `chalk`, `loader-utils`, and `semver` to latest stable versions](https://github.com/TypeStrong/ts-loader/pull/1237) - thanks Avi Vahl
49

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-loader",
3-
"version": "8.0.14",
3+
"version": "8.0.15",
44
"description": "TypeScript loader for webpack",
55
"main": "index.js",
66
"types": "dist",

src/instances.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -336,21 +336,33 @@ const addAssetHooks = !!webpack.version!.match(/^4.*/)
336336
makeAfterCompile(instance, false, true, instance.configFilePath)
337337
);
338338

339-
// Emit the assets at the afterProcessAssets stage
340-
loader._compilation.hooks.afterProcessAssets.tap(
341-
'ts-loader',
342-
(_: any) => {
343-
makeAfterCompile(
344-
instance,
345-
true,
346-
false,
347-
instance.configFilePath
348-
)(loader._compilation, () => {
349-
return null;
350-
});
351-
}
339+
// makeAfterCompile is a closure. It returns a function which closes over the variable checkAllFilesForErrors
340+
// We need to get the function once and then reuse it, otherwise it will be recreated each time
341+
// and all files will always be checked.
342+
const cachedMakeAfterCompile = makeAfterCompile(
343+
instance,
344+
true,
345+
false,
346+
instance.configFilePath
352347
);
353348

349+
// compilation is actually of type webpack.compilation.Compilation, but afterProcessAssets
350+
// only exists in webpack5 and at the time of writing ts-loader is built using webpack4
351+
const makeAssetsCallback = (compilation: any) => {
352+
compilation.hooks.afterProcessAssets.tap('ts-loader', () =>
353+
cachedMakeAfterCompile(compilation, () => {
354+
return null;
355+
})
356+
);
357+
};
358+
359+
// We need to add the hook above for each run.
360+
// For the first run, we just need to add the hook to loader._compilation
361+
makeAssetsCallback(loader._compilation);
362+
363+
// For future calls in watch mode we need to watch for a new compilation and add the hook
364+
loader._compiler.hooks.compilation.tap('ts-loader', makeAssetsCallback);
365+
354366
// It may be better to add assets at the processAssets stage (https://webpack.js.org/api/compilation-hooks/#processassets)
355367
// This requires Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, which does not exist in webpack4
356368
// Consider changing this when ts-loader is built using webpack5

0 commit comments

Comments
 (0)