1 | const webpack = require('webpack');
|
2 |
|
3 | const { ProgressPlugin } = webpack;
|
4 |
|
5 | function parseArgs(profile, ...args) {
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const [value, , counts, index, fileName] = args;
|
12 | const [, modulePos, totalModules] = (
|
13 | (counts || '').match(/(\d+)\/(\d+)/) || []
|
14 | ).map((match) => parseInt(match, 10));
|
15 | const [, indexNumber, indexState] = (index || '').match(/(\d+)\s(.+)/) || [];
|
16 | const percentage = parseFloat(value);
|
17 | const [, stepName] = args;
|
18 | let scope;
|
19 | let empty;
|
20 |
|
21 |
|
22 |
|
23 | if (args.length === 3) {
|
24 | scope = counts;
|
25 | }
|
26 |
|
27 | const result = {
|
28 | profile,
|
29 | fileName,
|
30 | scope,
|
31 | step: {
|
32 | index: parseInt(indexNumber, 10) || empty,
|
33 | modulePosition: modulePos || empty,
|
34 | name: stepName,
|
35 | percentage: percentage || empty,
|
36 | state: indexState,
|
37 | totalModules: totalModules || empty,
|
38 | },
|
39 | };
|
40 |
|
41 | return result;
|
42 | }
|
43 |
|
44 | module.exports = {
|
45 | apply(config, compiler, reporter) {
|
46 | const { profile } = config;
|
47 | const opts = { profile };
|
48 |
|
49 | if (reporter.progress) {
|
50 | opts.handler = (...args) => {
|
51 | const data = parseArgs(profile, ...args);
|
52 | reporter.progress(data);
|
53 | };
|
54 | }
|
55 |
|
56 | const plugin = new ProgressPlugin(opts);
|
57 | plugin.apply(compiler);
|
58 | },
|
59 |
|
60 | parseArgs,
|
61 | };
|