1 | 'use strict';
|
2 |
|
3 | const chalk = require('chalk');
|
4 | const parse = require('./lib/parse');
|
5 | const style = require('./lib/style');
|
6 |
|
7 | module.exports = class StylishReporter {
|
8 | constructor() {
|
9 | this.rendered = {
|
10 | header: false
|
11 | };
|
12 |
|
13 | this.state = { active: 0, instances: 0, time: 0 };
|
14 | }
|
15 |
|
16 | apply(compiler) {
|
17 | const { rendered, state } = this;
|
18 |
|
19 | state.active += 1;
|
20 | state.instances += 1;
|
21 |
|
22 | function render(stats) {
|
23 | const opts = {
|
24 | context: '/Users/powella/code/webpack-stylish/test/fixture',
|
25 | cached: false,
|
26 | cachedAssets: false,
|
27 | exclude: ['node_modules', 'bower_components', 'components']
|
28 | };
|
29 |
|
30 |
|
31 |
|
32 | const json = stats.toJson(opts, true);
|
33 | state.time += json.time;
|
34 |
|
35 |
|
36 | const problems = style.problems(parse.problems(json));
|
37 | const files = style.files(parse.files(json), compiler.options);
|
38 | const hidden = style.hidden(parse.hidden(json));
|
39 | const hash = style.hash(json, files, hidden);
|
40 |
|
41 | const { version } = json;
|
42 | const log = [];
|
43 |
|
44 | if (!rendered.header) {
|
45 | rendered.header = true;
|
46 | log.push(chalk.cyan(`webpack v${version}\n`));
|
47 | }
|
48 |
|
49 | log.push(hash);
|
50 | log.push(problems);
|
51 |
|
52 | state.active -= 1;
|
53 |
|
54 | if (state.active === 0) {
|
55 | const footer = style.footer(parse.footer(state));
|
56 | rendered.footer = true;
|
57 | log.push(footer);
|
58 | }
|
59 |
|
60 | console.log(log.join('\n'));
|
61 | }
|
62 |
|
63 | compiler.options.stats = 'none';
|
64 |
|
65 | if (compiler.hooks) {
|
66 | compiler.hooks.done.tap('webpack-stylish', render);
|
67 | } else {
|
68 | compiler.plugin('done', render);
|
69 | }
|
70 | }
|
71 | };
|