This repository was archived by the owner on Jul 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathfriendly-errors-plugin.js
More file actions
188 lines (150 loc) · 5.2 KB
/
Copy pathfriendly-errors-plugin.js
File metadata and controls
188 lines (150 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict';
const path = require('path');
const chalk = require('chalk');
const os = require('os');
const transformErrors = require('./core/transformErrors');
const formatErrors = require('./core/formatErrors');
const output = require('./output');
const utils = require('./utils');
const concat = utils.concat;
const uniqueBy = utils.uniqueBy;
const defaultTransformers = [
require('./transformers/babelSyntax'),
require('./transformers/moduleNotFound'),
require('./transformers/esLintError'),
];
const defaultFormatters = [
require('./formatters/moduleNotFound'),
require('./formatters/eslintError'),
require('./formatters/defaultError'),
];
const defaultOutputOptions = {
dateString: {
color: 'blue',
},
took: {
// can be "ms" or "s"
on: 'ms',
// greater than 5000 ms, it will be displayed on red
// (!) if took.on === 's' then took.red must be on seconds too
red: 5000,
},
displayLastCompile: true,
displayTook: true,
};
class FriendlyErrorsWebpackPlugin {
constructor(options) {
options = options || {};
this.compilationSuccessInfo = options.compilationSuccessInfo || {};
this.onErrors = options.onErrors;
this.shouldClearConsole = options.clearConsole == null ? true : Boolean(options.clearConsole);
this.formatters = concat(defaultFormatters, options.additionalFormatters);
this.transformers = concat(defaultTransformers, options.additionalTransformers);
this.output = Object.assign({}, defaultOutputOptions, options.output);
}
apply(compiler) {
compiler.plugin('done', stats => {
this.clearConsole();
const hasErrors = stats.hasErrors();
const hasWarnings = stats.hasWarnings();
if (!hasErrors && !hasWarnings) {
this.displaySuccess(stats);
return;
}
if (hasErrors) {
this.displayErrors(extractErrorsFromStats(stats, 'errors'), 'error');
return;
}
if (hasWarnings) {
this.displayErrors(extractErrorsFromStats(stats, 'warnings'), 'warning');
}
output.log();
if(this.output.displayLastCompile) {
this.displayLastCompile(this.output.dateString.color);
}
if(this.output.displayTook){
this.displayTook(stats, this.output.took);
}
output.log();
});
compiler.plugin('invalid', () => {
this.clearConsole();
output.title('info', 'WAIT', 'Compiling...');
});
}
clearConsole() {
if (this.shouldClearConsole) {
output.clearConsole();
}
}
displaySuccess(stats) {
const time = getCompileTime(stats);
output.title('success', 'DONE', 'Compiled successfully in ' + time + 'ms');
if (this.compilationSuccessInfo.messages) {
this.compilationSuccessInfo.messages.forEach(message => output.info(message));
}
if (this.compilationSuccessInfo.notes) {
output.log();
this.compilationSuccessInfo.notes.forEach(note => output.note(note));
}
}
displayErrors(errors, severity) {
const processedErrors = transformErrors(errors, this.transformers);
const topErrors = getMaxSeverityErrors(processedErrors);
const nbErrors = topErrors.length;
const subtitle = severity === 'error' ?
`Failed to compile with ${nbErrors} ${severity}s` :
`Compiled with ${nbErrors} ${severity}s`;
output.title(severity, severity.toUpperCase(), subtitle);
if (this.onErrors) {
this.onErrors(severity, topErrors);
}
formatErrors(topErrors, this.formatters, severity)
.forEach(chunk => output.log(chunk));
}
displayLastCompile(dateStringColor) {
const date = new Date();
const lastCompile = chalk[dateStringColor](date.toLocaleDateString()) + " at " + chalk[dateStringColor](date.toLocaleTimeString());
output.log("Last compile: " + lastCompile);
}
displayTook(stats, tookOptions) {
let time = getCompileTime(stats);
if (tookOptions.on === 's') {
time = time / 1000;
}
const took = (time < tookOptions.red) ? chalk.green(time + tookOptions.on) : chalk.red(time + tookOptions.on);
output.log("Took: " + took);
}
}
function extractErrorsFromStats(stats, type) {
if (isMultiStats(stats)) {
const errors = stats.stats
.reduce((errors, stats) => errors.concat(extractErrorsFromStats(stats, type)), []);
// Dedupe to avoid showing the same error many times when multiple
// compilers depend on the same module.
return uniqueBy(errors, error => error.message);
}
return stats.compilation[type];
}
function getCompileTime(stats) {
if (isMultiStats(stats)) {
// Webpack multi compilations run in parallel so using the longest duration.
// https://webpack.github.io/docs/configuration.html#multiple-configurations
return stats.stats
.reduce((time, stats) => Math.max(time, getCompileTime(stats)), 0);
}
return stats.endTime - stats.startTime;
}
function isMultiStats(stats) {
return stats.stats;
}
function getMaxSeverityErrors(errors) {
const maxSeverity = getMaxInt(errors, 'severity');
return errors.filter(e => e.severity === maxSeverity);
}
function getMaxInt(collection, propertyName) {
return collection.reduce((res, curr) => {
return curr[propertyName] > res ? curr[propertyName] : res;
}, 0)
}
module.exports = FriendlyErrorsWebpackPlugin;