1 |
|
2 | var friendlySyntaxErrorLabel = 'Syntax error:';
|
3 |
|
4 | function isLikelyASyntaxError(message) {
|
5 | return message.indexOf(friendlySyntaxErrorLabel) !== -1;
|
6 | }
|
7 |
|
8 | // Cleans up webpack error messages.
|
9 | function formatMessage(message) {
|
10 | var lines = message.split('\n');
|
11 |
|
12 | // Remove webpack-specific loader notation from filename.
|
13 | // Before:
|
14 | // ./~/css-loader!./~/postcss-loader!./src/App.css
|
15 | // After:
|
16 | // ./src/App.css
|
17 | if (lines[0].lastIndexOf('!') !== -1) {
|
18 | lines[0] = lines[0].substr(lines[0].lastIndexOf('!') + 1);
|
19 | }
|
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].replace(
|
33 | 'Cannot resolve \'file\' or \'directory\' ', ''
|
34 | ).replace(
|
35 | 'Cannot resolve module ', ''
|
36 | ).replace(
|
37 | 'Error: ', ''
|
38 | ),
|
39 | // Skip all irrelevant lines.
|
40 | // (For some reason they only appear on the client in browser.)
|
41 | '',
|
42 | lines[lines.length - 1] // error location is the last line
|
43 | ]
|
44 | }
|
45 |
|
46 | // Cleans up syntax error messages.
|
47 | if (lines[1].indexOf('Module build failed: ') === 0) {
|
48 | // For some reason, on the client messages appear duplicated:
|
49 | // https://github.com/webpack/webpack/issues/3008
|
50 | // This won't happen in Node but since we share this helpers,
|
51 | // we will dedupe them right here. We will ignore all lines
|
52 | // after the original error message text is repeated the second time.
|
53 | var errorText = lines[1].substr('Module build failed: '.length);
|
54 | var cleanedLines = [];
|
55 | var hasReachedDuplicateMessage = false;
|
56 | // Gather lines until we reach the beginning of duplicate message.
|
57 | lines.forEach(function (line, index) {
|
58 | if (
|
59 | // First time it occurs is fine.
|
60 | index !== 1 &&
|
61 | // line.endsWith(errorText)
|
62 | line.length >= errorText.length &&
|
63 | line.indexOf(errorText) === line.length - errorText.length
|
64 | ) {
|
65 | // We see the same error message for the second time!
|
66 | // Filter out repeated error message and everything after it.
|
67 | hasReachedDuplicateMessage = true;
|
68 | }
|
69 | if (
|
70 | !hasReachedDuplicateMessage ||
|
71 | // Print last line anyway because it contains the source location
|
72 | index === lines.length - 1
|
73 | ) {
|
74 | // This line is OK to appear in the output.
|
75 | cleanedLines.push(line);
|
76 | }
|
77 | });
|
78 | // We are clean now!
|
79 | lines = cleanedLines;
|
80 | // Finally, brush up the error message a little.
|
81 | lines[1] = lines[1].replace(
|
82 | 'Module build failed: SyntaxError:',
|
83 | friendlySyntaxErrorLabel
|
84 | );
|
85 | }
|
86 |
|
87 | // Reassemble the message.
|
88 | message = lines.join('\n');
|
89 | // Internal stacks are generally useless so we strip them... with the
|
90 | // exception of stacks containing `webpack:` because they're normally
|
91 | // from user code generated by WebPack. For more information see
|
92 | // https://github.com/facebookincubator/create-react-app/pull/1050
|
93 | message = message.replace(
|
94 | /^\s*at\s((?!webpack:).)*:\d+:\d+[\s\)]*(\n|$)/gm, ''
|
95 | ); // at ... ...:x:y
|
96 |
|
97 | return message;
|
98 | }
|
99 |
|
100 | function formatWebpackMessages(json) {
|
101 | var formattedErrors = json.errors.map(function (message) {
|
102 | return 'Error in ' + formatMessage(message)
|
103 | });
|
104 | var formattedWarnings = json.warnings.map(function (message) {
|
105 | return 'Warning in ' + formatMessage(message)
|
106 | });
|
107 | var result = {
|
108 | errors: formattedErrors,
|
109 | warnings: formattedWarnings
|
110 | };
|
111 | if (result.errors.some(isLikelyASyntaxError)) {
|
112 | // If there are any syntax errors, show just them.
|
113 | // This prevents a confusing ESLint parsing error
|
114 | // preceding a much more useful Babel syntax error.
|
115 | result.errors = result.errors.filter(isLikelyASyntaxError);
|
116 | }
|
117 | return result;
|
118 | }
|
119 |
|
120 | module.exports = formatWebpackMessages;
|