ProgressPlugin.js
5.79 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
class ProgressPlugin {
constructor(options) {
if(typeof options === "function") {
options = {
handler: options
};
}
options = options || {};
this.profile = options.profile;
this.handler = options.handler;
}
apply(compiler) {
const handler = this.handler || defaultHandler;
const profile = this.profile;
if(compiler.compilers) {
const states = new Array(compiler.compilers.length);
compiler.compilers.forEach(function(compiler, idx) {
compiler.apply(new ProgressPlugin(function(p, msg) {
states[idx] = Array.prototype.slice.apply(arguments);
handler.apply(null, [
states.map(state => state && state[0] || 0).reduce((a, b) => a + b) / states.length,
`[${idx}] ${msg}`
].concat(Array.prototype.slice.call(arguments, 2)));
}));
});
} else {
let lastModulesCount = 0;
let moduleCount = 500;
let doneModules = 0;
const activeModules = [];
const update = function update(module) {
handler(
0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6,
"building modules",
`${doneModules}/${moduleCount} modules`,
`${activeModules.length} active`,
activeModules[activeModules.length - 1]
);
};
const moduleDone = function moduleDone(module) {
doneModules++;
const ident = module.identifier();
if(ident) {
const idx = activeModules.indexOf(ident);
if(idx >= 0) activeModules.splice(idx, 1);
}
update();
};
compiler.plugin("compilation", function(compilation) {
if(compilation.compiler.isChild()) return;
lastModulesCount = moduleCount;
moduleCount = 0;
doneModules = 0;
handler(0, "compiling");
compilation.plugin("build-module", function(module) {
moduleCount++;
const ident = module.identifier();
if(ident) {
activeModules.push(ident);
}
update();
});
compilation.plugin("failed-module", moduleDone);
compilation.plugin("succeed-module", moduleDone);
const syncHooks = {
"seal": [0.71, "sealing"],
"optimize": [0.72, "optimizing"],
"optimize-modules-basic": [0.73, "basic module optimization"],
"optimize-modules": [0.74, "module optimization"],
"optimize-modules-advanced": [0.75, "advanced module optimization"],
"optimize-chunks-basic": [0.76, "basic chunk optimization"],
"optimize-chunks": [0.77, "chunk optimization"],
"optimize-chunks-advanced": [0.78, "advanced chunk optimization"],
// optimize-tree
"optimize-chunk-modules": [0.80, "chunk modules optimization"],
"optimize-chunk-modules-advanced": [0.81, "advanced chunk modules optimization"],
"revive-modules": [0.82, "module reviving"],
"optimize-module-order": [0.83, "module order optimization"],
"optimize-module-ids": [0.84, "module id optimization"],
"revive-chunks": [0.85, "chunk reviving"],
"optimize-chunk-order": [0.86, "chunk order optimization"],
"optimize-chunk-ids": [0.87, "chunk id optimization"],
"before-hash": [0.88, "hashing"],
"before-module-assets": [0.89, "module assets processing"],
"before-chunk-assets": [0.90, "chunk assets processing"],
"additional-chunk-assets": [0.91, "additional chunk assets processing"],
"record": [0.92, "recording"]
};
Object.keys(syncHooks).forEach(name => {
let pass = 0;
const settings = syncHooks[name];
compilation.plugin(name, () => {
if(pass++ > 0)
handler(settings[0], settings[1], `pass ${pass}`);
else
handler(settings[0], settings[1]);
});
});
compilation.plugin("optimize-tree", (chunks, modules, callback) => {
handler(0.79, "module and chunk tree optimization");
callback();
});
compilation.plugin("additional-assets", callback => {
handler(0.91, "additional asset processing");
callback();
});
compilation.plugin("optimize-chunk-assets", (chunks, callback) => {
handler(0.92, "chunk asset optimization");
callback();
});
compilation.plugin("optimize-assets", (assets, callback) => {
handler(0.94, "asset optimization");
callback();
});
});
compiler.plugin("emit", (compilation, callback) => {
handler(0.95, "emitting");
callback();
});
compiler.plugin("done", () => {
handler(1, "");
});
}
let lineCaretPosition = 0,
lastState, lastStateTime;
function defaultHandler(percentage, msg) {
let state = msg;
const details = Array.prototype.slice.call(arguments, 2);
if(percentage < 1) {
percentage = Math.floor(percentage * 100);
msg = `${percentage}% ${msg}`;
if(percentage < 100) {
msg = ` ${msg}`;
}
if(percentage < 10) {
msg = ` ${msg}`;
}
details.forEach(detail => {
if(!detail) return;
if(detail.length > 40) {
detail = `...${detail.substr(detail.length - 37)}`;
}
msg += ` ${detail}`;
});
}
if(profile) {
state = state.replace(/^\d+\/\d+\s+/, "");
if(percentage === 0) {
lastState = null;
lastStateTime = Date.now();
} else if(state !== lastState || percentage === 1) {
const now = Date.now();
if(lastState) {
const stateMsg = `${now - lastStateTime}ms ${lastState}`;
goToLineStart(stateMsg);
process.stderr.write(stateMsg + "\n");
lineCaretPosition = 0;
}
lastState = state;
lastStateTime = now;
}
}
goToLineStart(msg);
process.stderr.write(msg);
}
function goToLineStart(nextMessage) {
let str = "";
for(; lineCaretPosition > nextMessage.length; lineCaretPosition--) {
str += "\b \b";
}
for(var i = 0; i < lineCaretPosition; i++) {
str += "\b";
}
lineCaretPosition = nextMessage.length;
if(str) process.stderr.write(str);
}
}
}
module.exports = ProgressPlugin;