import chalk from 'chalk' ; import { StatsCompilation } from 'webpack' ; const friendlySyntaxErrorLabel = 'Syntax error:' ; type D = Exclude < StatsCompilation [ 'errors' ] , undefined > ; type ArrayType < T > = T extends Array < infer U > ? U : never ; type StatsError = ArrayType < D > ; param message function formatMessage ( rawMessage ) { const fileName = rawMessage . moduleId || rawMessage . file ; let message = fileName ? `${ fileName }\n${ rawMessage . message }` : `${ rawMessage . message }` ; let lines = message . split ( '\n' ) ; lines = lines . filter ( ( line ) => ! /Module [A-z ]+\(from/ . test ( line ) ) ; if ( lines . length > 2 && ~ lines [ 0 ] . indexOf ( lines [ 1 ] . slice ( 2 ) ) ) { lines . splice ( 1 , 1 ) ; } lines = lines . filter ( ( line ) => line . indexOf ( ' @ ' ) !== 0 ) ; lines = lines . map ( ( line ) => { const parseError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/ . exec ( line ) ; if ( ! parseError ) { return line ; } const [ , errorLine , errorColumn , errorMessage ] = parseError ; return `${ friendlySyntaxErrorLabel } ${ errorMessage } (${ errorLine }:${ errorColumn })` ; } ) ; if ( lines . length > 2 && lines [ 1 ] . trim ( ) === '' ) { lines . splice ( 1 , 1 ) ; } if ( lines [ 1 ] && lines [ 1 ] . indexOf ( 'Module not found: ' ) === 0 ) { lines = [ lines [ 0 ] , lines [ 1 ] . replace ( 'Error: ' , '' ) . replace ( 'Module not found: Cannot find file:' , 'Cannot find file:' ) , ] ; } if ( lines [ 0 ] ) { lines [ 0 ] = chalk . underline . bold ( lines [ 0 ] ) ; } message = lines . join ( '\n' ) ; message = message . replace ( /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm , '' ) ; message = message . replace ( /^\s*at\s<anonymous>(\n|$)/gm , '' ) ; lines = message . split ( '\n' ) ; lines = lines . filter ( ( line , index , arr ) => index === 0 || line . trim ( ) !== '' || line . trim ( ) !== arr [ index - 1 ] . trim ( ) ) ; message = lines . join ( '\n' ) ; return message . trim ( ) ; } export function formatWebpackMessages ( json ) { const errors = json . errors ?. map ( ( error ) => formatMessage ( error ) ) . filter ( ( message ) => message !== '' ) ?? [ ] ; const warnings = json . warnings ?. map ( ( warning ) => formatMessage ( warning ) ) . filter ( ( message ) => message !== '' ) ?? [ ] ; return { errors , warnings , } ; }