|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 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 | +// TODO Legacy CRA react-dev-utils package code |
| 9 | +// This code was in CRA/react-dev-utils (deprecated in 2025) |
| 10 | +// We just copied the code as-is to remove a fat/useless dependency subtree |
| 11 | +// See https://github.com/facebook/docusaurus/pull/10956 |
| 12 | +// See https://github.com/facebook/create-react-app/blob/main/packages/react-dev-utils/formatWebpackMessages.js |
| 13 | + |
| 14 | +/* eslint-disable */ |
| 15 | + |
| 16 | +const friendlySyntaxErrorLabel = 'Syntax error:'; |
| 17 | + |
| 18 | +function isLikelyASyntaxError(message) { |
| 19 | + return message.indexOf(friendlySyntaxErrorLabel) !== -1; |
| 20 | +} |
| 21 | + |
| 22 | +// Cleans up webpack error messages. |
| 23 | +function formatMessage(message) { |
| 24 | + let lines = []; |
| 25 | + |
| 26 | + if (typeof message === 'string') { |
| 27 | + lines = message.split('\n'); |
| 28 | + } else if ('message' in message) { |
| 29 | + lines = message['message'].split('\n'); |
| 30 | + } else if (Array.isArray(message)) { |
| 31 | + message.forEach((message) => { |
| 32 | + if ('message' in message) { |
| 33 | + lines = message['message'].split('\n'); |
| 34 | + } |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + // Strip webpack-added headers off errors/warnings |
| 39 | + // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js |
| 40 | + lines = lines.filter((line) => !/Module [A-z ]+\(from/.test(line)); |
| 41 | + |
| 42 | + // Transform parsing error into syntax error |
| 43 | + // TODO: move this to our ESLint formatter? |
| 44 | + lines = lines.map((line) => { |
| 45 | + const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec( |
| 46 | + line, |
| 47 | + ); |
| 48 | + if (!parsingError) { |
| 49 | + return line; |
| 50 | + } |
| 51 | + const [, errorLine, errorColumn, errorMessage] = parsingError; |
| 52 | + return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`; |
| 53 | + }); |
| 54 | + |
| 55 | + message = lines.join('\n'); |
| 56 | + // Smoosh syntax errors (commonly found in CSS) |
| 57 | + message = message.replace( |
| 58 | + /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g, |
| 59 | + `${friendlySyntaxErrorLabel} $3 ($1:$2)\n`, |
| 60 | + ); |
| 61 | + // Clean up export errors |
| 62 | + message = message.replace( |
| 63 | + /^.*export '(.+?)' was not found in '(.+?)'.*$/gm, |
| 64 | + `Attempted import error: '$1' is not exported from '$2'.`, |
| 65 | + ); |
| 66 | + message = message.replace( |
| 67 | + /^.*export 'default' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, |
| 68 | + `Attempted import error: '$2' does not contain a default export (imported as '$1').`, |
| 69 | + ); |
| 70 | + message = message.replace( |
| 71 | + /^.*export '(.+?)' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, |
| 72 | + `Attempted import error: '$1' is not exported from '$3' (imported as '$2').`, |
| 73 | + ); |
| 74 | + lines = message.split('\n'); |
| 75 | + |
| 76 | + // Remove leading newline |
| 77 | + if (lines.length > 2 && lines[1].trim() === '') { |
| 78 | + lines.splice(1, 1); |
| 79 | + } |
| 80 | + // Clean up file name |
| 81 | + lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, '$1'); |
| 82 | + |
| 83 | + // Cleans up verbose "module not found" messages for files and packages. |
| 84 | + if (lines[1] && lines[1].indexOf('Module not found: ') === 0) { |
| 85 | + lines = [ |
| 86 | + lines[0], |
| 87 | + lines[1] |
| 88 | + .replace('Error: ', '') |
| 89 | + .replace('Module not found: Cannot find file:', 'Cannot find file:'), |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + // Add helpful message for users trying to use Sass for the first time |
| 94 | + if (lines[1] && lines[1].match(/Cannot find module.+sass/)) { |
| 95 | + lines[1] = 'To import Sass files, you first need to install sass.\n'; |
| 96 | + lines[1] += |
| 97 | + 'Run `npm install sass` or `yarn add sass` inside your workspace.'; |
| 98 | + } |
| 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 || |
| 116 | + line.trim() !== '' || |
| 117 | + line.trim() !== arr[index - 1].trim(), |
| 118 | + ); |
| 119 | + |
| 120 | + // Reassemble the message |
| 121 | + message = lines.join('\n'); |
| 122 | + return message.trim(); |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * @param {import("webpack").Stats.ToJsonOutput} json. |
| 127 | + * @returns {{ errors: string[], warnings: string[] }} |
| 128 | + */ |
| 129 | +module.exports = function formatWebpackMessages(json) { |
| 130 | + const formattedErrors = json.errors.map(formatMessage); |
| 131 | + const formattedWarnings = json.warnings.map(formatMessage); |
| 132 | + const result = {errors: formattedErrors, warnings: formattedWarnings}; |
| 133 | + if (result.errors.some(isLikelyASyntaxError)) { |
| 134 | + // If there are any syntax errors, show just them. |
| 135 | + result.errors = result.errors.filter(isLikelyASyntaxError); |
| 136 | + } |
| 137 | + return result; |
| 138 | +}; |
0 commit comments