|
| 1 | +/** |
| 2 | +MIT License |
| 3 | +Copyright (c) 2015-present, Facebook, Inc. |
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +The above copyright notice and this permission notice shall be included in all |
| 11 | +copies or substantial portions of the Software. |
| 12 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 | +SOFTWARE. |
| 19 | +*/ |
| 20 | + |
| 21 | +import stripAnsi from "../modules/strip-ansi/index.js"; |
| 22 | +// This file is based on https://github.com/facebook/create-react-app/blob/7b1a32be6ec9f99a6c9a3c66813f3ac09c4736b9/packages/react-dev-utils/formatWebpackMessages.js |
| 23 | +// It's been edited to remove chalk and CRA-specific logic |
| 24 | + |
| 25 | +const friendlySyntaxErrorLabel = "Syntax error:"; |
| 26 | + |
| 27 | +// Cleans up webpack error messages. |
| 28 | +function formatWebpackMessage(message, verbose) { |
| 29 | + if (typeof message === "object" && message.message) { |
| 30 | + const filteredModuleTrace = |
| 31 | + message.moduleTrace && |
| 32 | + message.moduleTrace.filter( |
| 33 | + (trace) => |
| 34 | + !/next-(middleware|client-pages|flight-(client|server))-loader\.js/.test( |
| 35 | + trace.originName |
| 36 | + ) |
| 37 | + ); |
| 38 | + message = |
| 39 | + (message.moduleName ? `${stripAnsi(message.moduleName)}\n` : "") + |
| 40 | + (message.file ? `${stripAnsi(message.file)}\n` : "") + |
| 41 | + message.message + |
| 42 | + (message.details && verbose ? `\n${message.details}` : "") + |
| 43 | + (filteredModuleTrace && filteredModuleTrace.length && verbose |
| 44 | + ? `\n\nImport trace for requested module:${filteredModuleTrace |
| 45 | + .map((trace) => `\n${trace.originName}`) |
| 46 | + .join("")}` |
| 47 | + : "") + |
| 48 | + (message.stack && verbose ? `\n${message.stack}` : ""); |
| 49 | + } |
| 50 | + let lines = message.split("\n"); |
| 51 | + |
| 52 | + // Strip Webpack-added headers off errors/warnings |
| 53 | + // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js |
| 54 | + lines = lines.filter((line) => !/Module [A-z ]+\(from/.test(line)); |
| 55 | + |
| 56 | + // Transform parsing error into syntax error |
| 57 | + lines = lines.map((line) => { |
| 58 | + const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec( |
| 59 | + line |
| 60 | + ); |
| 61 | + if (!parsingError) { |
| 62 | + return line; |
| 63 | + } |
| 64 | + const [, errorLine, errorColumn, errorMessage] = parsingError; |
| 65 | + return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`; |
| 66 | + }); |
| 67 | + |
| 68 | + message = lines.join("\n"); |
| 69 | + // Smoosh syntax errors (commonly found in CSS) |
| 70 | + message = message.replace( |
| 71 | + /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g, |
| 72 | + `${friendlySyntaxErrorLabel} $3 ($1:$2)\n` |
| 73 | + ); |
| 74 | + // Clean up export errors |
| 75 | + message = message.replace( |
| 76 | + /^.*export '(.+?)' was not found in '(.+?)'.*$/gm, |
| 77 | + `Attempted import error: '$1' is not exported from '$2'.` |
| 78 | + ); |
| 79 | + message = message.replace( |
| 80 | + /^.*export 'default' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, |
| 81 | + `Attempted import error: '$2' does not contain a default export (imported as '$1').` |
| 82 | + ); |
| 83 | + message = message.replace( |
| 84 | + /^.*export '(.+?)' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, |
| 85 | + `Attempted import error: '$1' is not exported from '$3' (imported as '$2').` |
| 86 | + ); |
| 87 | + lines = message.split("\n"); |
| 88 | + |
| 89 | + // Remove leading newline |
| 90 | + if (lines.length > 2 && lines[1].trim() === "") { |
| 91 | + lines.splice(1, 1); |
| 92 | + } |
| 93 | + |
| 94 | + // Cleans up verbose "module not found" messages for files and packages. |
| 95 | + if (lines[1] && lines[1].indexOf("Module not found: ") === 0) { |
| 96 | + lines = [ |
| 97 | + lines[0], |
| 98 | + lines[1] |
| 99 | + .replace("Error: ", "") |
| 100 | + .replace("Module not found: Cannot find file:", "Cannot find file:"), |
| 101 | + ...lines.slice(2), |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + if (!verbose) { |
| 106 | + message = lines.join("\n"); |
| 107 | + // Internal stacks are generally useless so we strip them... with the |
| 108 | + // exception of stacks containing `webpack:` because they're normally |
| 109 | + // from user code generated by Webpack. For more information see |
| 110 | + // https://github.com/facebook/create-react-app/pull/1050 |
| 111 | + message = message.replace( |
| 112 | + /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm, |
| 113 | + "" |
| 114 | + // eslint-disable-next-line line-comment-position |
| 115 | + ); // at ... ...:x:y |
| 116 | + // eslint-disable-next-line line-comment-position |
| 117 | + message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, ""); // at <anonymous> |
| 118 | + lines = message.split("\n"); |
| 119 | + } |
| 120 | + |
| 121 | + // Remove duplicated newlines |
| 122 | + lines = lines.filter( |
| 123 | + (line, index, arr) => |
| 124 | + index === 0 || line.trim() !== "" || line.trim() !== arr[index - 1].trim() |
| 125 | + ); |
| 126 | + |
| 127 | + // Reassemble the message |
| 128 | + message = lines.join("\n"); |
| 129 | + return message.trim(); |
| 130 | +} |
| 131 | + |
| 132 | +export default formatWebpackMessage; |
0 commit comments