@@ -15,7 +15,7 @@ import { Schema as BrowserBuilderOptions } from '../../builders/browser/schema';
15
15
import { BudgetCalculatorResult } from '../../utils/bundle-calculator' ;
16
16
import { colors as ansiColors , removeColor } from '../../utils/color' ;
17
17
import { markAsyncChunksNonInitial } from './async-chunks' ;
18
- import { getStatsOptions , normalizeExtraEntryPoints } from './helpers' ;
18
+ import { WebpackStatsOptions , getStatsOptions , normalizeExtraEntryPoints } from './helpers' ;
19
19
20
20
export function formatSize ( size : number ) : string {
21
21
if ( size <= 0 ) {
@@ -317,8 +317,10 @@ function statsToString(
317
317
}
318
318
}
319
319
320
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
321
- export function statsWarningsToString ( json : StatsCompilation , statsConfig : any ) : string {
320
+ export function statsWarningsToString (
321
+ json : StatsCompilation ,
322
+ statsConfig : WebpackStatsOptions ,
323
+ ) : string {
322
324
const colors = statsConfig . colors ;
323
325
const c = ( x : string ) => ( colors ? ansiColors . reset . cyan ( x ) : x ) ;
324
326
const y = ( x : string ) => ( colors ? ansiColors . reset . yellow ( x ) : x ) ;
@@ -352,8 +354,10 @@ export function statsWarningsToString(json: StatsCompilation, statsConfig: any):
352
354
return output ? '\n' + output : output ;
353
355
}
354
356
355
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
356
- export function statsErrorsToString ( json : StatsCompilation , statsConfig : any ) : string {
357
+ export function statsErrorsToString (
358
+ json : StatsCompilation ,
359
+ statsConfig : WebpackStatsOptions ,
360
+ ) : string {
357
361
const colors = statsConfig . colors ;
358
362
const c = ( x : string ) => ( colors ? ansiColors . reset . cyan ( x ) : x ) ;
359
363
const yb = ( x : string ) => ( colors ? ansiColors . reset . yellowBright ( x ) : x ) ;
@@ -369,18 +373,36 @@ export function statsErrorsToString(json: StatsCompilation, statsConfig: any): s
369
373
if ( typeof error === 'string' ) {
370
374
output += r ( `Error: ${ error } \n\n` ) ;
371
375
} else {
372
- const file = error . file || error . moduleName ;
376
+ let file = error . file || error . moduleName ;
377
+ // Clean up error paths
378
+ // Ex: ./src/app/styles.scss.webpack[javascript/auto]!=!./node_modules/css-loader/dist/cjs.js....
379
+ // to ./src/app/styles.scss.webpack
380
+ if ( file && ! statsConfig . errorDetails ) {
381
+ const webpackPathIndex = file . indexOf ( '.webpack[' ) ;
382
+ if ( webpackPathIndex !== - 1 ) {
383
+ file = file . substring ( 0 , webpackPathIndex ) ;
384
+ }
385
+ }
386
+
373
387
if ( file ) {
374
388
output += c ( file ) ;
375
389
if ( error . loc ) {
376
390
output += ':' + yb ( error . loc ) ;
377
391
}
378
392
output += ' - ' ;
379
393
}
380
- if ( ! / ^ e r r o r / i. test ( error . message ) ) {
394
+
395
+ // In most cases webpack will add stack traces to error messages.
396
+ // This below cleans up the error from stacks.
397
+ // See: https://github.com/webpack/webpack/issues/15980
398
+ const message = statsConfig . errorStack
399
+ ? error . message
400
+ : / [ \s \S ] + ?(? = [ \n \s ] + a t ) / . exec ( error . message ) ?. [ 0 ] ?? error . message ;
401
+
402
+ if ( ! / ^ e r r o r / i. test ( message ) ) {
381
403
output += r ( 'Error: ' ) ;
382
404
}
383
- output += `${ error . message } \n\n` ;
405
+ output += `${ message } \n\n` ;
384
406
}
385
407
}
386
408
@@ -428,9 +450,14 @@ export function webpackStatsLogger(
428
450
) : void {
429
451
logger . info ( statsToString ( json , config . stats , budgetFailures ) ) ;
430
452
453
+ if ( typeof config . stats !== 'object' ) {
454
+ throw new Error ( 'Invalid Webpack stats configuration.' ) ;
455
+ }
456
+
431
457
if ( statsHasWarnings ( json ) ) {
432
458
logger . warn ( statsWarningsToString ( json , config . stats ) ) ;
433
459
}
460
+
434
461
if ( statsHasErrors ( json ) ) {
435
462
logger . error ( statsErrorsToString ( json , config . stats ) ) ;
436
463
}
0 commit comments