|
7 | 7 | * of patent rights can be found in the PATENTS file in the same directory.
|
8 | 8 | */
|
9 | 9 |
|
| 10 | +// WARNING: this code is untranspiled and is used in browser too. |
| 11 | +// Please make sure any changes are in ES5 or contribute a Babel compile step. |
| 12 | + |
10 | 13 | // Some custom utilities to prettify Webpack output.
|
11 |
| -// This is a little hacky. |
12 |
| -// It would be easier if webpack provided a rich error object. |
| 14 | +// This is quite hacky and hopefully won't be needed when Webpack fixes this. |
| 15 | +// https://github.com/webpack/webpack/issues/2878 |
| 16 | + |
13 | 17 | var friendlySyntaxErrorLabel = 'Syntax error:';
|
| 18 | + |
14 | 19 | function isLikelyASyntaxError(message) {
|
15 | 20 | return message.indexOf(friendlySyntaxErrorLabel) !== -1;
|
16 | 21 | }
|
| 22 | + |
| 23 | +// Cleans up webpack error messages. |
17 | 24 | function formatMessage(message) {
|
18 |
| - return message |
19 |
| - // Make some common errors shorter: |
20 |
| - .replace( |
21 |
| - // Babel syntax error |
| 25 | + var lines = message.split('\n'); |
| 26 | + |
| 27 | + // line #0 is filename |
| 28 | + // line #1 is the main error message |
| 29 | + if (!lines[0] || !lines[1]) { |
| 30 | + return message; |
| 31 | + } |
| 32 | + |
| 33 | + // Remove webpack-specific loader notation from filename. |
| 34 | + // Before: |
| 35 | + // ./~/css-loader!./~/postcss-loader!./src/App.css |
| 36 | + // After: |
| 37 | + // ./src/App.css |
| 38 | + if (lines[0].lastIndexOf('!') !== -1) { |
| 39 | + lines[0] = lines[0].substr(lines[0].lastIndexOf('!') + 1); |
| 40 | + } |
| 41 | + |
| 42 | + // Cleans up verbose "module not found" messages for files and packages. |
| 43 | + if (lines[1].indexOf('Module not found: ') === 0) { |
| 44 | + lines = [ |
| 45 | + lines[0], |
| 46 | + // Clean up message because "Module not found: " is descriptive enough. |
| 47 | + lines[1].replace( |
| 48 | + 'Cannot resolve \'file\' or \'directory\' ', '' |
| 49 | + ).replace( |
| 50 | + 'Cannot resolve module ', '' |
| 51 | + ).replace( |
| 52 | + 'Error: ', '' |
| 53 | + ), |
| 54 | + // Skip all irrelevant lines. |
| 55 | + // (For some reason they only appear on the client in browser.) |
| 56 | + '', |
| 57 | + lines[lines.length - 1] // error location is the last line |
| 58 | + ] |
| 59 | + } |
| 60 | + |
| 61 | + // Cleans up syntax error messages. |
| 62 | + if (lines[1].indexOf('Module build failed: ') === 0) { |
| 63 | + // For some reason, on the client messages appear duplicated: |
| 64 | + // https://github.com/webpack/webpack/issues/3008 |
| 65 | + // This won't happen in Node but since we share this helpers, |
| 66 | + // we will dedupe them right here. We will ignore all lines |
| 67 | + // after the original error message text is repeated the second time. |
| 68 | + var errorText = lines[1].substr('Module build failed: '.length); |
| 69 | + var cleanedLines = []; |
| 70 | + var hasReachedDuplicateMessage = false; |
| 71 | + // Gather lines until we reach the beginning of duplicate message. |
| 72 | + lines.forEach(function(line, index) { |
| 73 | + if ( |
| 74 | + // First time it occurs is fine. |
| 75 | + index !== 1 && |
| 76 | + // line.endsWith(errorText) |
| 77 | + line.length >= errorText.length && |
| 78 | + line.indexOf(errorText) === line.length - errorText.length |
| 79 | + ) { |
| 80 | + // We see the same error message for the second time! |
| 81 | + // Filter out repeated error message and everything after it. |
| 82 | + hasReachedDuplicateMessage = true; |
| 83 | + } |
| 84 | + if ( |
| 85 | + !hasReachedDuplicateMessage || |
| 86 | + // Print last line anyway because it contains the source location |
| 87 | + index === lines.length - 1 |
| 88 | + ) { |
| 89 | + // This line is OK to appear in the output. |
| 90 | + cleanedLines.push(line); |
| 91 | + } |
| 92 | + }); |
| 93 | + // We are clean now! |
| 94 | + lines = cleanedLines; |
| 95 | + // Finally, brush up the error message a little. |
| 96 | + lines[1] = lines[1].replace( |
22 | 97 | 'Module build failed: SyntaxError:',
|
23 | 98 | friendlySyntaxErrorLabel
|
24 |
| - ) |
25 |
| - .replace( |
26 |
| - // Webpack file not found error |
27 |
| - /Module not found: Error: Cannot resolve 'file' or 'directory'/, |
28 |
| - 'Module not found:' |
29 |
| - ) |
30 |
| - // Internal stacks are generally useless so we strip them |
31 |
| - .replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, '') // at ... ...:x:y |
32 |
| - // Webpack loader names obscure CSS filenames |
33 |
| - .replace('./~/css-loader!./~/postcss-loader!', ''); |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + // Reassemble the message. |
| 103 | + message = lines.join('\n'); |
| 104 | + // Internal stacks are generally useless so we strip them |
| 105 | + message = message.replace( |
| 106 | + /^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, '' |
| 107 | + ); // at ... ...:x:y |
| 108 | + |
| 109 | + return message; |
34 | 110 | }
|
35 | 111 |
|
36 |
| -function formatWebpackMessages(stats) { |
37 |
| - var hasErrors = stats.hasErrors(); |
38 |
| - var hasWarnings = stats.hasWarnings(); |
39 |
| - if (!hasErrors && !hasWarnings) { |
40 |
| - return { |
41 |
| - errors: [], |
42 |
| - warnings: [] |
43 |
| - }; |
44 |
| - } |
45 |
| - // We use stats.toJson({}, true) to make output more compact and readable: |
46 |
| - // https://github.com/facebookincubator/create-react-app/issues/401#issuecomment-238291901 |
47 |
| - var json = stats.toJson({}, true); |
48 |
| - var formattedErrors = json.errors.map(message => |
49 |
| - 'Error in ' + formatMessage(message) |
50 |
| - ); |
51 |
| - var formattedWarnings = json.warnings.map(message => |
52 |
| - 'Warning in ' + formatMessage(message) |
53 |
| - ); |
| 112 | +function formatWebpackMessages(json) { |
| 113 | + var formattedErrors = json.errors.map(function(message) { |
| 114 | + return 'Error in ' + formatMessage(message) |
| 115 | + }); |
| 116 | + var formattedWarnings = json.warnings.map(function(message) { |
| 117 | + return 'Warning in ' + formatMessage(message) |
| 118 | + }); |
54 | 119 | var result = {
|
55 | 120 | errors: formattedErrors,
|
56 | 121 | warnings: formattedWarnings
|
|
0 commit comments