1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | const friendlySyntaxErrorLabel = 'Syntax error:';
|
11 |
|
12 | function isLikelyASyntaxError(message) {
|
13 | return message.indexOf(friendlySyntaxErrorLabel) !== -1;
|
14 | }
|
15 |
|
16 |
|
17 | function formatMessage(message) {
|
18 | let lines = message.split('\n');
|
19 |
|
20 |
|
21 |
|
22 | lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line));
|
23 |
|
24 |
|
25 |
|
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 |
|
39 | message = message.replace(
|
40 | /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g,
|
41 | `${friendlySyntaxErrorLabel} $3 ($1:$2)\n`
|
42 | );
|
43 |
|
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 |
|
59 | if (lines.length > 2 && lines[1].trim() === '') {
|
60 | lines.splice(1, 1);
|
61 | }
|
62 |
|
63 | lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, '$1');
|
64 |
|
65 |
|
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 |
|
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 |
|
84 |
|
85 |
|
86 |
|
87 | message = message.replace(
|
88 | /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm,
|
89 | ''
|
90 | );
|
91 | message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, '');
|
92 | lines = message.split('\n');
|
93 |
|
94 |
|
95 | lines = lines.filter(
|
96 | (line, index, arr) =>
|
97 | index === 0 || line.trim() !== '' || line.trim() !== arr[index - 1].trim()
|
98 | );
|
99 |
|
100 |
|
101 | message = lines.join('\n');
|
102 | return message.trim();
|
103 | }
|
104 |
|
105 | function 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 |
|
111 | result.errors = result.errors.filter(isLikelyASyntaxError);
|
112 | }
|
113 | return result;
|
114 | }
|
115 |
|
116 | module.exports = formatWebpackMessages;
|