"use strict"; var _extends = require("@swc/helpers/lib/_extends.js").default; var _interop_require_default = require("@swc/helpers/lib/_interop_require_default.js").default; var _stripAnsi = _interop_require_default(require("next/dist/compiled/strip-ansi")); // This file is based on https://github.com/facebook/create-react-app/blob/7b1a32be6ec9f99a6c9a3c66813f3ac09c4736b9/packages/react-dev-utils/formatWebpackMessages.js // It's been edited to remove chalk and CRA-specific logic const friendlySyntaxErrorLabel = 'Syntax error:'; const WEBPACK_BREAKING_CHANGE_POLYFILLS = '\n\nBREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.'; function isLikelyASyntaxError(message) { return (0, _stripAnsi).default(message).indexOf(friendlySyntaxErrorLabel) !== -1; } let hadMissingSassError = false; // Cleans up webpack error messages. function formatMessage(message, verbose, importTraceNote) { // TODO: Replace this once webpack 5 is stable if (typeof message === 'object' && message.message) { const filteredModuleTrace = message.moduleTrace && message.moduleTrace.filter((trace)=>!/next-(middleware|client-pages|edge-function)-loader\.js/.test(trace.originName)); let body = message.message; const breakingChangeIndex = body.indexOf(WEBPACK_BREAKING_CHANGE_POLYFILLS); if (breakingChangeIndex >= 0) { body = body.slice(0, breakingChangeIndex); } message = (message.moduleName ? (0, _stripAnsi).default(message.moduleName) + '\n' : '') + (message.file ? (0, _stripAnsi).default(message.file) + '\n' : '') + body + (message.details && verbose ? '\n' + message.details : '') + (filteredModuleTrace && filteredModuleTrace.length && verbose ? (importTraceNote || '\n\nImport trace for requested module:') + filteredModuleTrace.map((trace)=>`\n${trace.moduleName}`).join('') : '') + (message.stack && verbose ? '\n' + message.stack : ''); } let lines = message.split('\n'); // Strip Webpack-added headers off errors/warnings // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js lines = lines.filter((line)=>!/Module [A-z ]+\(from/.test(line)); // Transform parsing error into syntax error // TODO: move this to our ESLint formatter? 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'); // Smoosh syntax errors (commonly found in CSS) message = message.replace(/SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g, `${friendlySyntaxErrorLabel} $3 ($1:$2)\n`); // Clean up export errors message = message.replace(/^.*export '(.+?)' was not found in '(.+?)'.*$/gm, `Attempted import error: '$1' is not exported from '$2'.`); message = message.replace(/^.*export 'default' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, `Attempted import error: '$2' does not contain a default export (imported as '$1').`); message = message.replace(/^.*export '(.+?)' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm, `Attempted import error: '$1' is not exported from '$3' (imported as '$2').`); lines = message.split('\n'); // Remove leading newline if (lines.length > 2 && lines[1].trim() === '') { lines.splice(1, 1); } // Cleans up verbose "module not found" messages for files and packages. if (lines[1] && lines[1].indexOf('Module not found: ') === 0) { lines = [ lines[0], lines[1].replace('Error: ', '').replace('Module not found: Cannot find file:', 'Cannot find file:'), ...lines.slice(2), ]; } // Add helpful message for users trying to use Sass for the first time if (lines[1] && lines[1].match(/Cannot find module.+sass/)) { // ./file.module.scss (<>) => ./file.module.scss const firstLine = lines[0].split('!'); lines[0] = firstLine[firstLine.length - 1]; lines[1] = "To use Next.js' built-in Sass support, you first need to install `sass`.\n"; lines[1] += 'Run `npm i sass` or `yarn add sass` inside your workspace.\n'; lines[1] += '\nLearn more: https://nextjs.org/docs/messages/install-sass'; // dispose of unhelpful stack trace lines = lines.slice(0, 2); hadMissingSassError = true; } else if (hadMissingSassError && message.match(/(sass-loader|resolve-url-loader: CSS error)/)) { // dispose of unhelpful stack trace following missing sass module lines = []; } if (!verbose) { message = lines.join('\n'); // Internal stacks are generally useless so we strip them... with the // exception of stacks containing `webpack:` because they're normally // from user code generated by Webpack. For more information see // https://github.com/facebook/create-react-app/pull/1050 message = message.replace(/^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm, '') // at ... ...:x:y ; message = message.replace(/^\s*at\s(\n|$)/gm, '') // at ; lines = message.split('\n'); } // Remove duplicated newlines lines = lines.filter((line, index, arr)=>index === 0 || line.trim() !== '' || line.trim() !== arr[index - 1].trim()); // Reassemble the message message = lines.join('\n'); return message.trim(); } function formatWebpackMessages(json, verbose) { const formattedErrors = json.errors.map(function(message) { let importTraceNote; // TODO: Shall we use invisible characters in the original error // message as meta information? if (message && message.message && /NEXT_RSC_ERR_/.test(message.message)) { // Comes from the "React Server Components" transform in SWC, always // attach the module trace. const NEXT_RSC_ERR_REACT_API = RegExp(".+NEXT_RSC_ERR_REACT_API: (.*?)\\n", "s"); const NEXT_RSC_ERR_SERVER_IMPORT = RegExp(".+NEXT_RSC_ERR_SERVER_IMPORT: (.*?)\\n", "s"); const NEXT_RSC_ERR_CLIENT_IMPORT = RegExp(".+NEXT_RSC_ERR_CLIENT_IMPORT: (.*?)\\n", "s"); if (NEXT_RSC_ERR_REACT_API.test(message.message)) { message.message = message.message.replace(NEXT_RSC_ERR_REACT_API, `\n\nYou're importing a component that needs $1. It only works in a Client Component but none of its parents are marked with "client", so they're Server Components by default.\n\n`); importTraceNote = '\n\nMaybe one of these should be marked as a "client" entry:\n'; } else if (NEXT_RSC_ERR_SERVER_IMPORT.test(message.message)) { message.message = message.message.replace(NEXT_RSC_ERR_SERVER_IMPORT, `\n\nYou're importing a component that imports $1. It only works in a Client Component but none of its parents are marked with "client", so they're Server Components by default.\n\n`); importTraceNote = '\n\nMaybe one of these should be marked as a "client" entry:\n'; } else if (NEXT_RSC_ERR_CLIENT_IMPORT.test(message.message)) { message.message = message.message.replace(NEXT_RSC_ERR_CLIENT_IMPORT, `\n\nYou're importing a component that needs $1. That only works in a Server Component but one of its parents is marked with "client", so it's a Client Component.\n\n`); importTraceNote = '\n\nOne of these is marked as a "client" entry:\n'; } verbose = true; } return formatMessage(message, verbose, importTraceNote); }); const formattedWarnings = json.warnings.map(function(message) { return formatMessage(message, verbose); }); const result = _extends({}, json, { errors: formattedErrors, warnings: formattedWarnings }); if (!verbose && result.errors.some(isLikelyASyntaxError)) { // If there are any syntax errors, show just them. result.errors = result.errors.filter(isLikelyASyntaxError); result.warnings = []; } return result; } module.exports = formatWebpackMessages; if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') { Object.defineProperty(exports.default, '__esModule', { value: true }); Object.assign(exports.default, exports); module.exports = exports.default; } //# sourceMappingURL=format-webpack-messages.js.map