UNPKG

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