From 2eae5fbfc962961fe42a8543335deacd0be96e84 Mon Sep 17 00:00:00 2001 From: sanex Date: Mon, 12 Oct 2020 21:34:37 +0300 Subject: [PATCH] Fixed deprecation warnings on webpack@5. Fixed https://github.com/TypeStrong/ts-loader/issues/1194 --- src/index.ts | 13 +++++++++++-- src/instances.ts | 22 ++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6ceb4adf5..dc9b01d70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -632,6 +632,7 @@ function getTranspilationEmit( reportDiagnostics: true, fileName, }); + const module = loaderContext._module; addDependenciesFromSolutionBuilder(instance, fileName, file => loaderContext.addDependency(path.resolve(file)) @@ -644,11 +645,19 @@ function getTranspilationEmit( instance.loaderOptions, instance.colors, instance.compiler, - { module: loaderContext._module }, + { module }, loaderContext.context ); - loaderContext._module.errors.push(...errors); + /** + * Since webpack 5, the `errors` property is deprecated, + * so we can check if some methods for reporting errors exist. + */ + if (!!module.addError) { + errors.forEach(error => module.addError(error)); + } else { + module.errors.push(...errors); + } } return { outputText, sourceMapText }; diff --git a/src/instances.ts b/src/instances.ts index 527d89f44..d9dfd079f 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -146,6 +146,7 @@ function successfulTypeScriptInstance( } } + const module = loader._module; const basePath = loaderOptions.context || path.dirname(configFilePath || ''); const configParseResult = getConfigParseResult( compiler, @@ -165,7 +166,15 @@ function successfulTypeScriptInstance( loader.context ); - loader._module.errors.push(...errors); + /** + * Since webpack 5, the `errors` property is deprecated, + * so we can check if some methods for reporting errors exist. + */ + if (!!module.addError) { + errors.forEach(error => module.addError(error)); + } else { + module.errors.push(...errors); + } return { error: makeError( @@ -415,6 +424,7 @@ export function reportTranspileErrors( if (!instance.reportTranspileErrors) { return; } + const module = loader._module; instance.reportTranspileErrors = false; // happypack does not have _module.errors - see https://github.com/TypeStrong/ts-loader/issues/336 if (!instance.loaderOptions.happyPackMode) { @@ -431,7 +441,15 @@ export function reportTranspileErrors( { file: instance.configFilePath || 'tsconfig.json' }, loader.context ); - loader._module.errors.push(...solutionErrors, ...errors); + /** + * Since webpack 5, the `errors` property is deprecated, + * so we can check if some methods for reporting errors exist. + */ + if (!!module.addError) { + [...solutionErrors, ...errors].forEach(error => module.addError(error)); + } else { + module.errors.push(...solutionErrors, ...errors); + } } }