1 | /**
|
2 | * Copyright (c) 2015-present, Facebook, Inc.
|
3 | *
|
4 | * This source code is licensed under the MIT license found in the
|
5 | * LICENSE file in the root directory of this source tree.
|
6 | */
|
7 |
|
8 | ;
|
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 |
|
13 | // Some custom utilities to prettify Webpack output.
|
14 | // This is quite hacky and hopefully won't be needed when Webpack fixes this.
|
15 | // https://github.com/webpack/webpack/issues/2878
|
16 |
|
17 | var chalk = require('chalk');
|
18 | var friendlySyntaxErrorLabel = 'Syntax error:';
|
19 |
|
20 | function isLikelyASyntaxError(message) {
|
21 | return message.indexOf(friendlySyntaxErrorLabel) !== -1;
|
22 | }
|
23 |
|
24 | // Cleans up webpack error messages.
|
25 | // eslint-disable-next-line no-unused-vars
|
26 | function formatMessage(message, isError) {
|
27 | var lines = message.split('\n');
|
28 |
|
29 | if (lines.length > 2 && lines[1] === '') {
|
30 | // Remove extra newline.
|
31 | lines.splice(1, 1);
|
32 | }
|
33 |
|
34 | // Remove webpack-specific loader notation from filename.
|
35 | // Before:
|
36 | // ./~/css-loader!./~/postcss-loader!./src/App.css
|
37 | // After:
|
38 | // ./src/App.css
|
39 | if (lines[0].lastIndexOf('!') !== -1) {
|
40 | lines[0] = lines[0].substr(lines[0].lastIndexOf('!') + 1);
|
41 | }
|
42 |
|
43 | lines = lines.filter(function(line) {
|
44 | // Webpack adds a list of entry points to warning messages:
|
45 | // @ ./src/index.js
|
46 | // @ multi react-scripts/~/react-dev-utils/webpackHotDevClient.js ...
|
47 | // It is misleading (and unrelated to the warnings) so we clean it up.
|
48 | // It is only useful for syntax errors but we have beautiful frames for them.
|
49 | return line.indexOf(' @ ') !== 0;
|
50 | });
|
51 |
|
52 | // line #0 is filename
|
53 | // line #1 is the main error message
|
54 | if (!lines[0] || !lines[1]) {
|
55 | return lines.join('\n');
|
56 | }
|
57 |
|
58 | // Cleans up verbose "module not found" messages for files and packages.
|
59 | if (lines[1].indexOf('Module not found: ') === 0) {
|
60 | lines = [
|
61 | lines[0],
|
62 | // Clean up message because "Module not found: " is descriptive enough.
|
63 | lines[1]
|
64 | .replace("Cannot resolve 'file' or 'directory' ", '')
|
65 | .replace('Cannot resolve module ', '')
|
66 | .replace('Error: ', '')
|
67 | .replace('[CaseSensitivePathsPlugin] ', ''),
|
68 | ];
|
69 | }
|
70 |
|
71 | // Cleans up syntax error messages.
|
72 | if (lines[1].indexOf('Module build failed: ') === 0) {
|
73 | lines[1] = lines[1].replace(
|
74 | 'Module build failed: SyntaxError:',
|
75 | friendlySyntaxErrorLabel
|
76 | );
|
77 | }
|
78 |
|
79 | // Clean up export errors.
|
80 | // TODO: we should really send a PR to Webpack for this.
|
81 | var exportError = /\s*(.+?)\s*(")?export '(.+?)' was not found in '(.+?)'/;
|
82 | if (lines[1].match(exportError)) {
|
83 | lines[1] = lines[1].replace(
|
84 | exportError,
|
85 | "$1 '$4' does not contain an export named '$3'."
|
86 | );
|
87 | }
|
88 |
|
89 | lines[0] = chalk.inverse(lines[0]);
|
90 |
|
91 | // Reassemble the message.
|
92 | message = lines.join('\n');
|
93 | // Internal stacks are generally useless so we strip them... with the
|
94 | // exception of stacks containing `webpack:` because they're normally
|
95 | // from user code generated by WebPack. For more information see
|
96 | // https://github.com/facebookincubator/create-react-app/pull/1050
|
97 | message = message.replace(
|
98 | /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm,
|
99 | ''
|
100 | ); // at ... ...:x:y
|
101 |
|
102 | return message.trim();
|
103 | }
|
104 |
|
105 | function formatWebpackMessages(json) {
|
106 | var formattedErrors = json.errors.map(function(message) {
|
107 | return formatMessage(message, true);
|
108 | });
|
109 | var formattedWarnings = json.warnings.map(function(message) {
|
110 | return formatMessage(message, false);
|
111 | });
|
112 | var result = {
|
113 | errors: formattedErrors,
|
114 | warnings: formattedWarnings,
|
115 | };
|
116 | if (result.errors.some(isLikelyASyntaxError)) {
|
117 | // If there are any syntax errors, show just them.
|
118 | // This prevents a confusing ESLint parsing error
|
119 | // preceding a much more useful Babel syntax error.
|
120 | result.errors = result.errors.filter(isLikelyASyntaxError);
|
121 | }
|
122 | return result;
|
123 | }
|
124 |
|
125 | module.exports = formatWebpackMessages;
|