UNPKG

3.33 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const chalk_1 = __importDefault(require("chalk"));
7const friendlySyntaxErrorLabel = 'Syntax error:';
8/**
9 * Formats webpack messages into a nicer looking, human friendly format.
10 * Heavily adopted from:
11 * https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/formatWebpackMessages.js
12 * @param message
13 */
14function formatMessage(message) {
15 let lines = message.split('\n');
16 // Remove webpack errors/warnings
17 lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line));
18 // remove extra file path if exists
19 // webpack somehow inserts a relative path into the messages, so we
20 // splice it off here.
21 if (lines.length > 2 && ~lines[0].indexOf(lines[1].slice(2))) {
22 lines.splice(1, 1);
23 }
24 // remove webpack @ stack
25 lines = lines.filter(line => line.indexOf(' @ ') !== 0);
26 // Transform 'Parsing error' to 'Syntax error'
27 lines = lines.map(line => {
28 const parseError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec(line);
29 if (!parseError) {
30 return line;
31 }
32 const [, errorLine, errorColumn, errorMessage] = parseError;
33 return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`;
34 });
35 // Strip leading newlines
36 if (lines.length > 2 && lines[1].trim() === '') {
37 lines.splice(1, 1);
38 }
39 // Cleans up verbose "module not found" messages for files and packages.
40 if (lines[1] && lines[1].indexOf('Module not found: ') === 0) {
41 lines = [
42 lines[0],
43 lines[1]
44 .replace('Error: ', '')
45 .replace('Module not found: Cannot find file:', 'Cannot find file:'),
46 ];
47 }
48 // Underline and bold file name
49 if (lines[0]) {
50 lines[0] = chalk_1.default.underline.bold(lines[0]);
51 }
52 message = lines.join('\n');
53 // Internal stacks are generally useless so we strip them... with the
54 // exception of stacks containing `webpack:` because they're normally
55 // from user code generated by Webpack. For more information see
56 // https://github.com/facebook/create-react-app/pull/1050
57 message = message.replace(/^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm, ''); // at ... ...:x:y
58 message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, ''); // at <anonymous>
59 lines = message.split('\n');
60 // Remove duplicated newlines
61 lines = lines.filter((line, index, arr) => index === 0 || line.trim() !== '' || line.trim() !== arr[index - 1].trim());
62 // Reassemble the message
63 message = lines.join('\n');
64 return message.trim();
65}
66function formatWebpackMessages(json // Webpack's stat output is typed as any
67) {
68 const errors = json.errors
69 .map((message) => formatMessage(message))
70 .filter(message => message !== '');
71 const warnings = json.warnings
72 .map((message) => formatMessage(message))
73 .filter(message => message !== '');
74 return {
75 errors,
76 warnings,
77 };
78}
79exports.formatWebpackMessages = formatWebpackMessages;
80//# sourceMappingURL=formatWebpackMessages.js.map
\No newline at end of file