UNPKG

1.59 kBJavaScriptView Raw
1const webpack = require('webpack');
2
3const { ProgressPlugin } = webpack;
4
5function parseArgs(profile, ...args) {
6 // args is an unorganized set of parameters.
7 // e.g. building modules|5/6 modules|1 active|/node_modules/lodash/lodash.js
8 // building modules|14/14 modules|0 active|
9 // finish module graph
10 // finish module graph|FlagDependencyExportsPlugin
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 // we've got a step with a scope on our hands
22 // e.g. finish module graph|FlagDependencyExportsPlugin
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
44module.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};