Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export default handler creator for Progress plugin #17312

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 71 additions & 15 deletions lib/ProgressPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@ const NormalModule = require("./NormalModule");
const createSchemaValidation = require("./util/create-schema-validation");
const { contextify } = require("./util/identifier");

/** @typedef {import("tapable").Tap} Tap */
/** @typedef {import("../declarations/plugins/ProgressPlugin").HandlerFunction} HandlerFunction */
/** @typedef {import("../declarations/plugins/ProgressPlugin").ProgressPluginArgument} ProgressPluginArgument */
/** @typedef {import("../declarations/plugins/ProgressPlugin").ProgressPluginOptions} ProgressPluginOptions */
/** @typedef {import("./Dependency")} Dependency */
/** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./logging/Logger").Logger} Logger */

/**
* @typedef {Object} CountsData
* @property {number} modulesCount modules count
* @property {number} dependenciesCount dependencies count
*/

const validate = createSchemaValidation(
require("../schemas/plugins/ProgressPlugin.check.js"),
Expand All @@ -23,14 +34,31 @@ const validate = createSchemaValidation(
baseDataPath: "options"
}
);

/**
* @param {number} a a
* @param {number} b b
* @param {number} c c
* @returns {number} median
*/
const median3 = (a, b, c) => {
return a + b + c - Math.max(a, b, c) - Math.min(a, b, c);
};

/**
* @param {boolean | null | undefined} profile need profile
* @param {Logger} logger logger
* @returns {defaultHandler} default handler
*/
const createDefaultHandler = (profile, logger) => {
/** @type {{ value: string, time: number }[]} */
/** @type {{ value: string | undefined, time: number }[]} */
const lastStateInfo = [];

/**
* @param {number} percentage percentage
* @param {string} msg message
* @param {...string} args additional arguments
*/
const defaultHandler = (percentage, msg, ...args) => {
if (profile) {
if (percentage === 0) {
Expand Down Expand Up @@ -95,18 +123,18 @@ const createDefaultHandler = (profile, logger) => {

/**
* @callback ReportProgress
* @param {number} p
* @param {...string} [args]
* @param {number} p percentage
* @param {...string} args additional arguments
* @returns {void}
*/

/** @type {WeakMap<Compiler,ReportProgress>} */
/** @type {WeakMap<Compiler, ReportProgress | undefined>} */
const progressReporters = new WeakMap();

class ProgressPlugin {
/**
* @param {Compiler} compiler the current compiler
* @returns {ReportProgress} a progress reporter, if any
* @returns {ReportProgress | undefined} a progress reporter, if any
*/
static getReporter(compiler) {
return progressReporters.get(compiler);
Expand Down Expand Up @@ -288,6 +316,9 @@ class ProgressPlugin {
};

// only used when showActiveModules is set
/**
* @param {Module} module the module
*/
const moduleBuild = module => {
const ident = module.identifier();
if (ident) {
Expand All @@ -297,11 +328,18 @@ class ProgressPlugin {
}
};

/**
* @param {Dependency} entry entry dependency
* @param {EntryOptions} options options object
*/
const entryAdd = (entry, options) => {
entriesCount++;
if (entriesCount < 5 || entriesCount % 10 === 0) updateThrottled();
};

/**
* @param {Module} module the module
*/
const moduleDone = module => {
doneModules++;
if (showActiveModules) {
Expand All @@ -321,6 +359,10 @@ class ProgressPlugin {
if (doneModules < 50 || doneModules % 100 === 0) updateThrottled();
};

/**
* @param {Dependency} entry entry dependency
* @param {EntryOptions} options options object
*/
const entryDone = (entry, options) => {
doneEntries++;
update();
Expand All @@ -330,6 +372,7 @@ class ProgressPlugin {
.getCache("ProgressPlugin")
.getItemCache("counts", null);

/** @type {Promise<CountsData> | undefined} */
let cacheGetPromise;

compiler.hooks.beforeCompile.tap("ProgressPlugin", () => {
Expand All @@ -352,15 +395,17 @@ class ProgressPlugin {

compiler.hooks.afterCompile.tapPromise("ProgressPlugin", compilation => {
if (compilation.compiler.isChild()) return Promise.resolve();
return cacheGetPromise.then(async oldData => {
if (
!oldData ||
oldData.modulesCount !== modulesCount ||
oldData.dependenciesCount !== dependenciesCount
) {
await cache.storePromise({ modulesCount, dependenciesCount });
return /** @type {Promise<CountsData>} */ (cacheGetPromise).then(
async oldData => {
if (
!oldData ||
oldData.modulesCount !== modulesCount ||
oldData.dependenciesCount !== dependenciesCount
) {
await cache.storePromise({ modulesCount, dependenciesCount });
}
}
});
);
});

compiler.hooks.compilation.tap("ProgressPlugin", compilation => {
Expand Down Expand Up @@ -463,9 +508,9 @@ class ProgressPlugin {
};
const numberOfHooks = Object.keys(hooks).length;
Object.keys(hooks).forEach((name, idx) => {
const title = hooks[name];
const title = hooks[/** @type {keyof typeof hooks} */ (name)];
const percentage = (idx / numberOfHooks) * 0.25 + 0.7;
compilation.hooks[name].intercept({
compilation.hooks[/** @type {keyof typeof hooks} */ (name)].intercept({
name: "ProgressPlugin",
call() {
handler(percentage, "sealing", title);
Expand Down Expand Up @@ -500,6 +545,12 @@ class ProgressPlugin {
handler(0.65, "building");
}
});
/**
* @param {TODO} hook hook
* @param {number} progress progress from 0 to 1
* @param {string} category category
* @param {string} name name
*/
const interceptHook = (hook, progress, category, name) => {
hook.intercept({
name: "ProgressPlugin",
Expand All @@ -516,6 +567,9 @@ class ProgressPlugin {
error() {
handler(progress, category, name);
},
/**
* @param {Tap} tap tap
*/
tap(tap) {
progressReporters.set(compiler, (p, ...args) => {
handler(progress, category, name, tap.name, ...args);
Expand Down Expand Up @@ -610,4 +664,6 @@ ProgressPlugin.defaultOptions = {
entries: true
};

ProgressPlugin.createDefaultHandler = createDefaultHandler;

module.exports = ProgressPlugin;
6 changes: 5 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9763,7 +9763,7 @@ declare class ProgressPlugin {
apply(compiler: Compiler | MultiCompiler): void;
static getReporter(
compiler: Compiler
): (p: number, ...args: string[]) => void;
): undefined | ((p: number, ...args: string[]) => void);
static defaultOptions: {
profile: boolean;
modulesCount: number;
Expand All @@ -9773,6 +9773,10 @@ declare class ProgressPlugin {
activeModules: boolean;
entries: boolean;
};
static createDefaultHandler: (
profile: undefined | null | boolean,
logger: WebpackLogger
) => (percentage: number, msg: string, ...args: string[]) => void;
}
type ProgressPluginArgument =
| ProgressPluginOptions
Expand Down