UNPKG

1.93 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 // Handle legacy and modern webpack shapes.
11 message = typeof message.message !== "undefined" ? message.message : message;
12
13 return message
14 .replace("Module build failed: SyntaxError:", friendlySyntaxErrorLabel)
15 .replace(/Module not found: Error: Cannot resolve 'file' or 'directory'/, "Module not found:")
16 .replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, "")
17 .replace("./~/css-loader!./~/postcss-loader!", "");
18}
19
20function _lineJoin(arr) {
21 return arr.join("\n");
22}
23
24// eslint-disable-next-line max-statements
25function formatOutput(stats) {
26 const output = [];
27 const hasErrors = stats.hasErrors();
28 const hasWarnings = stats.hasWarnings();
29
30 const json = stats.toJson({
31 source: true // Needed for webpack5+
32 });
33 let formattedErrors = json.errors.map(message => `Error in ${_formatMessage(message)}`);
34 const formattedWarnings = json.warnings.map(message => `Warning in ${_formatMessage(message)}`);
35
36 if (hasErrors) {
37 output.push("{red-fg}Failed to compile.{/}");
38 output.push("");
39 if (formattedErrors.some(_isLikelyASyntaxError)) {
40 formattedErrors = formattedErrors.filter(_isLikelyASyntaxError);
41 }
42 formattedErrors.forEach(message => {
43 output.push(message);
44 output.push("");
45 });
46 return _lineJoin(output);
47 }
48
49 if (hasWarnings) {
50 output.push("{yellow-fg}Compiled with warnings.{/yellow-fg}");
51 output.push("");
52 formattedWarnings.forEach(message => {
53 output.push(message);
54 output.push("");
55 });
56
57 return _lineJoin(output);
58 }
59
60 output.push("{green-fg}Compiled successfully!{/}");
61 output.push("");
62
63 return _lineJoin(output);
64}
65
66module.exports = { formatOutput, _formatMessage, _isLikelyASyntaxError, _lineJoin };