feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -7,14 +7,18 @@
const { HookMap, SyncWaterfallHook, SyncBailHook } = require("tapable");
/** @template T @typedef {import("tapable").AsArray<T>} AsArray<T> */
/** @typedef {import("tapable").Hook} Hook */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsAsset} StatsAsset */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsChunk} StatsChunk */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsChunkGroup} StatsChunkGroup */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsError} StatsError */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsLogging} StatsLogging */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsModule} StatsModule */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsModuleIssuer} StatsModuleIssuer */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsModuleReason} StatsModuleReason */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsModuleTraceDependency} StatsModuleTraceDependency */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsModuleTraceItem} StatsModuleTraceItem */
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsProfile} StatsProfile */
/**
* @typedef {object} PrintedElement
@@ -27,53 +31,78 @@ const { HookMap, SyncWaterfallHook, SyncBailHook } = require("tapable");
* @property {string=} type
* @property {StatsCompilation=} compilation
* @property {StatsChunkGroup=} chunkGroup
* @property {string=} chunkGroupKind
* @property {StatsAsset=} asset
* @property {StatsModule=} module
* @property {StatsChunk=} chunk
* @property {StatsModuleReason=} moduleReason
* @property {StatsModuleIssuer=} moduleIssuer
* @property {StatsError=} error
* @property {StatsProfile=} profile
* @property {StatsLogging=} logging
* @property {StatsModuleTraceItem=} moduleTraceItem
* @property {StatsModuleTraceDependency=} moduleTraceDependency
*/
/**
* @typedef {object} KnownStatsPrinterColorFn
* @property {(str: string) => string=} bold
* @property {(str: string) => string=} yellow
* @property {(str: string) => string=} red
* @property {(str: string) => string=} green
* @property {(str: string) => string=} magenta
* @property {(str: string) => string=} cyan
*/
/**
* @typedef {object} KnownStatsPrinterFormaters
* @property {(file: string, oversize?: boolean) => string=} formatFilename
* @property {(id: string) => string=} formatModuleId
* @property {(id: string, direction?: "parent"|"child"|"sibling") => string=} formatChunkId
* @property {(size: number) => string=} formatSize
* @property {(size: string) => string=} formatLayer
* @property {(dateTime: number) => string=} formatDateTime
* @property {(flag: string) => string=} formatFlag
* @property {(time: number, boldQuantity?: boolean) => string=} formatTime
* @property {string=} chunkGroupKind
* @property {(message: string) => string=} formatError
*/
/** @typedef {KnownStatsPrinterContext & Record<string, any>} StatsPrinterContext */
/** @typedef {Record<string, EXPECTED_ANY> & KnownStatsPrinterColorFn & KnownStatsPrinterFormaters & KnownStatsPrinterContext} StatsPrinterContext */
/** @typedef {any} PrintObject */
/**
* @typedef {object} StatsPrintHooks
* @property {HookMap<SyncBailHook<[string[], StatsPrinterContext], void>>} sortElements
* @property {HookMap<SyncBailHook<[PrintedElement[], StatsPrinterContext], string | void>>} printElements
* @property {HookMap<SyncBailHook<[PrintObject[], StatsPrinterContext], boolean | void>>} sortItems
* @property {HookMap<SyncBailHook<[PrintObject, StatsPrinterContext], string | void>>} getItemName
* @property {HookMap<SyncBailHook<[string[], StatsPrinterContext], string | void>>} printItems
* @property {HookMap<SyncBailHook<[PrintObject, StatsPrinterContext], string | void>>} print
* @property {HookMap<SyncWaterfallHook<[string, StatsPrinterContext]>>} result
*/
class StatsPrinter {
constructor() {
/** @type {StatsPrintHooks} */
this.hooks = Object.freeze({
/** @type {HookMap<SyncBailHook<[string[], StatsPrinterContext], true>>} */
sortElements: new HookMap(
() => new SyncBailHook(["elements", "context"])
),
/** @type {HookMap<SyncBailHook<[PrintedElement[], StatsPrinterContext], string>>} */
printElements: new HookMap(
() => new SyncBailHook(["printedElements", "context"])
),
/** @type {HookMap<SyncBailHook<[any[], StatsPrinterContext], true>>} */
sortItems: new HookMap(() => new SyncBailHook(["items", "context"])),
/** @type {HookMap<SyncBailHook<[any, StatsPrinterContext], string>>} */
getItemName: new HookMap(() => new SyncBailHook(["item", "context"])),
/** @type {HookMap<SyncBailHook<[string[], StatsPrinterContext], string>>} */
printItems: new HookMap(
() => new SyncBailHook(["printedItems", "context"])
),
/** @type {HookMap<SyncBailHook<[{}, StatsPrinterContext], string>>} */
print: new HookMap(() => new SyncBailHook(["object", "context"])),
/** @type {HookMap<SyncWaterfallHook<[string, StatsPrinterContext]>>} */
result: new HookMap(() => new SyncWaterfallHook(["result", "context"]))
});
/** @type {Map<HookMap<Hook>, Map<string, Hook[]>>} */
/**
* @type {TODO}
*/
this._levelHookCache = new Map();
this._inPrint = false;
}
@@ -81,15 +110,14 @@ class StatsPrinter {
/**
* get all level hooks
* @private
* @template {Hook} T
* @param {HookMap<T>} hookMap HookMap
* @template {StatsPrintHooks[keyof StatsPrintHooks]} HM
* @template {HM extends HookMap<infer H> ? H : never} H
* @param {HM} hookMap hook map
* @param {string} type type
* @returns {T[]} hooks
* @returns {H[]} hooks
*/
_getAllLevelHooks(hookMap, type) {
let cache = /** @type {Map<string, T[]>} */ (
this._levelHookCache.get(hookMap)
);
let cache = this._levelHookCache.get(hookMap);
if (cache === undefined) {
cache = new Map();
this._levelHookCache.set(hookMap, cache);
@@ -98,11 +126,11 @@ class StatsPrinter {
if (cacheEntry !== undefined) {
return cacheEntry;
}
/** @type {T[]} */
/** @type {H[]} */
const hooks = [];
const typeParts = type.split(".");
for (let i = 0; i < typeParts.length; i++) {
const hook = hookMap.get(typeParts.slice(i).join("."));
const hook = /** @type {H} */ (hookMap.get(typeParts.slice(i).join(".")));
if (hook) {
hooks.push(hook);
}
@@ -114,16 +142,17 @@ class StatsPrinter {
/**
* Run `fn` for each level
* @private
* @template T
* @template R
* @param {HookMap<SyncBailHook<T, R>>} hookMap HookMap
* @template {StatsPrintHooks[keyof StatsPrintHooks]} HM
* @template {HM extends HookMap<infer H> ? H : never} H
* @template {H extends import("tapable").Hook<any, infer R> ? R : never} R
* @param {HM} hookMap hook map
* @param {string} type type
* @param {(hook: SyncBailHook<T, R>) => R} fn function
* @returns {R} result of `fn`
* @param {function(H): R | void} fn fn
* @returns {R | void} hook
*/
_forEachLevel(hookMap, type, fn) {
for (const hook of this._getAllLevelHooks(hookMap, type)) {
const result = fn(hook);
const result = fn(/** @type {H} */ (hook));
if (result !== undefined) return result;
}
}
@@ -131,48 +160,49 @@ class StatsPrinter {
/**
* Run `fn` for each level
* @private
* @template T
* @param {HookMap<SyncWaterfallHook<T>>} hookMap HookMap
* @template {StatsPrintHooks[keyof StatsPrintHooks]} HM
* @template {HM extends HookMap<infer H> ? H : never} H
* @param {HM} hookMap hook map
* @param {string} type type
* @param {AsArray<T>[0]} data data
* @param {(hook: SyncWaterfallHook<T>, data: AsArray<T>[0]) => AsArray<T>[0]} fn function
* @returns {AsArray<T>[0]} result of `fn`
* @param {string} data data
* @param {function(H, string): string} fn fn
* @returns {string} result of `fn`
*/
_forEachLevelWaterfall(hookMap, type, data, fn) {
for (const hook of this._getAllLevelHooks(hookMap, type)) {
data = fn(hook, data);
data = fn(/** @type {H} */ (hook), data);
}
return data;
}
/**
* @param {string} type The type
* @param {object} object Object to print
* @param {object=} baseContext The base context
* @param {PrintObject} object Object to print
* @param {StatsPrinterContext=} baseContext The base context
* @returns {string} printed result
*/
print(type, object, baseContext) {
if (this._inPrint) {
return this._print(type, object, baseContext);
} else {
try {
this._inPrint = true;
return this._print(type, object, baseContext);
} finally {
this._levelHookCache.clear();
this._inPrint = false;
}
}
try {
this._inPrint = true;
return this._print(type, object, baseContext);
} finally {
this._levelHookCache.clear();
this._inPrint = false;
}
}
/**
* @private
* @param {string} type type
* @param {object} object object
* @param {object=} baseContext context
* @param {PrintObject} object object
* @param {StatsPrinterContext=} baseContext context
* @returns {string} printed result
*/
_print(type, object, baseContext) {
/** @type {StatsPrinterContext} */
const context = {
...baseContext,
type,
@@ -189,6 +219,7 @@ class StatsPrinter {
h.call(sortedItems, context)
);
const printedItems = sortedItems.map((item, i) => {
/** @type {StatsPrinterContext} */
const itemContext = {
...context,
_index: i
@@ -241,7 +272,7 @@ class StatsPrinter {
return this._forEachLevelWaterfall(
this.hooks.result,
type,
printResult,
/** @type {string} */ (printResult),
(h, r) => h.call(r, context)
);
}