-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
57 lines (47 loc) · 1.54 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
const colors = require('kleur');
const cClear = require('console-clear');
const format = require('webpack-format-messages');
const NAME = 'webpack-messages';
const log = str => console.log(str);
const clear = _ => (cClear(true),true);
class WebpackMessages {
constructor(opts) {
opts = opts || {};
this.name = opts.name;
this.onDone = opts.onComplete;
this.logger = opts.logger || log;
}
printError(str, arr) {
arr && (str += '\n\n' + arr.join(''));
clear() && this.logger(str);
}
apply(compiler) {
const name = this.name ? ` ${colors.cyan(this.name)} bundle` : '';
const onStart = _ => this.logger(`Building${name}...`);
const onComplete = stats => {
const messages = format(stats);
if (messages.errors.length) {
return this.printError( colors.red(`Failed to compile${name}!`), messages.errors );
}
if (messages.warnings.length) {
return this.printError( colors.yellow(`Compiled${name} with warnings.`), messages.warnings );
}
if (this.onDone !== undefined) {
this.onDone(name, stats);
} else {
const sec = (stats.endTime - stats.startTime) / 1e3;
this.logger( colors.green(`Completed${name} in ${sec}s!`) );
}
};
if (compiler.hooks !== void 0) {
compiler.hooks.compile.tap(NAME, onStart);
compiler.hooks.invalid.tap(NAME, _ => clear() && onStart());
compiler.hooks.done.tap(NAME, onComplete);
} else {
compiler.plugin('compile', onStart);
compiler.plugin('invalid', _ => clear() && onStart());
compiler.plugin('done', onComplete);
}
}
}
module.exports = WebpackMessages;