1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const Compiler = require("./Compiler");
|
9 | const MultiCompiler = require("./MultiCompiler");
|
10 | const NormalModule = require("./NormalModule");
|
11 | const createSchemaValidation = require("./util/create-schema-validation");
|
12 | const { contextify } = require("./util/identifier");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const validate = createSchemaValidation(
|
19 | require("../schemas/plugins/ProgressPlugin.check.js"),
|
20 | () => require("../schemas/plugins/ProgressPlugin.json"),
|
21 | {
|
22 | name: "Progress Plugin",
|
23 | baseDataPath: "options"
|
24 | }
|
25 | );
|
26 | const median3 = (a, b, c) => {
|
27 | return a + b + c - Math.max(a, b, c) - Math.min(a, b, c);
|
28 | };
|
29 |
|
30 | const createDefaultHandler = (profile, logger) => {
|
31 |
|
32 | const lastStateInfo = [];
|
33 |
|
34 | const defaultHandler = (percentage, msg, ...args) => {
|
35 | if (profile) {
|
36 | if (percentage === 0) {
|
37 | lastStateInfo.length = 0;
|
38 | }
|
39 | const fullState = [msg, ...args];
|
40 | const state = fullState.map(s => s.replace(/\d+\/\d+ /g, ""));
|
41 | const now = Date.now();
|
42 | const len = Math.max(state.length, lastStateInfo.length);
|
43 | for (let i = len; i >= 0; i--) {
|
44 | const stateItem = i < state.length ? state[i] : undefined;
|
45 | const lastStateItem =
|
46 | i < lastStateInfo.length ? lastStateInfo[i] : undefined;
|
47 | if (lastStateItem) {
|
48 | if (stateItem !== lastStateItem.value) {
|
49 | const diff = now - lastStateItem.time;
|
50 | if (lastStateItem.value) {
|
51 | let reportState = lastStateItem.value;
|
52 | if (i > 0) {
|
53 | reportState = lastStateInfo[i - 1].value + " > " + reportState;
|
54 | }
|
55 | const stateMsg = `${" | ".repeat(i)}${diff} ms ${reportState}`;
|
56 | const d = diff;
|
57 |
|
58 |
|
59 | {
|
60 | if (d > 10000) {
|
61 | logger.error(stateMsg);
|
62 | } else if (d > 1000) {
|
63 | logger.warn(stateMsg);
|
64 | } else if (d > 10) {
|
65 | logger.info(stateMsg);
|
66 | } else if (d > 5) {
|
67 | logger.log(stateMsg);
|
68 | } else {
|
69 | logger.debug(stateMsg);
|
70 | }
|
71 | }
|
72 | }
|
73 | if (stateItem === undefined) {
|
74 | lastStateInfo.length = i;
|
75 | } else {
|
76 | lastStateItem.value = stateItem;
|
77 | lastStateItem.time = now;
|
78 | lastStateInfo.length = i + 1;
|
79 | }
|
80 | }
|
81 | } else {
|
82 | lastStateInfo[i] = {
|
83 | value: stateItem,
|
84 | time: now
|
85 | };
|
86 | }
|
87 | }
|
88 | }
|
89 | logger.status(`${Math.floor(percentage * 100)}%`, msg, ...args);
|
90 | if (percentage === 1 || (!msg && args.length === 0)) logger.status();
|
91 | };
|
92 |
|
93 | return defaultHandler;
|
94 | };
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | const progressReporters = new WeakMap();
|
105 |
|
106 | class ProgressPlugin {
|
107 | |
108 |
|
109 |
|
110 |
|
111 | static getReporter(compiler) {
|
112 | return progressReporters.get(compiler);
|
113 | }
|
114 |
|
115 | |
116 |
|
117 |
|
118 | constructor(options = {}) {
|
119 | if (typeof options === "function") {
|
120 | options = {
|
121 | handler: options
|
122 | };
|
123 | }
|
124 |
|
125 | validate(options);
|
126 | options = { ...ProgressPlugin.defaultOptions, ...options };
|
127 |
|
128 | this.profile = options.profile;
|
129 | this.handler = options.handler;
|
130 | this.modulesCount = options.modulesCount;
|
131 | this.dependenciesCount = options.dependenciesCount;
|
132 | this.showEntries = options.entries;
|
133 | this.showModules = options.modules;
|
134 | this.showDependencies = options.dependencies;
|
135 | this.showActiveModules = options.activeModules;
|
136 | this.percentBy = options.percentBy;
|
137 | }
|
138 |
|
139 | |
140 |
|
141 |
|
142 |
|
143 | apply(compiler) {
|
144 | const handler =
|
145 | this.handler ||
|
146 | createDefaultHandler(
|
147 | this.profile,
|
148 | compiler.getInfrastructureLogger("webpack.Progress")
|
149 | );
|
150 | if (compiler instanceof MultiCompiler) {
|
151 | this._applyOnMultiCompiler(compiler, handler);
|
152 | } else if (compiler instanceof Compiler) {
|
153 | this._applyOnCompiler(compiler, handler);
|
154 | }
|
155 | }
|
156 |
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 | _applyOnMultiCompiler(compiler, handler) {
|
163 | const states = compiler.compilers.map(
|
164 | () => ([0])
|
165 | );
|
166 | compiler.compilers.forEach((compiler, idx) => {
|
167 | new ProgressPlugin((p, msg, ...args) => {
|
168 | states[idx] = [p, msg, ...args];
|
169 | let sum = 0;
|
170 | for (const [p] of states) sum += p;
|
171 | handler(sum / states.length, `[${idx}] ${msg}`, ...args);
|
172 | }).apply(compiler);
|
173 | });
|
174 | }
|
175 |
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 | _applyOnCompiler(compiler, handler) {
|
182 | const showEntries = this.showEntries;
|
183 | const showModules = this.showModules;
|
184 | const showDependencies = this.showDependencies;
|
185 | const showActiveModules = this.showActiveModules;
|
186 | let lastActiveModule = "";
|
187 | let currentLoader = "";
|
188 | let lastModulesCount = 0;
|
189 | let lastDependenciesCount = 0;
|
190 | let lastEntriesCount = 0;
|
191 | let modulesCount = 0;
|
192 | let dependenciesCount = 0;
|
193 | let entriesCount = 1;
|
194 | let doneModules = 0;
|
195 | let doneDependencies = 0;
|
196 | let doneEntries = 0;
|
197 | const activeModules = new Set();
|
198 | let lastUpdate = 0;
|
199 |
|
200 | const updateThrottled = () => {
|
201 | if (lastUpdate + 500 < Date.now()) update();
|
202 | };
|
203 |
|
204 | const update = () => {
|
205 |
|
206 | const items = [];
|
207 | const percentByModules =
|
208 | doneModules /
|
209 | Math.max(lastModulesCount || this.modulesCount || 1, modulesCount);
|
210 | const percentByEntries =
|
211 | doneEntries /
|
212 | Math.max(lastEntriesCount || this.dependenciesCount || 1, entriesCount);
|
213 | const percentByDependencies =
|
214 | doneDependencies /
|
215 | Math.max(lastDependenciesCount || 1, dependenciesCount);
|
216 | let percentageFactor;
|
217 |
|
218 | switch (this.percentBy) {
|
219 | case "entries":
|
220 | percentageFactor = percentByEntries;
|
221 | break;
|
222 | case "dependencies":
|
223 | percentageFactor = percentByDependencies;
|
224 | break;
|
225 | case "modules":
|
226 | percentageFactor = percentByModules;
|
227 | break;
|
228 | default:
|
229 | percentageFactor = median3(
|
230 | percentByModules,
|
231 | percentByEntries,
|
232 | percentByDependencies
|
233 | );
|
234 | }
|
235 |
|
236 | const percentage = 0.1 + percentageFactor * 0.55;
|
237 |
|
238 | if (currentLoader) {
|
239 | items.push(
|
240 | `import loader ${contextify(
|
241 | compiler.context,
|
242 | currentLoader,
|
243 | compiler.root
|
244 | )}`
|
245 | );
|
246 | } else {
|
247 | const statItems = [];
|
248 | if (showEntries) {
|
249 | statItems.push(`${doneEntries}/${entriesCount} entries`);
|
250 | }
|
251 | if (showDependencies) {
|
252 | statItems.push(
|
253 | `${doneDependencies}/${dependenciesCount} dependencies`
|
254 | );
|
255 | }
|
256 | if (showModules) {
|
257 | statItems.push(`${doneModules}/${modulesCount} modules`);
|
258 | }
|
259 | if (showActiveModules) {
|
260 | statItems.push(`${activeModules.size} active`);
|
261 | }
|
262 | if (statItems.length > 0) {
|
263 | items.push(statItems.join(" "));
|
264 | }
|
265 | if (showActiveModules) {
|
266 | items.push(lastActiveModule);
|
267 | }
|
268 | }
|
269 | handler(percentage, "building", ...items);
|
270 | lastUpdate = Date.now();
|
271 | };
|
272 |
|
273 | const factorizeAdd = () => {
|
274 | dependenciesCount++;
|
275 | if (dependenciesCount < 50 || dependenciesCount % 100 === 0)
|
276 | updateThrottled();
|
277 | };
|
278 |
|
279 | const factorizeDone = () => {
|
280 | doneDependencies++;
|
281 | if (doneDependencies < 50 || doneDependencies % 100 === 0)
|
282 | updateThrottled();
|
283 | };
|
284 |
|
285 | const moduleAdd = () => {
|
286 | modulesCount++;
|
287 | if (modulesCount < 50 || modulesCount % 100 === 0) updateThrottled();
|
288 | };
|
289 |
|
290 |
|
291 | const moduleBuild = module => {
|
292 | const ident = module.identifier();
|
293 | if (ident) {
|
294 | activeModules.add(ident);
|
295 | lastActiveModule = ident;
|
296 | update();
|
297 | }
|
298 | };
|
299 |
|
300 | const entryAdd = (entry, options) => {
|
301 | entriesCount++;
|
302 | if (entriesCount < 5 || entriesCount % 10 === 0) updateThrottled();
|
303 | };
|
304 |
|
305 | const moduleDone = module => {
|
306 | doneModules++;
|
307 | if (showActiveModules) {
|
308 | const ident = module.identifier();
|
309 | if (ident) {
|
310 | activeModules.delete(ident);
|
311 | if (lastActiveModule === ident) {
|
312 | lastActiveModule = "";
|
313 | for (const m of activeModules) {
|
314 | lastActiveModule = m;
|
315 | }
|
316 | update();
|
317 | return;
|
318 | }
|
319 | }
|
320 | }
|
321 | if (doneModules < 50 || doneModules % 100 === 0) updateThrottled();
|
322 | };
|
323 |
|
324 | const entryDone = (entry, options) => {
|
325 | doneEntries++;
|
326 | update();
|
327 | };
|
328 |
|
329 | const cache = compiler
|
330 | .getCache("ProgressPlugin")
|
331 | .getItemCache("counts", null);
|
332 |
|
333 | let cacheGetPromise;
|
334 |
|
335 | compiler.hooks.beforeCompile.tap("ProgressPlugin", () => {
|
336 | if (!cacheGetPromise) {
|
337 | cacheGetPromise = cache.getPromise().then(
|
338 | data => {
|
339 | if (data) {
|
340 | lastModulesCount = lastModulesCount || data.modulesCount;
|
341 | lastDependenciesCount =
|
342 | lastDependenciesCount || data.dependenciesCount;
|
343 | }
|
344 | return data;
|
345 | },
|
346 | err => {
|
347 |
|
348 | }
|
349 | );
|
350 | }
|
351 | });
|
352 |
|
353 | compiler.hooks.afterCompile.tapPromise("ProgressPlugin", compilation => {
|
354 | if (compilation.compiler.isChild()) return Promise.resolve();
|
355 | return cacheGetPromise.then(async oldData => {
|
356 | if (
|
357 | !oldData ||
|
358 | oldData.modulesCount !== modulesCount ||
|
359 | oldData.dependenciesCount !== dependenciesCount
|
360 | ) {
|
361 | await cache.storePromise({ modulesCount, dependenciesCount });
|
362 | }
|
363 | });
|
364 | });
|
365 |
|
366 | compiler.hooks.compilation.tap("ProgressPlugin", compilation => {
|
367 | if (compilation.compiler.isChild()) return;
|
368 | lastModulesCount = modulesCount;
|
369 | lastEntriesCount = entriesCount;
|
370 | lastDependenciesCount = dependenciesCount;
|
371 | modulesCount = dependenciesCount = entriesCount = 0;
|
372 | doneModules = doneDependencies = doneEntries = 0;
|
373 |
|
374 | compilation.factorizeQueue.hooks.added.tap(
|
375 | "ProgressPlugin",
|
376 | factorizeAdd
|
377 | );
|
378 | compilation.factorizeQueue.hooks.result.tap(
|
379 | "ProgressPlugin",
|
380 | factorizeDone
|
381 | );
|
382 |
|
383 | compilation.addModuleQueue.hooks.added.tap("ProgressPlugin", moduleAdd);
|
384 | compilation.processDependenciesQueue.hooks.result.tap(
|
385 | "ProgressPlugin",
|
386 | moduleDone
|
387 | );
|
388 |
|
389 | if (showActiveModules) {
|
390 | compilation.hooks.buildModule.tap("ProgressPlugin", moduleBuild);
|
391 | }
|
392 |
|
393 | compilation.hooks.addEntry.tap("ProgressPlugin", entryAdd);
|
394 | compilation.hooks.failedEntry.tap("ProgressPlugin", entryDone);
|
395 | compilation.hooks.succeedEntry.tap("ProgressPlugin", entryDone);
|
396 |
|
397 |
|
398 |
|
399 | if (typeof __webpack_require__ !== "function") {
|
400 | const requiredLoaders = new Set();
|
401 | NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(
|
402 | "ProgressPlugin",
|
403 | loaders => {
|
404 | for (const loader of loaders) {
|
405 | if (
|
406 | loader.type !== "module" &&
|
407 | !requiredLoaders.has(loader.loader)
|
408 | ) {
|
409 | requiredLoaders.add(loader.loader);
|
410 | currentLoader = loader.loader;
|
411 | update();
|
412 | require(loader.loader);
|
413 | }
|
414 | }
|
415 | if (currentLoader) {
|
416 | currentLoader = "";
|
417 | update();
|
418 | }
|
419 | }
|
420 | );
|
421 | }
|
422 |
|
423 | const hooks = {
|
424 | finishModules: "finish module graph",
|
425 | seal: "plugins",
|
426 | optimizeDependencies: "dependencies optimization",
|
427 | afterOptimizeDependencies: "after dependencies optimization",
|
428 | beforeChunks: "chunk graph",
|
429 | afterChunks: "after chunk graph",
|
430 | optimize: "optimizing",
|
431 | optimizeModules: "module optimization",
|
432 | afterOptimizeModules: "after module optimization",
|
433 | optimizeChunks: "chunk optimization",
|
434 | afterOptimizeChunks: "after chunk optimization",
|
435 | optimizeTree: "module and chunk tree optimization",
|
436 | afterOptimizeTree: "after module and chunk tree optimization",
|
437 | optimizeChunkModules: "chunk modules optimization",
|
438 | afterOptimizeChunkModules: "after chunk modules optimization",
|
439 | reviveModules: "module reviving",
|
440 | beforeModuleIds: "before module ids",
|
441 | moduleIds: "module ids",
|
442 | optimizeModuleIds: "module id optimization",
|
443 | afterOptimizeModuleIds: "module id optimization",
|
444 | reviveChunks: "chunk reviving",
|
445 | beforeChunkIds: "before chunk ids",
|
446 | chunkIds: "chunk ids",
|
447 | optimizeChunkIds: "chunk id optimization",
|
448 | afterOptimizeChunkIds: "after chunk id optimization",
|
449 | recordModules: "record modules",
|
450 | recordChunks: "record chunks",
|
451 | beforeModuleHash: "module hashing",
|
452 | beforeCodeGeneration: "code generation",
|
453 | beforeRuntimeRequirements: "runtime requirements",
|
454 | beforeHash: "hashing",
|
455 | afterHash: "after hashing",
|
456 | recordHash: "record hash",
|
457 | beforeModuleAssets: "module assets processing",
|
458 | beforeChunkAssets: "chunk assets processing",
|
459 | processAssets: "asset processing",
|
460 | afterProcessAssets: "after asset optimization",
|
461 | record: "recording",
|
462 | afterSeal: "after seal"
|
463 | };
|
464 | const numberOfHooks = Object.keys(hooks).length;
|
465 | Object.keys(hooks).forEach((name, idx) => {
|
466 | const title = hooks[name];
|
467 | const percentage = (idx / numberOfHooks) * 0.25 + 0.7;
|
468 | compilation.hooks[name].intercept({
|
469 | name: "ProgressPlugin",
|
470 | call() {
|
471 | handler(percentage, "sealing", title);
|
472 | },
|
473 | done() {
|
474 | progressReporters.set(compiler, undefined);
|
475 | handler(percentage, "sealing", title);
|
476 | },
|
477 | result() {
|
478 | handler(percentage, "sealing", title);
|
479 | },
|
480 | error() {
|
481 | handler(percentage, "sealing", title);
|
482 | },
|
483 | tap(tap) {
|
484 |
|
485 |
|
486 | progressReporters.set(compilation.compiler, (p, ...args) => {
|
487 | handler(percentage, "sealing", title, tap.name, ...args);
|
488 | });
|
489 | handler(percentage, "sealing", title, tap.name);
|
490 | }
|
491 | });
|
492 | });
|
493 | });
|
494 | compiler.hooks.make.intercept({
|
495 | name: "ProgressPlugin",
|
496 | call() {
|
497 | handler(0.1, "building");
|
498 | },
|
499 | done() {
|
500 | handler(0.65, "building");
|
501 | }
|
502 | });
|
503 | const interceptHook = (hook, progress, category, name) => {
|
504 | hook.intercept({
|
505 | name: "ProgressPlugin",
|
506 | call() {
|
507 | handler(progress, category, name);
|
508 | },
|
509 | done() {
|
510 | progressReporters.set(compiler, undefined);
|
511 | handler(progress, category, name);
|
512 | },
|
513 | result() {
|
514 | handler(progress, category, name);
|
515 | },
|
516 | error() {
|
517 | handler(progress, category, name);
|
518 | },
|
519 | tap(tap) {
|
520 | progressReporters.set(compiler, (p, ...args) => {
|
521 | handler(progress, category, name, tap.name, ...args);
|
522 | });
|
523 | handler(progress, category, name, tap.name);
|
524 | }
|
525 | });
|
526 | };
|
527 | compiler.cache.hooks.endIdle.intercept({
|
528 | name: "ProgressPlugin",
|
529 | call() {
|
530 | handler(0, "");
|
531 | }
|
532 | });
|
533 | interceptHook(compiler.cache.hooks.endIdle, 0.01, "cache", "end idle");
|
534 | compiler.hooks.initialize.intercept({
|
535 | name: "ProgressPlugin",
|
536 | call() {
|
537 | handler(0, "");
|
538 | }
|
539 | });
|
540 | interceptHook(compiler.hooks.initialize, 0.01, "setup", "initialize");
|
541 | interceptHook(compiler.hooks.beforeRun, 0.02, "setup", "before run");
|
542 | interceptHook(compiler.hooks.run, 0.03, "setup", "run");
|
543 | interceptHook(compiler.hooks.watchRun, 0.03, "setup", "watch run");
|
544 | interceptHook(
|
545 | compiler.hooks.normalModuleFactory,
|
546 | 0.04,
|
547 | "setup",
|
548 | "normal module factory"
|
549 | );
|
550 | interceptHook(
|
551 | compiler.hooks.contextModuleFactory,
|
552 | 0.05,
|
553 | "setup",
|
554 | "context module factory"
|
555 | );
|
556 | interceptHook(
|
557 | compiler.hooks.beforeCompile,
|
558 | 0.06,
|
559 | "setup",
|
560 | "before compile"
|
561 | );
|
562 | interceptHook(compiler.hooks.compile, 0.07, "setup", "compile");
|
563 | interceptHook(compiler.hooks.thisCompilation, 0.08, "setup", "compilation");
|
564 | interceptHook(compiler.hooks.compilation, 0.09, "setup", "compilation");
|
565 | interceptHook(compiler.hooks.finishMake, 0.69, "building", "finish");
|
566 | interceptHook(compiler.hooks.emit, 0.95, "emitting", "emit");
|
567 | interceptHook(compiler.hooks.afterEmit, 0.98, "emitting", "after emit");
|
568 | interceptHook(compiler.hooks.done, 0.99, "done", "plugins");
|
569 | compiler.hooks.done.intercept({
|
570 | name: "ProgressPlugin",
|
571 | done() {
|
572 | handler(0.99, "");
|
573 | }
|
574 | });
|
575 | interceptHook(
|
576 | compiler.cache.hooks.storeBuildDependencies,
|
577 | 0.99,
|
578 | "cache",
|
579 | "store build dependencies"
|
580 | );
|
581 | interceptHook(compiler.cache.hooks.shutdown, 0.99, "cache", "shutdown");
|
582 | interceptHook(compiler.cache.hooks.beginIdle, 0.99, "cache", "begin idle");
|
583 | interceptHook(
|
584 | compiler.hooks.watchClose,
|
585 | 0.99,
|
586 | "end",
|
587 | "closing watch compilation"
|
588 | );
|
589 | compiler.cache.hooks.beginIdle.intercept({
|
590 | name: "ProgressPlugin",
|
591 | done() {
|
592 | handler(1, "");
|
593 | }
|
594 | });
|
595 | compiler.cache.hooks.shutdown.intercept({
|
596 | name: "ProgressPlugin",
|
597 | done() {
|
598 | handler(1, "");
|
599 | }
|
600 | });
|
601 | }
|
602 | }
|
603 |
|
604 | ProgressPlugin.defaultOptions = {
|
605 | profile: false,
|
606 | modulesCount: 5000,
|
607 | dependenciesCount: 10000,
|
608 | modules: true,
|
609 | dependencies: true,
|
610 | activeModules: false,
|
611 | entries: true
|
612 | };
|
613 |
|
614 | module.exports = ProgressPlugin;
|