/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file at
 * https://github.com/facebook/create-react-app/blob/master/LICENSE
 */ import webpack , { StatsCompilation } from 'webpack' ; const friendlySyntaxErrorLabel = 'Syntax error:' ; function isLikelyASyntaxError ( message ) { return message . includes ( friendlySyntaxErrorLabel ) ; } function formatMessage ( message ) { let lines = [ ] ; if ( typeof message === 'object' ) { message = `${ ( message . moduleName && `${ message . moduleName }\n` ) as string }${ message . details || message . stack || message . message }` ; } lines = message . split ( '\n' ) ; lines = lines . filter ( line => ! /Module [A-z ]+\(from/ . test ( line ) ) ; lines = lines . map ( line => { const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/ . exec ( line ) ; if ( ! parsingError ) { return line ; } const [ , errorLine , errorColumn , errorMessage ] = parsingError ; return `${ friendlySyntaxErrorLabel } ${ errorMessage } (${ errorLine }:${ errorColumn })` ; } ) ; message = lines . join ( '\n' ) ; message = message . replace ( /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g , `${ friendlySyntaxErrorLabel } $3 ($1:$2)\n` ) ; lines = message . split ( '\n' ) ; if ( lines . length > 2 && lines [ 1 ] . trim ( ) === '' ) { lines . splice ( 1 , 1 ) ; } lines [ 0 ] = lines [ 0 ] . replace ( /^(.*) \d+:\d+-\d+$/ , '$1' ) ; if ( lines [ 1 ] ?. startsWith ( 'Module not found: ' ) ) { lines = [ lines [ 0 ] , lines [ 1 ] . replace ( 'Error: ' , '' ) . replace ( 'Module not found: Cannot find file:' , 'Cannot find file:' ) . replace ( '[CaseSensitivePathsPlugin] ' , '' ) . replace ( "Cannot resolve 'file' or 'directory' " , '' ) , ] ; } 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 ( ) ; } function formatWebpackMessages ( json ) { const formattedErrors = json . errors ?. map ( formatMessage ) ; const formattedWarnings = json . warnings ?. map ( formatMessage ) ; const result = { errors : formattedErrors || [ ] , warnings : formattedWarnings || [ ] , } ; if ( result . errors ?. some ( isLikelyASyntaxError ) ) { result . errors = result . errors . filter ( isLikelyASyntaxError ) ; } if ( result . errors . length > 1 ) { result . errors . length = 1 ; } return result ; } export { formatWebpackMessages } ;