UNPKG

2.17 kBJavaScriptView Raw
1/* eslint-disable no-param-reassign */
2module.exports = message => {
3 let lines = message.split('\n')
4
5 // Remove webpack-specific loader notation from filename.
6 // Before:
7 // ./~/css-loader!./~/postcss-loader!./src/App.css
8 // After:
9 // ./src/App.css
10 if (lines[0].lastIndexOf('!') !== -1) {
11 lines[0] = lines[0].substr(lines[0].lastIndexOf('!') + 1)
12 }
13
14 // Webpack adds a list of entry points to warning messages:
15 // @ ./src/index.js
16 // @ multi react-scripts/~/react-dev-utils/webpackHotDevClient.js ...
17 // It is misleading (and unrelated to the warnings) so we clean it up.
18 // It is only useful for syntax errors but we have beautiful frames for them.
19 lines = lines.filter(line => line.indexOf(' @ ') !== 0)
20
21 // line #0 is filename
22 // line #1 is the main error message
23 if (!lines[0] || !lines[1]) {
24 return lines.join('\n')
25 }
26
27 // Cleans up verbose "module not found" messages for files and packages.
28 if (lines[1].indexOf('Module not found: ') === 0) {
29 lines = [
30 lines[0],
31 // Clean up message because "Module not found: " is descriptive enough.
32 lines[1]
33 .replace("Cannot resolve 'file' or 'directory' ", '')
34 .replace('Cannot resolve module ', '')
35 .replace('Error: ', '')
36 ]
37 }
38
39 // Cleans up syntax error messages.
40 if (lines[1].indexOf('Module build failed: ') === 0) {
41 lines[1] = lines[1].replace('Module build failed: SyntaxError:', 'Syntax error:')
42 }
43
44 // Clean up export errors.
45 // TODO: we should really send a PR to Webpack for this.
46 const exportError = /\s*(.+?)\s*(")?export '(.+?)' was not found in '(.+?)'/
47 if (lines[1].match(exportError)) {
48 lines[1] = lines[1].replace(
49 exportError,
50 "$1 '$4' does not contain an export named '$3'."
51 )
52 }
53
54 // Reassemble the message.
55 message = lines.join('\n')
56 // Internal stacks are generally useless so we strip them... with the
57 // exception of stacks containing `webpack:` because they're normally
58 // from user code generated by WebPack. For more information see
59 // https://github.com/facebookincubator/create-react-app/pull/1050
60 message = message.replace(/^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm, '') // at ... ...:x:y
61
62 return message.trim()
63}