| 15 | |
| 16 | // Cleans up webpack error messages. |
| 17 | function formatMessage(message) { |
| 18 | let lines = []; |
| 19 | |
| 20 | if (typeof message === 'string') { |
| 21 | lines = message.split('\n'); |
| 22 | } else if ('message' in message) { |
| 23 | lines = message['message'].split('\n'); |
| 24 | } else if (Array.isArray(message)) { |
| 25 | message.forEach(message => { |
| 26 | if ('message' in message) { |
| 27 | lines = message['message'].split('\n'); |
| 28 | } |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | // Strip webpack-added headers off errors/warnings |
| 33 | // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js |
| 34 | lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line)); |
| 35 | |
| 36 | // Transform parsing error into syntax error |
| 37 | // TODO: move this to our ESLint formatter? |
| 38 | lines = lines.map(line => { |
| 39 | const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec( |
| 40 | line |
| 41 | ); |
| 42 | if (!parsingError) { |
| 43 | return line; |
| 44 | } |
| 45 | const [, errorLine, errorColumn, errorMessage] = parsingError; |
| 46 | return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`; |
| 47 | }); |
| 48 | |
| 49 | message = lines.join('\n'); |
| 50 | // Smoosh syntax errors (commonly found in CSS) |
| 51 | message = message.replace( |
| 52 | /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g, |
| 53 | `${friendlySyntaxErrorLabel} $3 ($1:$2)\n` |
| 54 | ); |
| 55 | // Clean up export errors |
| 56 | message = message.replace( |
| 57 | /^.*export '(.+?)' was not found in '(.+?)'.*$/gm, |
| 58 | `Attempted import error: '$1' is not exported from '$2'.` |
| 59 | ); |
| 60 | message = message.replace( |
| 61 | /^.*export 'default' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, |
| 62 | `Attempted import error: '$2' does not contain a default export (imported as '$1').` |
| 63 | ); |
| 64 | message = message.replace( |
| 65 | /^.*export '(.+?)' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, |
| 66 | `Attempted import error: '$1' is not exported from '$3' (imported as '$2').` |
| 67 | ); |
| 68 | lines = message.split('\n'); |
| 69 | |
| 70 | // Remove leading newline |
| 71 | if (lines.length > 2 && lines[1].trim() === '') { |
| 72 | lines.splice(1, 1); |
| 73 | } |
| 74 | // Clean up file name |