source: imaps-frontend/node_modules/webpack/lib/ProgressPlugin.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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