UNPKG

3.98 kBJavaScriptView Raw
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'use strict';
9
10const friendlySyntaxErrorLabel = 'Syntax error:';
11
12function isLikelyASyntaxError(message) {
13 return message.indexOf(friendlySyntaxErrorLabel) !== -1;
14}
15
16// Cleans up webpack error messages.
17function formatMessage(message) {
18 let lines = message.split('\n');
19
20 // Strip webpack-added headers off errors/warnings
21 // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js
22 lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line));
23
24 // Transform parsing error into syntax error
25 // TODO: move this to our ESLint formatter?
26 lines = lines.map(line => {
27 const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec(
28 line
29 );
30 if (!parsingError) {
31 return line;
32 }
33 const [, errorLine, errorColumn, errorMessage] = parsingError;
34 return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`;
35 });
36
37 message = lines.join('\n');
38 // Smoosh syntax errors (commonly found in CSS)
39 message = message.replace(
40 /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g,
41 `${friendlySyntaxErrorLabel} $3 ($1:$2)\n`
42 );
43 // Clean up export errors
44 message = message.replace(
45 /^.*export '(.+?)' was not found in '(.+?)'.*$/gm,
46 `Attempted import error: '$1' is not exported from '$2'.`
47 );
48 message = message.replace(
49 /^.*export 'default' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm,
50 `Attempted import error: '$2' does not contain a default export (imported as '$1').`
51 );
52 message = message.replace(
53 /^.*export '(.+?)' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm,
54 `Attempted import error: '$1' is not exported from '$3' (imported as '$2').`
55 );
56 lines = message.split('\n');
57
58 // Remove leading newline
59 if (lines.length > 2 && lines[1].trim() === '') {
60 lines.splice(1, 1);
61 }
62 // Clean up file name
63 lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, '$1');
64
65 // Cleans up verbose "module not found" messages for files and packages.
66 if (lines[1] && lines[1].indexOf('Module not found: ') === 0) {
67 lines = [
68 lines[0],
69 lines[1]
70 .replace('Error: ', '')
71 .replace('Module not found: Cannot find file:', 'Cannot find file:'),
72 ];
73 }
74
75 // Add helpful message for users trying to use Sass for the first time
76 if (lines[1] && lines[1].match(/Cannot find module.+node-sass/)) {
77 lines[1] = 'To import Sass files, you first need to install node-sass.\n';
78 lines[1] +=
79 'Run `npm install node-sass` or `yarn add node-sass` inside your workspace.';
80 }
81
82 message = lines.join('\n');
83 // Internal stacks are generally useless so we strip them... with the
84 // exception of stacks containing `webpack:` because they're normally
85 // from user code generated by webpack. For more information see
86 // https://github.com/facebook/create-react-app/pull/1050
87 message = message.replace(
88 /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm,
89 ''
90 ); // at ... ...:x:y
91 message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, ''); // at <anonymous>
92 lines = message.split('\n');
93
94 // Remove duplicated newlines
95 lines = lines.filter(
96 (line, index, arr) =>
97 index === 0 || line.trim() !== '' || line.trim() !== arr[index - 1].trim()
98 );
99
100 // Reassemble the message
101 message = lines.join('\n');
102 return message.trim();
103}
104
105function formatWebpackMessages(json) {
106 const formattedErrors = json.errors.map(formatMessage);
107 const formattedWarnings = json.warnings.map(formatMessage);
108 const result = { errors: formattedErrors, warnings: formattedWarnings };
109 if (result.errors.some(isLikelyASyntaxError)) {
110 // If there are any syntax errors, show just them.
111 result.errors = result.errors.filter(isLikelyASyntaxError);
112 }
113 return result;
114}
115
116module.exports = formatWebpackMessages;