/
index.js
206 lines (166 loc) · 5.13 KB
/
index.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
const PrettyError = require('pretty-error');
const chalk = require('chalk');
const indentString = require('indent-string');
const merge = require('lodash.merge');
const checkHumanErrors = require('./util/checkHumanErrors');
// Symbols table
// On windows we hav to use more simple symbols otherwise they won't display correctly
const symbols = process.platform !== 'win32' ?
{
ok: '✓',
fail: '✖',
bullet: '•',
hr: '━',
} :
/* istanbul ignore next */
{
ok: '√',
fail: '×',
bullet: '*',
hr: '-',
};
// Configure prettyError instance
const prettyError = new PrettyError();
prettyError.appendStyle({
'pretty-error > header': {
display: 'none',
},
'pretty-error > trace': {
marginTop: 0,
},
'pretty-error > trace > item': {
marginBottom: 0,
},
});
function calculateDuration(clientStats, serverStats, beginAt) {
const leniantBeginAt = beginAt - 100;
return Math.max(
clientStats.startTime >= leniantBeginAt ? clientStats.endTime - clientStats.startTime : 0,
serverStats.startTime >= leniantBeginAt ? serverStats.endTime - serverStats.startTime : 0
);
}
function renderGenericError(err) {
let str = '';
if (err.code || (err.name && err.name !== 'Error')) {
str += chalk.dim(`${err.code || err.name}: `);
}
str += `${err.message}\n`;
if (!err.hideStack) {
const prettyErrStr = prettyError
.render(err)
.trim()
.split('\n')
.slice(0, -1)
.join('\n');
str += `${prettyErrStr}\n`;
}
return str;
}
function renderError(err, statsOptions) {
let str;
// If there's stats & compilation errors, then we just render them
if (err.stats && err.stats.hasErrors && err.stats.hasErrors()) {
str = `${err.message}\n\n`;
str += renderStats(err.stats, Object.assign({}, statsOptions, { assets: false }));
// Render standard error
} else {
str = renderGenericError(err);
}
return str;
}
function renderStats(stats, statsOptions) {
statsOptions = Object.assign({
assets: true,
chunks: false,
version: false,
children: false,
modules: false,
colors: chalk.enabled,
timings: false,
hash: false,
}, statsOptions);
return `${stats.toString(statsOptions).trim()}\n`;
}
function renderBanner(label) {
let str = '';
str += chalk.inverse(` ${label} ${' '.repeat(35 - label.length - 1)}`);
str += '\n';
str += chalk.dim(symbols.hr.repeat(35));
str += '\n';
return str;
}
// -----------------------------------------------------------
function getOptions(options) {
return merge({
humanErrors: true, // Display human errors
stats: true, // Display output assets, can be `true`, `false` and `once`
statsOptions: { // Stats object to use for stats.toString(),
assets: true,
chunks: false,
version: false,
children: false,
modules: false,
timings: false,
hash: false,
colors: chalk.enabled,
},
output: process.stderr, // Writable stream to print stuff
}, options);
}
function reporter(compiler, options) {
options = getOptions(options);
let displayStats = options.stats === true || options.stats === 'once';
let beginAt = -1;
function onBeginOnce() {
options.humanErrors && checkHumanErrors(compiler, options);
}
function onBegin() {
beginAt = Date.now();
const str = `${chalk.dim(symbols.bullet)} Compiling...\n`;
options.output.write(str);
}
function onEnd({ client, server }) {
let str;
str = `${chalk.green(symbols.ok)} Compilation succeeded`;
str += ` ${chalk.dim(`(${calculateDuration(client, server, beginAt)}ms)`)}\n\n`;
options.output.write(str);
if (displayStats) {
if (options.stats === 'once') {
displayStats = false;
}
str = '';
str += renderBanner('CLIENT');
str += renderStats(client, options.statsOptions);
str += '\n';
str += renderBanner('SERVER');
str += renderStats(server, options.statsOptions);
str += '\n';
options.output.write(indentString(str, 4));
}
}
function onError(err) {
let str;
str = '';
str += `${chalk.red(symbols.fail)} Compilation failed\n\n`;
str += indentString(renderError(err, options.statsOptions), 4);
str += '\n';
options.output.write(str);
}
compiler
.once('begin', onBeginOnce)
.on('begin', onBegin)
.on('end', onEnd)
.on('error', onError);
return () => {
compiler
.removeListener('begin', onBeginOnce)
.removeListener('begin', onBegin)
.removeListener('end', onEnd)
.removeListener('error', onError);
};
}
module.exports = reporter;
module.exports.getOptions = getOptions;
module.exports.renderStats = renderStats;
module.exports.renderError = renderError;