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