From 0267734a6208d398df23ec84e03faa7047db3646 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Tue, 10 Sep 2024 12:24:13 -0700 Subject: [PATCH] Fix an issue with webpack error printing. --- ...ack-error-formatting_2024-09-10-19-24.json | 10 +++++ .../src/WebpackTask.ts | 43 +++++++++++++------ 2 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 common/changes/@microsoft/gulp-core-build-webpack/fix-webpack-error-formatting_2024-09-10-19-24.json diff --git a/common/changes/@microsoft/gulp-core-build-webpack/fix-webpack-error-formatting_2024-09-10-19-24.json b/common/changes/@microsoft/gulp-core-build-webpack/fix-webpack-error-formatting_2024-09-10-19-24.json new file mode 100644 index 0000000..defab76 --- /dev/null +++ b/common/changes/@microsoft/gulp-core-build-webpack/fix-webpack-error-formatting_2024-09-10-19-24.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@microsoft/gulp-core-build-webpack", + "comment": "Fix an issue where webpack errors were printed as `[object Object]`.", + "type": "patch" + } + ], + "packageName": "@microsoft/gulp-core-build-webpack" +} \ No newline at end of file diff --git a/core-build/gulp-core-build-webpack/src/WebpackTask.ts b/core-build/gulp-core-build-webpack/src/WebpackTask.ts index c03d2cb..9c20a23 100644 --- a/core-build/gulp-core-build-webpack/src/WebpackTask.ts +++ b/core-build/gulp-core-build-webpack/src/WebpackTask.ts @@ -46,6 +46,24 @@ export interface IWebpackResources { webpack: typeof Webpack; } +function normalizeWebpackError(error: Webpack.StatsError): string { + if (typeof error === 'string') { + return error; + } else { + const { loc, moduleName, moduleIdentifier } = error; + const modulePath: string | undefined = moduleName ?? moduleIdentifier; + if (modulePath) { + if (loc) { + return `${modulePath}:${loc}: ${error.message}`; + } else { + return `${modulePath}: ${error.message}`; + } + } else { + return error.message; + } + } +} + /** * @public */ @@ -141,7 +159,12 @@ export class WebpackTask extends GulpTask extends GulpTask 0) { - this.logWarning( - `'${outputDir}':` + - EOL + - unsuppressedWarnings - .map( - (unsuppressedWarning) => - (unsuppressedWarning.loc ? `${unsuppressedWarning.loc}: ` : '') + - unsuppressedWarning.message - ) - .join(EOL) + - EOL - ); + const warningTexts: string[] = []; + for (const warning of unsuppressedWarnings) { + warningTexts.push(normalizeWebpackError(warning)); + } + + this.logWarning(`'${outputDir}':` + EOL + warningTexts.join(EOL) + EOL); } }