From 0b35e189559eb32a00188f56d242927939730d0b Mon Sep 17 00:00:00 2001 From: Nick Excell Date: Wed, 3 Feb 2021 14:21:40 +0000 Subject: [PATCH 1/5] Add afterDeclarations to getCustomTransformers in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 65d4f2051..7b313efcb 100644 --- a/README.md +++ b/README.md @@ -344,7 +344,7 @@ These options should be functions which will be used to resolve the import state #### getCustomTransformers | Type | |------| -| ` (program: Program) => { before?: TransformerFactory[]; after?: TransformerFactory[]; } ` | +| ` (program: Program) => { before?: TransformerFactory[]; after?: TransformerFactory[]; afterDeclarations?: TransformerFactory[]; } ` | 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). From 484e47f9f900e91680f22587bc072feb688ae1c2 Mon Sep 17 00:00:00 2001 From: Nick Excell Date: Wed, 3 Feb 2021 23:22:36 +0000 Subject: [PATCH 2/5] Emit d.ts file in subsequent runs in watch mode --- src/instances.ts | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/instances.ts b/src/instances.ts index a670e4262..ad5ddf0a9 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -336,21 +336,33 @@ const addAssetHooks = !!webpack.version!.match(/^4.*/) makeAfterCompile(instance, false, true, instance.configFilePath) ); - // Emit the assets at the afterProcessAssets stage - loader._compilation.hooks.afterProcessAssets.tap( - 'ts-loader', - (_: any) => { - makeAfterCompile( - instance, - true, - false, - instance.configFilePath - )(loader._compilation, () => { - return null; - }); - } + // makeAfterCompile is a closure. It returns a function which closes over the variable checkAllFilesForErrors + // We need to get the function once and then reuse it, otherwise it will be recreated each time + // and all files will always be checked. + const cachedMakeAfterCompile = makeAfterCompile( + instance, + true, + false, + instance.configFilePath ); + // compilation is actually of type webpack.compilation.Compilation, but afterProcessAssets + // only exists in webpack5 and at the time of writing ts-loader is built using webpack4 + const makeAssetsCallback = (compilation: any) => { + compilation.hooks.afterProcessAssets.tap('ts-loader', () => + cachedMakeAfterCompile(compilation, () => { + return null; + }) + ); + }; + + // We need to add the hook above for each run. + // For the first run, we just need to add the hook to loader._compilation + makeAssetsCallback(loader._compilation); + + // For future calls in watch mode we need to watch for a new compilation and add the hook + loader._compiler.hooks.compilation.tap('ts-loader', makeAssetsCallback); + // It may be better to add assets at the processAssets stage (https://webpack.js.org/api/compilation-hooks/#processassets) // This requires Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, which does not exist in webpack4 // Consider changing this when ts-loader is built using webpack5 From 66af28d43bd681acc9087fa6b72e8c71032cb86a Mon Sep 17 00:00:00 2001 From: Nick Excell Date: Thu, 4 Feb 2021 18:15:33 +0000 Subject: [PATCH 3/5] Update package.json and changelog.md --- CHANGELOG.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48adf5178..af8bb0446 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ # Changelog + +## v8.0.15 +* [Update definition files in watch mode in webpack@5](https://github.com/TypeStrong/ts-loader/pull/1249) - thanks @appzuka,@JonWallsten,@alexander-akait +* [Add afterDeclarations to getCustomTransformers in README.md](https://github.com/TypeStrong/ts-loader/pull/1248) - thanks @appzuka + ## v8.0.14 * [Upgrade `chalk`, `loader-utils`, and `semver` to latest stable versions](https://github.com/TypeStrong/ts-loader/pull/1237) - thanks Avi Vahl diff --git a/package.json b/package.json index ad688d5c6..90b5ff1f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-loader", - "version": "8.0.14", + "version": "8.0.15", "description": "TypeScript loader for webpack", "main": "index.js", "types": "dist", From 834873c590db42e2aff632564fa7fc73e0c5a6d3 Mon Sep 17 00:00:00 2001 From: Nick Excell Date: Mon, 8 Feb 2021 10:42:28 +0000 Subject: [PATCH 4/5] Re-fixed missing errors in watch mode --- CHANGELOG.md | 3 +++ package.json | 2 +- src/after-compile.ts | 54 ++++++++++++++++++-------------------------- src/instances.ts | 11 +-------- 4 files changed, 27 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af8bb0446..fe811fa87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## v8.0.16 +* [Re-Fixed missing errors in watch mode in webpack5](https://github.com/TypeStrong/ts-loader/issues/1204) - thanks @appzuka + ## v8.0.15 * [Update definition files in watch mode in webpack@5](https://github.com/TypeStrong/ts-loader/pull/1249) - thanks @appzuka,@JonWallsten,@alexander-akait * [Add afterDeclarations to getCustomTransformers in README.md](https://github.com/TypeStrong/ts-loader/pull/1248) - thanks @appzuka diff --git a/package.json b/package.json index 90b5ff1f3..2b31163b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-loader", - "version": "8.0.15", + "version": "8.0.16", "description": "TypeScript loader for webpack", "main": "index.js", "types": "dist", diff --git a/src/after-compile.ts b/src/after-compile.ts index 7cc284a08..03fa8543a 100644 --- a/src/after-compile.ts +++ b/src/after-compile.ts @@ -29,8 +29,6 @@ import { */ export function makeAfterCompile( instance: TSInstance, - addAssets: boolean, - provideErrors: boolean, configFilePath: string | undefined ) { let getCompilerOptionDiagnostics = true; @@ -47,22 +45,18 @@ export function makeAfterCompile( } if (instance.loaderOptions.transpileOnly) { - if (addAssets) { - provideAssetsFromSolutionBuilderHost(instance, compilation); - } + provideAssetsFromSolutionBuilderHost(instance, compilation); callback(); return; } removeCompilationTSLoaderErrors(compilation, instance.loaderOptions); - if (provideErrors) { - provideCompilerOptionDiagnosticErrorsToWebpack( - getCompilerOptionDiagnostics, - compilation, - instance, - configFilePath - ); - } + provideCompilerOptionDiagnosticErrorsToWebpack( + getCompilerOptionDiagnostics, + compilation, + instance, + configFilePath + ); getCompilerOptionDiagnostics = false; const modules = determineModules(compilation, instance); @@ -74,25 +68,21 @@ export function makeAfterCompile( checkAllFilesForErrors = false; const filesWithErrors: TSFiles = new Map(); - if (provideErrors) { - provideErrorsToWebpack( - filesToCheckForErrors, - filesWithErrors, - compilation, - modules, - instance - ); - provideSolutionErrorsToWebpack(compilation, modules, instance); - } - if (addAssets) { - provideDeclarationFilesToWebpack( - filesToCheckForErrors, - instance, - compilation - ); - provideTsBuildInfoFilesToWebpack(instance, compilation); - provideAssetsFromSolutionBuilderHost(instance, compilation); - } + provideErrorsToWebpack( + filesToCheckForErrors, + filesWithErrors, + compilation, + modules, + instance + ); + provideSolutionErrorsToWebpack(compilation, modules, instance); + provideDeclarationFilesToWebpack( + filesToCheckForErrors, + instance, + compilation + ); + provideTsBuildInfoFilesToWebpack(instance, compilation); + provideAssetsFromSolutionBuilderHost(instance, compilation); instance.filesWithErrors = filesWithErrors; instance.modifiedFiles = undefined; diff --git a/src/instances.ts b/src/instances.ts index ad5ddf0a9..3d7c299c5 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -323,26 +323,17 @@ const addAssetHooks = !!webpack.version!.match(/^4.*/) // add makeAfterCompile with addAssets = true to emit assets and report errors loader._compiler.hooks.afterCompile.tapAsync( 'ts-loader', - makeAfterCompile(instance, true, true, instance.configFilePath) + makeAfterCompile(instance, instance.configFilePath) ); } : (loader: webpack.loader.LoaderContext, instance: TSInstance) => { // We must be running under webpack 5+ - // Add makeAfterCompile with addAssets = false to suppress emitting assets - // during the afterCompile stage. Errors will be still be reported to webpack - loader._compiler.hooks.afterCompile.tapAsync( - 'ts-loader', - makeAfterCompile(instance, false, true, instance.configFilePath) - ); - // makeAfterCompile is a closure. It returns a function which closes over the variable checkAllFilesForErrors // We need to get the function once and then reuse it, otherwise it will be recreated each time // and all files will always be checked. const cachedMakeAfterCompile = makeAfterCompile( instance, - true, - false, instance.configFilePath ); From f2be750385c91ede795e8296bfac52e2142ff88f Mon Sep 17 00:00:00 2001 From: Nick Excell Date: Mon, 8 Feb 2021 14:35:35 +0000 Subject: [PATCH 5/5] Correct merge error --- src/instances.ts | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/instances.ts b/src/instances.ts index 4c709bc06..3d7c299c5 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -354,23 +354,6 @@ const addAssetHooks = !!webpack.version!.match(/^4.*/) // For future calls in watch mode we need to watch for a new compilation and add the hook loader._compiler.hooks.compilation.tap('ts-loader', makeAssetsCallback); - // compilation is actually of type webpack.compilation.Compilation, but afterProcessAssets - // only exists in webpack5 and at the time of writing ts-loader is built using webpack4 - const makeAssetsCallback = (compilation: any) => { - compilation.hooks.afterProcessAssets.tap('ts-loader', () => - cachedMakeAfterCompile(compilation, () => { - return null; - }) - ); - }; - - // We need to add the hook above for each run. - // For the first run, we just need to add the hook to loader._compilation - makeAssetsCallback(loader._compilation); - - // For future calls in watch mode we need to watch for a new compilation and add the hook - loader._compiler.hooks.compilation.tap('ts-loader', makeAssetsCallback); - // It may be better to add assets at the processAssets stage (https://webpack.js.org/api/compilation-hooks/#processassets) // This requires Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, which does not exist in webpack4 // Consider changing this when ts-loader is built using webpack5