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); } }