UNPKG

1.75 kBJavaScriptView Raw
1"use strict";
2
3const friendlySyntaxErrorLabel = "Syntax error:";
4
5function _isLikelyASyntaxError(message) {
6 return message.indexOf(friendlySyntaxErrorLabel) !== -1;
7}
8
9function _formatMessage(message) {
10 return message
11 .replace("Module build failed: SyntaxError:", friendlySyntaxErrorLabel)
12 .replace(/Module not found: Error: Cannot resolve 'file' or 'directory'/, "Module not found:")
13 .replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, "")
14 .replace("./~/css-loader!./~/postcss-loader!", "");
15}
16
17function _lineJoin(arr) {
18 return arr.join("\n");
19}
20
21// eslint-disable-next-line max-statements
22function formatOutput(stats) {
23 const output = [];
24 const hasErrors = stats.hasErrors();
25 const hasWarnings = stats.hasWarnings();
26
27 const json = stats.toJson();
28 let formattedErrors = json.errors.map(message => `Error in ${_formatMessage(message)}`);
29 const formattedWarnings = json.warnings.map(message => `Warning in ${_formatMessage(message)}`);
30
31 if (hasErrors) {
32 output.push("{red-fg}Failed to compile.{/}");
33 output.push("");
34 if (formattedErrors.some(_isLikelyASyntaxError)) {
35 formattedErrors = formattedErrors.filter(_isLikelyASyntaxError);
36 }
37 formattedErrors.forEach(message => {
38 output.push(message);
39 output.push("");
40 });
41 return _lineJoin(output);
42 }
43
44 if (hasWarnings) {
45 output.push("{yellow-fg}Compiled with warnings.{/yellow-fg}");
46 output.push("");
47 formattedWarnings.forEach(message => {
48 output.push(message);
49 output.push("");
50 });
51
52 return _lineJoin(output);
53 }
54
55 output.push("{green-fg}Compiled successfully!{/}");
56 output.push("");
57
58 return _lineJoin(output);
59}
60
61module.exports = { formatOutput, _formatMessage, _isLikelyASyntaxError, _lineJoin };