|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +'use strict'; |
| 9 | + |
| 10 | +const chalk = require('chalk'); |
| 11 | +const friendlySyntaxErrorLabel = 'Syntax error:'; |
| 12 | + |
| 13 | +function isLikelyASyntaxError(message) { |
| 14 | + return message.indexOf(friendlySyntaxErrorLabel) !== -1; |
| 15 | +} |
| 16 | + |
| 17 | +// Cleans up webpack error messages. |
| 18 | +// eslint-disable-next-line no-unused-vars |
| 19 | +function formatMessage(message, isError) { |
| 20 | + // Webpack 5 can emit warning/error objects; normalize to strings. |
| 21 | + if (typeof message !== 'string') { |
| 22 | + if (message && typeof message.message === 'string') { |
| 23 | + message = message.message; |
| 24 | + } else if (message && typeof message.stack === 'string') { |
| 25 | + message = message.stack; |
| 26 | + } else { |
| 27 | + message = String(message); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + let lines = message.split('\n'); |
| 32 | + |
| 33 | + // Strip Webpack-added headers off errors/warnings |
| 34 | + // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js |
| 35 | + lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line)); |
| 36 | + |
| 37 | + // Transform parsing error into syntax error |
| 38 | + // TODO: move this to our ESLint formatter? |
| 39 | + lines = lines.map(line => { |
| 40 | + const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec( |
| 41 | + line |
| 42 | + ); |
| 43 | + if (!parsingError) { |
| 44 | + return line; |
| 45 | + } |
| 46 | + const [, errorLine, errorColumn, errorMessage] = parsingError; |
| 47 | + return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`; |
| 48 | + }); |
| 49 | + |
| 50 | + message = lines.join('\n'); |
| 51 | + // Smoosh syntax errors (commonly found in CSS) |
| 52 | + message = message.replace( |
| 53 | + /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g, |
| 54 | + `${friendlySyntaxErrorLabel} $3 ($1:$2)\n` |
| 55 | + ); |
| 56 | + // Remove columns from ESLint formatter output (we added these for more |
| 57 | + // accurate syntax errors) |
| 58 | + message = message.replace(/Line (\d+):\d+:/g, 'Line $1:'); |
| 59 | + // Clean up export errors |
| 60 | + message = message.replace( |
| 61 | + /^.*export '(.+?)' was not found in '(.+?)'.*$/gm, |
| 62 | + `Attempted import error: '$1' is not exported from '$2'.` |
| 63 | + ); |
| 64 | + message = message.replace( |
| 65 | + /^.*export 'default' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, |
| 66 | + `Attempted import error: '$2' does not contain a default export (imported as '$1').` |
| 67 | + ); |
| 68 | + message = message.replace( |
| 69 | + /^.*export '(.+?)' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, |
| 70 | + `Attempted import error: '$1' is not exported from '$3' (imported as '$2').` |
| 71 | + ); |
| 72 | + lines = message.split('\n'); |
| 73 | + |
| 74 | + // Remove leading newline |
| 75 | + if (lines.length > 2 && lines[1].trim() === '') { |
| 76 | + lines.splice(1, 1); |
| 77 | + } |
| 78 | + // Clean up file name |
| 79 | + lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, '$1'); |
| 80 | + |
| 81 | + // Cleans up verbose "module not found" messages for files and packages. |
| 82 | + if (lines[1] && lines[1].indexOf('Module not found: ') === 0) { |
| 83 | + lines = [ |
| 84 | + lines[0], |
| 85 | + lines[1] |
| 86 | + .replace('Error: ', '') |
| 87 | + .replace('Module not found: Cannot find file:', 'Cannot find file:'), |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + // Add helpful message for users trying to use Sass for the first time |
| 92 | + if (lines[1] && lines[1].match(/Cannot find module.+node-sass/)) { |
| 93 | + lines[1] = 'To import Sass files, you first need to install node-sass.\n'; |
| 94 | + lines[1] += |
| 95 | + 'Run `npm install node-sass` or `yarn add node-sass` inside your workspace.'; |
| 96 | + } |
| 97 | + |
| 98 | + lines[0] = chalk.inverse(lines[0]); |
| 99 | + |
| 100 | + message = lines.join('\n'); |
| 101 | + // Internal stacks are generally useless so we strip them... with the |
| 102 | + // exception of stacks containing `webpack:` because they're normally |
| 103 | + // from user code generated by Webpack. For more information see |
| 104 | + // https://github.com/facebook/create-react-app/pull/1050 |
| 105 | + message = message.replace( |
| 106 | + /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm, |
| 107 | + '' |
| 108 | + ); // at ... ...:x:y |
| 109 | + message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, ''); // at <anonymous> |
| 110 | + lines = message.split('\n'); |
| 111 | + |
| 112 | + // Remove duplicated newlines |
| 113 | + lines = lines.filter( |
| 114 | + (line, index, arr) => |
| 115 | + index === 0 || line.trim() !== '' || line.trim() !== arr[index - 1].trim() |
| 116 | + ); |
| 117 | + |
| 118 | + // Reassemble the message |
| 119 | + message = lines.join('\n'); |
| 120 | + return message.trim(); |
| 121 | +} |
| 122 | + |
| 123 | +function formatWebpackMessages(json) { |
| 124 | + const formattedErrors = json.errors.map(function(message) { |
| 125 | + return formatMessage(message, true); |
| 126 | + }); |
| 127 | + const formattedWarnings = json.warnings.map(function(message) { |
| 128 | + return formatMessage(message, false); |
| 129 | + }); |
| 130 | + const result = { errors: formattedErrors, warnings: formattedWarnings }; |
| 131 | + if (result.errors.some(isLikelyASyntaxError)) { |
| 132 | + // If there are any syntax errors, show just them. |
| 133 | + result.errors = result.errors.filter(isLikelyASyntaxError); |
| 134 | + } |
| 135 | + return result; |
| 136 | +} |
| 137 | + |
| 138 | +module.exports = formatWebpackMessages; |
0 commit comments