This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
100 lines (81 loc) · 2.38 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
'use strict';
const chalk = require('chalk');
const parse = require('./lib/parse');
const style = require('./lib/style');
module.exports = class StylishReporter {
constructor() {
this.rendered = {
footer: false,
header: false
};
this.state = {
active: 0,
hashes: [],
instances: 0,
totals: {
errors: 0,
time: 0,
warnings: 0
},
time: 0
};
}
apply(compiler) {
const { rendered, state } = this;
const { log } = console;
state.active += 1;
state.instances += 1;
function render(stats) {
const opts = {
context: compiler.context,
cached: false,
cachedAssets: false,
exclude: ['node_modules', 'bower_components', 'components']
};
const json = stats.toJson(opts, true);
// for --watch more than anything, don't print duplicate output for a hash
// if we've already seen that hash. compensates for a bug in webpack.
if (state.hashes.includes(json.hash)) {
return;
}
state.active -= 1;
state.hashes.push(json.hash);
state.time += json.time;
// errors and warnings go first, to make sure the counts are correct for modules
const problems = style.problems(parse.problems(json, state));
const files = style.files(parse.files(json), compiler.options);
const hidden = style.hidden(parse.hidden(json));
const hash = style.hash(json, files, hidden);
const { version } = json;
const out = [];
if (!rendered.header) {
rendered.header = true;
out.push(chalk.cyan(`\nwebpack v${version}\n`));
}
out.push(hash);
out.push(problems);
// note: when --watch the active count will drop below zero.
if (state.active <= 0) {
const footer = style.footer(parse.footer(state));
if (footer.length) {
rendered.footer = true;
out.push(footer);
}
// reset the totals
state.totals = { errors: 0, time: 0, warnings: 0 };
} else {
rendered.footer = false;
}
log(out.join('\n'));
if (rendered.footer && compiler.options.watch === true) {
log();
}
}
compiler.options.stats = 'none';
if (compiler.hooks) {
compiler.hooks.done.tap('webpack-stylish', render);
} else {
compiler.plugin('done', render);
}
}
};