UNPKG

17.1 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 validate(schema, options, {
119 name: "Progress Plugin",
120 baseDataPath: "options"
121 });
122 options = { ...ProgressPlugin.defaultOptions, ...options };
123
124 this.profile = options.profile;
125 this.handler = options.handler;
126 this.modulesCount = options.modulesCount;
127 this.dependenciesCount = options.dependenciesCount;
128 this.showEntries = options.entries;
129 this.showModules = options.modules;
130 this.showDependencies = options.dependencies;
131 this.showActiveModules = options.activeModules;
132 this.percentBy = options.percentBy;
133 }
134
135 /**
136 * @param {Compiler | MultiCompiler} compiler webpack compiler
137 * @returns {void}
138 */
139 apply(compiler) {
140 const handler =
141 this.handler ||
142 createDefaultHandler(
143 this.profile,
144 compiler.getInfrastructureLogger("webpack.Progress")
145 );
146 if (compiler instanceof MultiCompiler) {
147 this._applyOnMultiCompiler(compiler, handler);
148 } else if (compiler instanceof Compiler) {
149 this._applyOnCompiler(compiler, handler);
150 }
151 }
152
153 /**
154 * @param {MultiCompiler} compiler webpack multi-compiler
155 * @param {HandlerFunction} handler function that executes for every progress step
156 * @returns {void}
157 */
158 _applyOnMultiCompiler(compiler, handler) {
159 const states = compiler.compilers.map(
160 () => /** @type {[number, ...string[]]} */ ([0])
161 );
162 compiler.compilers.forEach((compiler, idx) => {
163 new ProgressPlugin((p, msg, ...args) => {
164 states[idx] = [p, msg, ...args];
165 let sum = 0;
166 for (const [p] of states) sum += p;
167 handler(sum / states.length, `[${idx}] ${msg}`, ...args);
168 }).apply(compiler);
169 });
170 }
171
172 /**
173 * @param {Compiler} compiler webpack compiler
174 * @param {HandlerFunction} handler function that executes for every progress step
175 * @returns {void}
176 */
177 _applyOnCompiler(compiler, handler) {
178 const showEntries = this.showEntries;
179 const showModules = this.showModules;
180 const showDependencies = this.showDependencies;
181 const showActiveModules = this.showActiveModules;
182 let lastActiveModule = "";
183 let currentLoader = "";
184 let lastModulesCount = 0;
185 let lastDependenciesCount = 0;
186 let lastEntriesCount = 0;
187 let modulesCount = 0;
188 let dependenciesCount = 0;
189 let entriesCount = 1;
190 let doneModules = 0;
191 let doneDependencies = 0;
192 let doneEntries = 0;
193 const activeModules = new Set();
194 let lastUpdate = 0;
195
196 const updateThrottled = () => {
197 if (lastUpdate + 500 < Date.now()) update();
198 };
199
200 const update = () => {
201 /** @type {string[]} */
202 const items = [];
203 const percentByModules =
204 doneModules /
205 Math.max(lastModulesCount || this.modulesCount || 1, modulesCount);
206 const percentByEntries =
207 doneEntries /
208 Math.max(lastEntriesCount || this.dependenciesCount || 1, entriesCount);
209 const percentByDependencies =
210 doneDependencies /
211 Math.max(lastDependenciesCount || 1, 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 < 50 || dependenciesCount % 100 === 0)
272 updateThrottled();
273 };
274
275 const factorizeDone = () => {
276 doneDependencies++;
277 if (doneDependencies < 50 || doneDependencies % 100 === 0)
278 updateThrottled();
279 };
280
281 const moduleAdd = () => {
282 modulesCount++;
283 if (modulesCount < 50 || modulesCount % 100 === 0) updateThrottled();
284 };
285
286 // only used when showActiveModules is set
287 const moduleBuild = module => {
288 const ident = module.identifier();
289 if (ident) {
290 activeModules.add(ident);
291 lastActiveModule = ident;
292 update();
293 }
294 };
295
296 const entryAdd = (entry, options) => {
297 entriesCount++;
298 if (entriesCount < 5 || entriesCount % 10 === 0) updateThrottled();
299 };
300
301 const moduleDone = module => {
302 doneModules++;
303 if (showActiveModules) {
304 const ident = module.identifier();
305 if (ident) {
306 activeModules.delete(ident);
307 if (lastActiveModule === ident) {
308 lastActiveModule = "";
309 for (const m of activeModules) {
310 lastActiveModule = m;
311 }
312 update();
313 return;
314 }
315 }
316 }
317 if (doneModules < 50 || doneModules % 100 === 0) updateThrottled();
318 };
319
320 const entryDone = (entry, options) => {
321 doneEntries++;
322 update();
323 };
324
325 const cache = compiler
326 .getCache("ProgressPlugin")
327 .getItemCache("counts", null);
328
329 let cacheGetPromise;
330
331 compiler.hooks.beforeCompile.tap("ProgressPlugin", () => {
332 if (!cacheGetPromise) {
333 cacheGetPromise = cache.getPromise().then(
334 data => {
335 if (data) {
336 lastModulesCount = lastModulesCount || data.modulesCount;
337 lastDependenciesCount =
338 lastDependenciesCount || data.dependenciesCount;
339 }
340 return data;
341 },
342 err => {
343 // Ignore error
344 }
345 );
346 }
347 });
348
349 compiler.hooks.afterCompile.tapPromise("ProgressPlugin", compilation => {
350 if (compilation.compiler.isChild()) return Promise.resolve();
351 return cacheGetPromise.then(async oldData => {
352 if (
353 !oldData ||
354 oldData.modulesCount !== modulesCount ||
355 oldData.dependenciesCount !== dependenciesCount
356 ) {
357 await cache.storePromise({ modulesCount, dependenciesCount });
358 }
359 });
360 });
361
362 compiler.hooks.compilation.tap("ProgressPlugin", compilation => {
363 if (compilation.compiler.isChild()) return;
364 lastModulesCount = modulesCount;
365 lastEntriesCount = entriesCount;
366 lastDependenciesCount = dependenciesCount;
367 modulesCount = dependenciesCount = entriesCount = 0;
368 doneModules = doneDependencies = doneEntries = 0;
369
370 compilation.factorizeQueue.hooks.added.tap(
371 "ProgressPlugin",
372 factorizeAdd
373 );
374 compilation.factorizeQueue.hooks.result.tap(
375 "ProgressPlugin",
376 factorizeDone
377 );
378
379 compilation.addModuleQueue.hooks.added.tap("ProgressPlugin", moduleAdd);
380 compilation.processDependenciesQueue.hooks.result.tap(
381 "ProgressPlugin",
382 moduleDone
383 );
384
385 if (showActiveModules) {
386 compilation.hooks.buildModule.tap("ProgressPlugin", moduleBuild);
387 }
388
389 compilation.hooks.addEntry.tap("ProgressPlugin", entryAdd);
390 compilation.hooks.failedEntry.tap("ProgressPlugin", entryDone);
391 compilation.hooks.succeedEntry.tap("ProgressPlugin", entryDone);
392
393 // avoid dynamic require if bundled with webpack
394 // @ts-expect-error
395 if (typeof __webpack_require__ !== "function") {
396 const requiredLoaders = new Set();
397 NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(
398 "ProgressPlugin",
399 loaders => {
400 for (const loader of loaders) {
401 if (
402 loader.type !== "module" &&
403 !requiredLoaders.has(loader.loader)
404 ) {
405 requiredLoaders.add(loader.loader);
406 currentLoader = loader.loader;
407 update();
408 require(loader.loader);
409 }
410 }
411 if (currentLoader) {
412 currentLoader = "";
413 update();
414 }
415 }
416 );
417 }
418
419 const hooks = {
420 finishModules: "finish module graph",
421 seal: "plugins",
422 optimizeDependencies: "dependencies optimization",
423 afterOptimizeDependencies: "after dependencies optimization",
424 beforeChunks: "chunk graph",
425 afterChunks: "after chunk graph",
426 optimize: "optimizing",
427 optimizeModules: "module optimization",
428 afterOptimizeModules: "after module optimization",
429 optimizeChunks: "chunk optimization",
430 afterOptimizeChunks: "after chunk optimization",
431 optimizeTree: "module and chunk tree optimization",
432 afterOptimizeTree: "after module and chunk tree optimization",
433 optimizeChunkModules: "chunk modules optimization",
434 afterOptimizeChunkModules: "after chunk modules optimization",
435 reviveModules: "module reviving",
436 beforeModuleIds: "before module ids",
437 moduleIds: "module ids",
438 optimizeModuleIds: "module id optimization",
439 afterOptimizeModuleIds: "module id optimization",
440 reviveChunks: "chunk reviving",
441 beforeChunkIds: "before chunk ids",
442 chunkIds: "chunk ids",
443 optimizeChunkIds: "chunk id optimization",
444 afterOptimizeChunkIds: "after chunk id optimization",
445 recordModules: "record modules",
446 recordChunks: "record chunks",
447 beforeModuleHash: "module hashing",
448 beforeCodeGeneration: "code generation",
449 beforeRuntimeRequirements: "runtime requirements",
450 beforeHash: "hashing",
451 afterHash: "after hashing",
452 recordHash: "record hash",
453 beforeModuleAssets: "module assets processing",
454 beforeChunkAssets: "chunk assets processing",
455 processAssets: "asset processing",
456 afterProcessAssets: "after asset optimization",
457 record: "recording",
458 afterSeal: "after seal"
459 };
460 const numberOfHooks = Object.keys(hooks).length;
461 Object.keys(hooks).forEach((name, idx) => {
462 const title = hooks[name];
463 const percentage = (idx / numberOfHooks) * 0.25 + 0.7;
464 compilation.hooks[name].intercept({
465 name: "ProgressPlugin",
466 call() {
467 handler(percentage, "sealing", title);
468 },
469 done() {
470 progressReporters.set(compiler, undefined);
471 handler(percentage, "sealing", title);
472 },
473 result() {
474 handler(percentage, "sealing", title);
475 },
476 error() {
477 handler(percentage, "sealing", title);
478 },
479 tap(tap) {
480 // p is percentage from 0 to 1
481 // args is any number of messages in a hierarchical matter
482 progressReporters.set(compilation.compiler, (p, ...args) => {
483 handler(percentage, "sealing", title, tap.name, ...args);
484 });
485 handler(percentage, "sealing", title, tap.name);
486 }
487 });
488 });
489 });
490 compiler.hooks.make.intercept({
491 name: "ProgressPlugin",
492 call() {
493 handler(0.1, "building");
494 },
495 done() {
496 handler(0.65, "building");
497 }
498 });
499 const interceptHook = (hook, progress, category, name) => {
500 hook.intercept({
501 name: "ProgressPlugin",
502 call() {
503 handler(progress, category, name);
504 },
505 done() {
506 progressReporters.set(compiler, undefined);
507 handler(progress, category, name);
508 },
509 result() {
510 handler(progress, category, name);
511 },
512 error() {
513 handler(progress, category, name);
514 },
515 tap(tap) {
516 progressReporters.set(compiler, (p, ...args) => {
517 handler(progress, category, name, tap.name, ...args);
518 });
519 handler(progress, category, name, tap.name);
520 }
521 });
522 };
523 compiler.cache.hooks.endIdle.intercept({
524 name: "ProgressPlugin",
525 call() {
526 handler(0, "");
527 }
528 });
529 interceptHook(compiler.cache.hooks.endIdle, 0.01, "cache", "end idle");
530 compiler.hooks.initialize.intercept({
531 name: "ProgressPlugin",
532 call() {
533 handler(0, "");
534 }
535 });
536 interceptHook(compiler.hooks.initialize, 0.01, "setup", "initialize");
537 interceptHook(compiler.hooks.beforeRun, 0.02, "setup", "before run");
538 interceptHook(compiler.hooks.run, 0.03, "setup", "run");
539 interceptHook(compiler.hooks.watchRun, 0.03, "setup", "watch run");
540 interceptHook(
541 compiler.hooks.normalModuleFactory,
542 0.04,
543 "setup",
544 "normal module factory"
545 );
546 interceptHook(
547 compiler.hooks.contextModuleFactory,
548 0.05,
549 "setup",
550 "context module factory"
551 );
552 interceptHook(
553 compiler.hooks.beforeCompile,
554 0.06,
555 "setup",
556 "before compile"
557 );
558 interceptHook(compiler.hooks.compile, 0.07, "setup", "compile");
559 interceptHook(compiler.hooks.thisCompilation, 0.08, "setup", "compilation");
560 interceptHook(compiler.hooks.compilation, 0.09, "setup", "compilation");
561 interceptHook(compiler.hooks.finishMake, 0.69, "building", "finish");
562 interceptHook(compiler.hooks.emit, 0.95, "emitting", "emit");
563 interceptHook(compiler.hooks.afterEmit, 0.98, "emitting", "after emit");
564 interceptHook(compiler.hooks.done, 0.99, "done", "plugins");
565 compiler.hooks.done.intercept({
566 name: "ProgressPlugin",
567 done() {
568 handler(0.99, "");
569 }
570 });
571 interceptHook(
572 compiler.cache.hooks.storeBuildDependencies,
573 0.99,
574 "cache",
575 "store build dependencies"
576 );
577 interceptHook(compiler.cache.hooks.shutdown, 0.99, "cache", "shutdown");
578 interceptHook(compiler.cache.hooks.beginIdle, 0.99, "cache", "begin idle");
579 interceptHook(
580 compiler.hooks.watchClose,
581 0.99,
582 "end",
583 "closing watch compilation"
584 );
585 compiler.cache.hooks.beginIdle.intercept({
586 name: "ProgressPlugin",
587 done() {
588 handler(1, "");
589 }
590 });
591 compiler.cache.hooks.shutdown.intercept({
592 name: "ProgressPlugin",
593 done() {
594 handler(1, "");
595 }
596 });
597 }
598}
599
600ProgressPlugin.defaultOptions = {
601 profile: false,
602 modulesCount: 5000,
603 dependenciesCount: 10000,
604 modules: true,
605 dependencies: true,
606 activeModules: false,
607 entries: true
608};
609
610module.exports = ProgressPlugin;