webpack-dashboard/utils/format-output.js
Ryan Roemer b74c883042
Chore: webpack5 support. (#319)
- Add webpack5 support. Fixes #316 
- Bugfix: Webpack 5 warning message conflict. Fixes #314
- Fix bugs in updated `commander` integration.
2021-01-21 11:17:42 -08:00

67 lines
1.9 KiB
JavaScript

"use strict";
const friendlySyntaxErrorLabel = "Syntax error:";
function _isLikelyASyntaxError(message) {
return message.indexOf(friendlySyntaxErrorLabel) !== -1;
}
function _formatMessage(message = "") {
// Handle legacy and modern webpack shapes.
message = typeof message.message !== "undefined" ? message.message : message;
return message
.replace("Module build failed: SyntaxError:", friendlySyntaxErrorLabel)
.replace(/Module not found: Error: Cannot resolve 'file' or 'directory'/, "Module not found:")
.replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, "")
.replace("./~/css-loader!./~/postcss-loader!", "");
}
function _lineJoin(arr) {
return arr.join("\n");
}
// eslint-disable-next-line max-statements
function formatOutput(stats) {
const output = [];
const hasErrors = stats.hasErrors();
const hasWarnings = stats.hasWarnings();
const json = stats.toJson({
source: true // Needed for webpack5+
});
let formattedErrors = json.errors.map(message => `Error in ${_formatMessage(message)}`);
const formattedWarnings = json.warnings.map(message => `Warning in ${_formatMessage(message)}`);
if (hasErrors) {
output.push("{red-fg}Failed to compile.{/}");
output.push("");
if (formattedErrors.some(_isLikelyASyntaxError)) {
formattedErrors = formattedErrors.filter(_isLikelyASyntaxError);
}
formattedErrors.forEach(message => {
output.push(message);
output.push("");
});
return _lineJoin(output);
}
if (hasWarnings) {
output.push("{yellow-fg}Compiled with warnings.{/yellow-fg}");
output.push("");
formattedWarnings.forEach(message => {
output.push(message);
output.push("");
});
return _lineJoin(output);
}
output.push("{green-fg}Compiled successfully!{/}");
output.push("");
return _lineJoin(output);
}
module.exports = { formatOutput, _formatMessage, _isLikelyASyntaxError, _lineJoin };