Unverified Commit 9bb8fbf5 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Update webpack message formatter (#6299)

parent 4051ffcb
/** /**
MIT License MIT License
Copyright (c) 2013-present, Facebook, Inc. Copyright (c) 2015-present, Facebook, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
...@@ -21,16 +21,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ...@@ -21,16 +21,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// This file is based on https://github.com/facebook/create-react-app/blob/v1.1.4/packages/react-dev-utils/formatWebpackMessages.js // 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 // It's been edited to remove chalk and CRA-specific logic
'use strict' 'use strict'
// Some custom utilities to prettify Webpack output. const friendlySyntaxErrorLabel = 'Syntax error:'
// This is quite hacky and hopefully won't be needed when Webpack fixes this.
// https://github.com/webpack/webpack/issues/2878
var friendlySyntaxErrorLabel = 'Syntax error:'
function isLikelyASyntaxError (message) { function isLikelyASyntaxError (message) {
return message.indexOf(friendlySyntaxErrorLabel) !== -1 return message.indexOf(friendlySyntaxErrorLabel) !== -1
...@@ -39,112 +35,99 @@ function isLikelyASyntaxError (message) { ...@@ -39,112 +35,99 @@ function isLikelyASyntaxError (message) {
// Cleans up webpack error messages. // Cleans up webpack error messages.
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
function formatMessage (message, isError) { function formatMessage (message, isError) {
var lines = message.split('\n') let lines = message.split('\n')
if (lines.length > 2 && lines[1] === '') { // Strip Webpack-added headers off errors/warnings
// Remove extra newline. // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js
lines.splice(1, 1) lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line))
}
// Remove webpack-specific loader notation from filename. // Transform parsing error into syntax error
// Before: // TODO: move this to our ESLint formatter?
// ./~/css-loader!./~/postcss-loader!./src/App.css lines = lines.map(line => {
// After: const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec(
// ./src/App.css line
if (lines[0].lastIndexOf('!') !== -1) { )
lines[0] = lines[0].substr(lines[0].lastIndexOf('!') + 1) if (!parsingError) {
} return line
// Remove unnecessary stack added by `thread-loader`
var threadLoaderIndex = -1
lines.forEach(function (line, index) {
if (threadLoaderIndex !== -1) {
return
}
if (/thread.loader/i.test(line)) {
threadLoaderIndex = index
} }
const [, errorLine, errorColumn, errorMessage] = parsingError
return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`
}) })
if (threadLoaderIndex !== -1) { message = lines.join('\n')
lines = lines.slice(0, threadLoaderIndex) // Smoosh syntax errors (commonly found in CSS)
} message = message.replace(
/SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g,
lines = lines.filter(function (line) { `${friendlySyntaxErrorLabel} $3 ($1:$2)\n`
// Webpack adds a list of entry points to warning messages: )
// @ ./src/index.js // Remove columns from ESLint formatter output (we added these for more
// @ multi react-scripts/~/react-dev-utils/webpackHotDevClient.js ... // accurate syntax errors)
// It is misleading (and unrelated to the warnings) so we clean it up. message = message.replace(/Line (\d+):\d+:/g, 'Line $1:')
// It is only useful for syntax errors but we have beautiful frames for them. // Clean up export errors
return line.indexOf(' @ ') !== 0 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')
// line #0 is filename // Remove leading newline
// line #1 is the main error message if (lines.length > 2 && lines[1].trim() === '') {
if (!lines[0] || !lines[1]) { lines.splice(1, 1)
return lines.join('\n')
} }
// Clean up file name
lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, '$1')
// Cleans up verbose "module not found" messages for files and packages. // Cleans up verbose "module not found" messages for files and packages.
if (lines[1].indexOf('Module not found: ') === 0) { if (lines[1] && lines[1].indexOf('Module not found: ') === 0) {
lines = [ lines = [
lines[0], lines[0],
// Clean up message because "Module not found: " is descriptive enough.
lines[1] lines[1]
.replace("Cannot resolve 'file' or 'directory' ", '')
.replace('Cannot resolve module ', '')
.replace('Error: ', '') .replace('Error: ', '')
.replace('[CaseSensitivePathsPlugin] ', '') .replace('Module not found: Cannot find file:', 'Cannot find file:')
] ]
} }
// Cleans up syntax error messages.
if (lines[1].indexOf('Module build failed: ') === 0) {
lines[1] = lines[1].replace(
'Module build failed: SyntaxError:',
friendlySyntaxErrorLabel
)
}
// Clean up export errors.
// TODO: we should really send a PR to Webpack for this.
var exportError = /\s*(.+?)\s*(")?export '(.+?)' was not found in '(.+?)'/
if (lines[1].match(exportError)) {
lines[1] = lines[1].replace(
exportError,
"$1 '$4' does not contain an export named '$3'."
)
}
// Reassemble the message.
message = lines.join('\n') message = lines.join('\n')
// Internal stacks are generally useless so we strip them... with the // Internal stacks are generally useless so we strip them... with the
// exception of stacks containing `webpack:` because they're normally // exception of stacks containing `webpack:` because they're normally
// from user code generated by WebPack. For more information see // from user code generated by Webpack. For more information see
// https://github.com/facebook/create-react-app/pull/1050 // https://github.com/facebook/create-react-app/pull/1050
message = message.replace( message = message.replace(
/^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm, /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm,
'' ''
) // at ... ...:x:y ) // at ... ...:x:y
message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, '') // at <anonymous>
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() return message.trim()
} }
function formatWebpackMessages (json) { function formatWebpackMessages (json) {
var formattedErrors = json.errors.map(function (message) { const formattedErrors = json.errors.map(function (message) {
return formatMessage(message, true) return formatMessage(message, true)
}) })
var formattedWarnings = json.warnings.map(function (message) { const formattedWarnings = json.warnings.map(function (message) {
return formatMessage(message, false) return formatMessage(message, false)
}) })
var result = { const result = { errors: formattedErrors, warnings: formattedWarnings }
errors: formattedErrors,
warnings: formattedWarnings
}
if (result.errors.some(isLikelyASyntaxError)) { if (result.errors.some(isLikelyASyntaxError)) {
// If there are any syntax errors, show just them. // If there are any syntax errors, show just them.
// This prevents a confusing ESLint parsing error
// preceding a much more useful Babel syntax error.
result.errors = result.errors.filter(isLikelyASyntaxError) result.errors = result.errors.filter(isLikelyASyntaxError)
} }
return result return result
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment