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

@@ -6,7 +6,10 @@
"use strict";
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("./DefaultStatsFactoryPlugin").KnownStatsChunkGroup} KnownStatsChunkGroup */
/** @typedef {import("./StatsPrinter")} StatsPrinter */
/** @typedef {import("./StatsPrinter").KnownStatsPrinterColorFn} KnownStatsPrinterColorFn */
/** @typedef {import("./StatsPrinter").KnownStatsPrinterFormaters} KnownStatsPrinterFormaters */
/** @typedef {import("./StatsPrinter").StatsPrinterContext} StatsPrinterContext */
const DATA_URI_CONTENT_LENGTH = 16;
@@ -22,9 +25,8 @@ const plural = (n, singular, plural) => (n === 1 ? singular : plural);
/**
* @param {Record<string, number>} sizes sizes by source type
* @param {object} options options
* @param {(number) => string=} options.formatSize size formatter
* @returns {string} text
* @param {StatsPrinterContext} options options
* @returns {string | undefined} text
*/
const printSizes = (sizes, { formatSize = n => `${n}` }) => {
const keys = Object.keys(sizes);
@@ -56,7 +58,9 @@ const getResourceName = resource => {
* @returns {[string,string]} prefix and module name
*/
const getModuleName = name => {
const [, prefix, resource] = /^(.*!)?([^!]*)$/.exec(name);
const [, prefix, resource] =
/** @type {[any, string, string]} */
(/** @type {unknown} */ (/^(.*!)?([^!]*)$/.exec(name)));
if (resource.length > MAX_MODULE_IDENTIFIER_LENGTH) {
const truncatedResource = `${resource.slice(
@@ -86,22 +90,29 @@ const mapLines = (str, fn) => str.split("\n").map(fn).join("\n");
*/
const twoDigit = n => (n >= 10 ? `${n}` : `0${n}`);
const isValidId = id => {
return typeof id === "number" || id;
};
/**
* @param {string | number} id an id
* @returns {boolean | string} is i
*/
const isValidId = id => typeof id === "number" || id;
/**
* @template T
* @param {Array<T>} list of items
* @param {Array<T> | undefined} list of items
* @param {number} count number of items to show
* @returns {string} string representation of list
*/
const moreCount = (list, count) => {
return list && list.length > 0 ? `+ ${count}` : `${count}`;
};
const moreCount = (list, count) =>
list && list.length > 0 ? `+ ${count}` : `${count}`;
/** @type {Record<string, (thing: any, context: StatsPrinterContext, printer: StatsPrinter) => string | void>} */
const SIMPLE_PRINTERS = {
/**
* @template T
* @template {keyof T} K
* @typedef {{ [P in K]-?: T[P] }} WithRequired
*/
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation">, printer: StatsPrinter) => string | undefined>} */
const COMPILATION_SIMPLE_PRINTERS = {
"compilation.summary!": (
_,
{
@@ -125,14 +136,16 @@ const SIMPLE_PRINTERS = {
) => {
const root = type === "compilation.summary!";
const warningsMessage =
warningsCount > 0
/** @type {number} */ (warningsCount) > 0
? yellow(
`${warningsCount} ${plural(warningsCount, "warning", "warnings")}`
`${warningsCount} ${plural(/** @type {number} */ (warningsCount), "warning", "warnings")}`
)
: "";
const errorsMessage =
errorsCount > 0
? red(`${errorsCount} ${plural(errorsCount, "error", "errors")}`)
/** @type {number} */ (errorsCount) > 0
? red(
`${errorsCount} ${plural(/** @type {number} */ (errorsCount), "error", "errors")}`
)
: "";
const timeMessage = root && time ? ` in ${formatTime(time)}` : "";
const hashMessage = hash ? ` (${hash})` : "";
@@ -161,7 +174,7 @@ const SIMPLE_PRINTERS = {
} else if (errorsCount === 0 && warningsCount === 0) {
statusMessage = `compiled ${green("successfully")}`;
} else {
statusMessage = `compiled`;
statusMessage = "compiled";
}
if (
builtAtMessage ||
@@ -258,11 +271,12 @@ const SIMPLE_PRINTERS = {
"compilation.warningsInChildren!": (_, { yellow, compilation }) => {
if (
!compilation.children &&
compilation.warningsCount > 0 &&
/** @type {number} */ (compilation.warningsCount) > 0 &&
compilation.warnings
) {
const childWarnings =
compilation.warningsCount - compilation.warnings.length;
/** @type {number} */ (compilation.warningsCount) -
compilation.warnings.length;
if (childWarnings > 0) {
return yellow(
`${childWarnings} ${plural(
@@ -281,10 +295,12 @@ const SIMPLE_PRINTERS = {
"compilation.errorsInChildren!": (_, { red, compilation }) => {
if (
!compilation.children &&
compilation.errorsCount > 0 &&
/** @type {number} */ (compilation.errorsCount) > 0 &&
compilation.errors
) {
const childErrors = compilation.errorsCount - compilation.errors.length;
const childErrors =
/** @type {number} */ (compilation.errorsCount) -
compilation.errors.length;
if (childErrors > 0) {
return red(
`${childErrors} ${plural(
@@ -299,15 +315,16 @@ const SIMPLE_PRINTERS = {
);
}
}
},
}
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "asset">, printer: StatsPrinter) => string | undefined>} */
const ASSET_SIMPLE_PRINTERS = {
"asset.type": type => type,
"asset.name": (name, { formatFilename, asset: { isOverSizeLimit } }) =>
formatFilename(name, isOverSizeLimit),
"asset.size": (
size,
{ asset: { isOverSizeLimit }, yellow, green, formatSize }
) => (isOverSizeLimit ? yellow(formatSize(size)) : formatSize(size)),
"asset.size": (size, { asset: { isOverSizeLimit }, yellow, formatSize }) =>
isOverSizeLimit ? yellow(formatSize(size)) : formatSize(size),
"asset.emitted": (emitted, { green, formatFlag }) =>
emitted ? green(formatFlag("emitted")) : undefined,
"asset.comparedForEmit": (comparedForEmit, { yellow, formatFlag }) =>
@@ -356,8 +373,11 @@ const SIMPLE_PRINTERS = {
assetChunk: (id, { formatChunkId }) => formatChunkId(id),
assetChunkName: name => name,
assetChunkIdHint: name => name,
assetChunkIdHint: name => name
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "module">, printer: StatsPrinter) => string | undefined>} */
const MODULE_SIMPLE_PRINTERS = {
"module.type": type => (type !== "module" ? type : undefined),
"module.id": (id, { formatModuleId }) =>
isValidId(id) ? formatModuleId(id) : undefined,
@@ -433,11 +453,11 @@ const SIMPLE_PRINTERS = {
providedExportsCount === usedExports.length
) {
return cyan(formatFlag("all exports used"));
} else {
return cyan(
formatFlag(`only some exports used: ${usedExports.join(", ")}`)
);
}
return cyan(
formatFlag(`only some exports used: ${usedExports.join(", ")}`)
);
}
}
},
@@ -470,11 +490,17 @@ const SIMPLE_PRINTERS = {
"modules"
)}`
: undefined,
"module.separator!": () => "\n",
"module.separator!": () => "\n"
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "moduleIssuer">, printer: StatsPrinter) => string | undefined>} */
const MODULE_ISSUER_PRINTERS = {
"moduleIssuer.id": (id, { formatModuleId }) => formatModuleId(id),
"moduleIssuer.profile.total": (value, { formatTime }) => formatTime(value),
"moduleIssuer.profile.total": (value, { formatTime }) => formatTime(value)
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "moduleReason">, printer: StatsPrinter) => string | undefined>} */
const MODULE_REASON_PRINTERS = {
"moduleReason.type": type => type,
"moduleReason.userRequest": (userRequest, { cyan }) =>
cyan(getResourceName(userRequest)),
@@ -496,8 +522,11 @@ const SIMPLE_PRINTERS = {
"reason",
"reasons"
)}`
: undefined,
: undefined
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "profile">, printer: StatsPrinter) => string | undefined>} */
const MODULE_PROFILE_PRINTERS = {
"module.profile.total": (value, { formatTime }) => formatTime(value),
"module.profile.resolving": (value, { formatTime }) =>
`resolving: ${formatTime(value)}`,
@@ -512,8 +541,11 @@ const SIMPLE_PRINTERS = {
"module.profile.additionalResolving": (value, { formatTime }) =>
value ? `additional resolving: ${formatTime(value)}` : undefined,
"module.profile.additionalIntegration": (value, { formatTime }) =>
value ? `additional integration: ${formatTime(value)}` : undefined,
value ? `additional integration: ${formatTime(value)}` : undefined
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "chunkGroupKind" | "chunkGroup">, printer: StatsPrinter) => string | undefined>} */
const CHUNK_GROUP_PRINTERS = {
"chunkGroup.kind!": (_, { chunkGroupKind }) => chunkGroupKind,
"chunkGroup.separator!": () => "\n",
"chunkGroup.name": (name, { bold }) => bold(name),
@@ -541,10 +573,11 @@ const SIMPLE_PRINTERS = {
"chunkGroup.is!": () => "=",
"chunkGroupAsset.name": (asset, { green }) => green(asset),
"chunkGroupAsset.size": (size, { formatSize, chunkGroup }) =>
chunkGroup.assets.length > 1 ||
chunkGroup.assets &&
(chunkGroup.assets.length > 1 ||
(chunkGroup.auxiliaryAssets && chunkGroup.auxiliaryAssets.length > 0)
? formatSize(size)
: undefined,
: undefined),
"chunkGroup.children": (children, context, printer) =>
Array.isArray(children)
? undefined
@@ -560,8 +593,11 @@ const SIMPLE_PRINTERS = {
"chunkGroupChild.assets[]": (file, { formatFilename }) =>
formatFilename(file),
"chunkGroupChild.chunks[]": (id, { formatChunkId }) => formatChunkId(id),
"chunkGroupChild.name": name => (name ? `(name: ${name})` : undefined),
"chunkGroupChild.name": name => (name ? `(name: ${name})` : undefined)
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "chunk">, printer: StatsPrinter) => string | undefined>} */
const CHUNK_PRINTERS = {
"chunk.id": (id, { formatChunkId }) => formatChunkId(id),
"chunk.files[]": (file, { formatFilename }) => formatFilename(file),
"chunk.names[]": name => name,
@@ -611,8 +647,11 @@ const SIMPLE_PRINTERS = {
"chunkOrigin.moduleId": (moduleId, { formatModuleId }) =>
isValidId(moduleId) ? formatModuleId(moduleId) : undefined,
"chunkOrigin.moduleName": (moduleName, { bold }) => bold(moduleName),
"chunkOrigin.loc": loc => loc,
"chunkOrigin.loc": loc => loc
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "error">, printer: StatsPrinter) => string | undefined>} */
const ERROR_PRINTERS = {
"error.compilerPath": (compilerPath, { bold }) =>
compilerPath ? bold(`(${compilerPath})`) : undefined,
"error.chunkId": (chunkId, { formatChunkId }) =>
@@ -622,21 +661,23 @@ const SIMPLE_PRINTERS = {
"error.chunkInitial": (chunkInitial, { formatFlag }) =>
chunkInitial ? formatFlag("initial") : undefined,
"error.file": (file, { bold }) => bold(file),
"error.moduleName": (moduleName, { bold }) => {
return moduleName.includes("!")
"error.moduleName": (moduleName, { bold }) =>
moduleName.includes("!")
? `${bold(moduleName.replace(/^(\s|\S)*!/, ""))} (${moduleName})`
: `${bold(moduleName)}`;
},
: `${bold(moduleName)}`,
"error.loc": (loc, { green }) => green(loc),
"error.message": (message, { bold, formatError }) =>
message.includes("\u001b[") ? message : bold(formatError(message)),
message.includes("\u001B[") ? message : bold(formatError(message)),
"error.details": (details, { formatError }) => formatError(details),
"error.filteredDetails": filteredDetails =>
filteredDetails ? `+ ${filteredDetails} hidden lines` : undefined,
"error.stack": stack => stack,
"error.moduleTrace": moduleTrace => undefined,
"error.separator!": () => "\n",
"error.separator!": () => "\n"
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "logging">, printer: StatsPrinter) => string | undefined>} */
const LOG_ENTRY_PRINTERS = {
"loggingEntry(error).loggingEntry.message": (message, { red }) =>
mapLines(message, x => `<e> ${red(x)}`),
"loggingEntry(warn).loggingEntry.message": (message, { yellow }) =>
@@ -666,20 +707,26 @@ const SIMPLE_PRINTERS = {
"loggingEntry.trace[]": trace =>
trace ? mapLines(trace, x => `| ${x}`) : undefined,
"moduleTraceItem.originName": originName => originName,
loggingGroup: loggingGroup =>
loggingGroup.entries.length === 0 ? "" : undefined,
"loggingGroup.debug": (flag, { red }) => (flag ? red("DEBUG") : undefined),
"loggingGroup.name": (name, { bold }) => bold(`LOG from ${name}`),
"loggingGroup.separator!": () => "\n",
"loggingGroup.filteredEntries": filteredEntries =>
filteredEntries > 0 ? `+ ${filteredEntries} hidden lines` : undefined,
filteredEntries > 0 ? `+ ${filteredEntries} hidden lines` : undefined
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "moduleTraceItem">, printer: StatsPrinter) => string | undefined>} */
const MODULE_TRACE_ITEM_PRINTERS = {
"moduleTraceItem.originName": originName => originName
};
/** @type {Record<string, (thing: any, context: Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "moduleTraceDependency">, printer: StatsPrinter) => string | undefined>} */
const MODULE_TRACE_DEPENDENCY_PRINTERS = {
"moduleTraceDependency.loc": loc => loc
};
/** @type {Record<string, string | Function>} */
/** @type {Record<string, string | function(any): string>} */
const ITEM_NAMES = {
"compilation.assets[]": "asset",
"compilation.modules[]": "module",
@@ -908,19 +955,27 @@ const PREFERRED_ORDERS = {
loggingEntry: ["message", "trace", "children"]
};
/** @typedef {(items: string[]) => string | undefined} SimpleItemsJoiner */
/** @type {SimpleItemsJoiner} */
const itemsJoinOneLine = items => items.filter(Boolean).join(" ");
/** @type {SimpleItemsJoiner} */
const itemsJoinOneLineBrackets = items =>
items.length > 0 ? `(${items.filter(Boolean).join(" ")})` : undefined;
/** @type {SimpleItemsJoiner} */
const itemsJoinMoreSpacing = items => items.filter(Boolean).join("\n\n");
/** @type {SimpleItemsJoiner} */
const itemsJoinComma = items => items.filter(Boolean).join(", ");
/** @type {SimpleItemsJoiner} */
const itemsJoinCommaBrackets = items =>
items.length > 0 ? `(${items.filter(Boolean).join(", ")})` : undefined;
/** @type {function(string): SimpleItemsJoiner} */
const itemsJoinCommaBracketsWithName = name => items =>
items.length > 0
? `(${name}: ${items.filter(Boolean).join(", ")})`
: undefined;
/** @type {Record<string, (items: string[]) => string>} */
/** @type {Record<string, SimpleItemsJoiner>} */
const SIMPLE_ITEMS_JOINER = {
"chunk.parents": itemsJoinOneLine,
"chunk.siblings": itemsJoinOneLine,
@@ -952,18 +1007,27 @@ const SIMPLE_ITEMS_JOINER = {
"compilation.errors": itemsJoinMoreSpacing,
"compilation.warnings": itemsJoinMoreSpacing,
"compilation.logging": itemsJoinMoreSpacing,
"compilation.children": items => indent(itemsJoinMoreSpacing(items), " "),
"compilation.children": items =>
indent(/** @type {string} */ (itemsJoinMoreSpacing(items)), " "),
"moduleTraceItem.dependencies": itemsJoinOneLine,
"loggingEntry.children": items =>
indent(items.filter(Boolean).join("\n"), " ", false)
};
/**
* @param {Item[]} items items
* @returns {string} result
*/
const joinOneLine = items =>
items
.map(item => item.content)
.filter(Boolean)
.join(" ");
/**
* @param {Item[]} items items
* @returns {string} result
*/
const joinInBrackets = items => {
const res = [];
let mode = 0;
@@ -1006,13 +1070,24 @@ const joinInBrackets = items => {
return res.join("");
};
/**
* @param {string} str a string
* @param {string} prefix prefix
* @param {boolean=} noPrefixInFirstLine need prefix in the first line?
* @returns {string} result
*/
const indent = (str, prefix, noPrefixInFirstLine) => {
const rem = str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
const rem = str.replace(/\n([^\n])/g, `\n${prefix}$1`);
if (noPrefixInFirstLine) return rem;
const ind = str[0] === "\n" ? "" : prefix;
return ind + rem;
};
/**
* @param {(false | Item)[]} items items
* @param {string} indenter indenter
* @returns {string} result
*/
const joinExplicitNewLine = (items, indenter) => {
let firstInLine = true;
let first = true;
@@ -1027,22 +1102,34 @@ const joinExplicitNewLine = (items, indenter) => {
first = false;
const noJoiner = firstInLine || content.startsWith("\n");
firstInLine = content.endsWith("\n");
return noJoiner ? content : " " + content;
return noJoiner ? content : ` ${content}`;
})
.filter(Boolean)
.join("")
.trim();
};
/**
* @param {boolean} error is an error
* @returns {SimpleElementJoiner} joiner
*/
const joinError =
error =>
/**
* @param {Item[]} items items
* @param {Required<StatsPrinterContext>} ctx context
* @returns {string} result
*/
(items, { red, yellow }) =>
`${error ? red("ERROR") : yellow("WARNING")} in ${joinExplicitNewLine(
items,
""
)}`;
/** @type {Record<string, (items: ({ element: string, content: string })[], context: StatsPrinterContext) => string>} */
/** @typedef {{ element: string, content: string }} Item */
/** @typedef {(items: Item[], context: Required<StatsPrinterContext>) => string} SimpleElementJoiner */
/** @type {Record<string, SimpleElementJoiner>} */
const SIMPLE_ELEMENT_JOINERS = {
compilation: items => {
const result = [];
@@ -1119,23 +1206,20 @@ const SIMPLE_ELEMENT_JOINERS = {
},
chunk: items => {
let hasEntry = false;
return (
"chunk " +
joinExplicitNewLine(
items.filter(item => {
switch (item.element) {
case "entry":
if (item.content) hasEntry = true;
break;
case "initial":
if (hasEntry) return false;
break;
}
return true;
}),
" "
)
);
return `chunk ${joinExplicitNewLine(
items.filter(item => {
switch (item.element) {
case "entry":
if (item.content) hasEntry = true;
break;
case "initial":
if (hasEntry) return false;
break;
}
return true;
}),
" "
)}`;
},
"chunk.childrenByOrder[]": items => `(${joinOneLine(items)})`,
chunkGroup: items => joinExplicitNewLine(items, " "),
@@ -1196,23 +1280,27 @@ const SIMPLE_ELEMENT_JOINERS = {
},
"module.profile": joinInBrackets,
moduleIssuer: joinOneLine,
chunkOrigin: items => "> " + joinOneLine(items),
chunkOrigin: items => `> ${joinOneLine(items)}`,
"errors[].error": joinError(true),
"warnings[].error": joinError(false),
loggingGroup: items => joinExplicitNewLine(items, "").trimEnd(),
moduleTraceItem: items => " @ " + joinOneLine(items),
moduleTraceItem: items => ` @ ${joinOneLine(items)}`,
moduleTraceDependency: joinOneLine
};
/** @typedef {"bold" | "yellow" | "red" | "green" | "cyan" | "magenta"} ColorNames */
/** @type {Record<ColorNames, string>} */
const AVAILABLE_COLORS = {
bold: "\u001b[1m",
yellow: "\u001b[1m\u001b[33m",
red: "\u001b[1m\u001b[31m",
green: "\u001b[1m\u001b[32m",
cyan: "\u001b[1m\u001b[36m",
magenta: "\u001b[1m\u001b[35m"
bold: "\u001B[1m",
yellow: "\u001B[1m\u001B[33m",
red: "\u001B[1m\u001B[31m",
green: "\u001B[1m\u001B[32m",
cyan: "\u001B[1m\u001B[36m",
magenta: "\u001B[1m\u001B[35m"
};
/** @type {Record<string, function(any, Required<KnownStatsPrinterColorFn> & StatsPrinterContext, ...any): string>} */
const AVAILABLE_FORMATS = {
formatChunkId: (id, { yellow }, direction) => {
switch (direction) {
@@ -1256,13 +1344,12 @@ const AVAILABLE_FORMATS = {
else if (time < times[2]) return bold(`${time}${unit}`);
else if (time < times[1]) return green(`${time}${unit}`);
else if (time < times[0]) return yellow(`${time}${unit}`);
else return red(`${time}${unit}`);
} else {
return `${boldQuantity ? bold(time) : time}${unit}`;
return red(`${time}${unit}`);
}
return `${boldQuantity ? bold(time) : time}${unit}`;
},
formatError: (message, { green, yellow, red }) => {
if (message.includes("\u001b[")) return message;
if (message.includes("\u001B[")) return message;
const highlights = [
{ regExp: /(Did you mean .+)/g, format: green },
{
@@ -1290,23 +1377,36 @@ const AVAILABLE_FORMATS = {
}
];
for (const { regExp, format } of highlights) {
message = message.replace(regExp, (match, content) => {
return match.replace(content, format(content));
});
message = message.replace(
regExp,
/**
* @param {string} match match
* @param {string} content content
* @returns {string} result
*/
(match, content) => match.replace(content, format(content))
);
}
return message;
}
};
/** @typedef {function(string): string} ResultModifierFn */
/** @type {Record<string, ResultModifierFn>} */
const RESULT_MODIFIER = {
"module.modules": result => {
return indent(result, "| ");
}
"module.modules": result => indent(result, "| ")
};
/**
* @param {string[]} array array
* @param {string[]} preferredOrder preferred order
* @returns {string[]} result
*/
const createOrder = (array, preferredOrder) => {
const originalArray = array.slice();
/** @type {Set<string>} */
const set = new Set(array);
/** @type {Set<string>} */
const usedSet = new Set();
array.length = 0;
for (const element of preferredOrder) {
@@ -1333,49 +1433,218 @@ class DefaultStatsPrinterPlugin {
compiler.hooks.compilation.tap("DefaultStatsPrinterPlugin", compilation => {
compilation.hooks.statsPrinter.tap(
"DefaultStatsPrinterPlugin",
(stats, options, context) => {
(stats, options) => {
// Put colors into context
stats.hooks.print
.for("compilation")
.tap("DefaultStatsPrinterPlugin", (compilation, context) => {
for (const color of Object.keys(AVAILABLE_COLORS)) {
const name = /** @type {ColorNames} */ (color);
/** @type {string | undefined} */
let start;
if (options.colors) {
if (
typeof options.colors === "object" &&
typeof options.colors[color] === "string"
typeof options.colors[name] === "string"
) {
start = options.colors[color];
start = options.colors[name];
} else {
start = AVAILABLE_COLORS[color];
start = AVAILABLE_COLORS[name];
}
}
if (start) {
/**
* @param {string} str string
* @returns {string} string with color
*/
context[color] = str =>
`${start}${
typeof str === "string"
? str.replace(
/((\u001b\[39m|\u001b\[22m|\u001b\[0m)+)/g,
/((\u001B\[39m|\u001B\[22m|\u001B\[0m)+)/g,
`$1${start}`
)
: str
}\u001b[39m\u001b[22m`;
}\u001B[39m\u001B[22m`;
} else {
/**
* @param {string} str string
* @returns {string} str string
*/
context[color] = str => str;
}
}
for (const format of Object.keys(AVAILABLE_FORMATS)) {
context[format] = (content, ...args) =>
AVAILABLE_FORMATS[format](content, context, ...args);
context[format] =
/**
* @param {string | number} content content
* @param {...TODO} args args
* @returns {string} result
*/
(content, ...args) =>
AVAILABLE_FORMATS[format](
content,
/** @type {Required<KnownStatsPrinterColorFn> & StatsPrinterContext} */
(context),
...args
);
}
context.timeReference = compilation.time;
});
for (const key of Object.keys(SIMPLE_PRINTERS)) {
for (const key of Object.keys(COMPILATION_SIMPLE_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
SIMPLE_PRINTERS[key](obj, ctx, stats)
COMPILATION_SIMPLE_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(ASSET_SIMPLE_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
ASSET_SIMPLE_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "asset">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(MODULE_SIMPLE_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
MODULE_SIMPLE_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "module">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(MODULE_ISSUER_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
MODULE_ISSUER_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "moduleIssuer">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(MODULE_REASON_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
MODULE_REASON_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "moduleReason">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(MODULE_PROFILE_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
MODULE_PROFILE_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "profile">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(CHUNK_GROUP_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
CHUNK_GROUP_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "chunkGroupKind" | "chunkGroup">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(CHUNK_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
CHUNK_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "chunk">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(ERROR_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
ERROR_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "error">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(LOG_ENTRY_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
LOG_ENTRY_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "logging">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(MODULE_TRACE_DEPENDENCY_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
MODULE_TRACE_DEPENDENCY_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "moduleTraceDependency">} */
(ctx),
stats
)
);
}
for (const key of Object.keys(MODULE_TRACE_ITEM_PRINTERS)) {
stats.hooks.print
.for(key)
.tap("DefaultStatsPrinterPlugin", (obj, ctx) =>
MODULE_TRACE_ITEM_PRINTERS[key](
obj,
/** @type {Required<KnownStatsPrinterColorFn> & Required<KnownStatsPrinterFormaters> & WithRequired<StatsPrinterContext, "type" | "compilation" | "moduleTraceItem">} */
(ctx),
stats
)
);
}
@@ -1409,7 +1678,7 @@ class DefaultStatsPrinterPlugin {
const joiner = SIMPLE_ELEMENT_JOINERS[key];
stats.hooks.printElements
.for(key)
.tap("DefaultStatsPrinterPlugin", joiner);
.tap("DefaultStatsPrinterPlugin", /** @type {TODO} */ (joiner));
}
for (const key of Object.keys(RESULT_MODIFIER)) {