feat: refactoring project
This commit is contained in:
91
node_modules/webpack/lib/stats/DefaultStatsPresetPlugin.js
generated
vendored
91
node_modules/webpack/lib/stats/DefaultStatsPresetPlugin.js
generated
vendored
@@ -11,15 +11,24 @@ const RequestShortener = require("../RequestShortener");
|
||||
/** @typedef {import("../Compilation")} Compilation */
|
||||
/** @typedef {import("../Compilation").CreateStatsOptionsContext} CreateStatsOptionsContext */
|
||||
/** @typedef {import("../Compiler")} Compiler */
|
||||
/** @typedef {import("./DefaultStatsFactoryPlugin").StatsError} StatsError */
|
||||
|
||||
/**
|
||||
* @param {StatsOptions} options options
|
||||
* @param {StatsOptions} defaults default options
|
||||
*/
|
||||
const applyDefaults = (options, defaults) => {
|
||||
for (const key of Object.keys(defaults)) {
|
||||
for (const _k of Object.keys(defaults)) {
|
||||
const key = /** @type {keyof StatsOptions} */ (_k);
|
||||
if (typeof options[key] === "undefined") {
|
||||
options[key] = defaults[key];
|
||||
/** @type {TODO} */
|
||||
(options)[key] = defaults[key];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/** @typedef {Record<string, StatsOptions>} NamedPresets */
|
||||
/** @type {NamedPresets} */
|
||||
const NAMED_PRESETS = {
|
||||
verbose: {
|
||||
hash: true,
|
||||
@@ -126,12 +135,35 @@ const NAMED_PRESETS = {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {StatsOptions} all stats option
|
||||
* @returns {boolean} true when enabled, otherwise false
|
||||
*/
|
||||
const NORMAL_ON = ({ all }) => all !== false;
|
||||
/**
|
||||
* @param {StatsOptions} all stats option
|
||||
* @returns {boolean} true when enabled, otherwise false
|
||||
*/
|
||||
const NORMAL_OFF = ({ all }) => all === true;
|
||||
/**
|
||||
* @param {StatsOptions} all stats option
|
||||
* @param {CreateStatsOptionsContext} forToString stats options context
|
||||
* @returns {boolean} true when enabled, otherwise false
|
||||
*/
|
||||
const ON_FOR_TO_STRING = ({ all }, { forToString }) =>
|
||||
forToString ? all !== false : all === true;
|
||||
/**
|
||||
* @param {StatsOptions} all stats option
|
||||
* @param {CreateStatsOptionsContext} forToString stats options context
|
||||
* @returns {boolean} true when enabled, otherwise false
|
||||
*/
|
||||
const OFF_FOR_TO_STRING = ({ all }, { forToString }) =>
|
||||
forToString ? all === true : all !== false;
|
||||
/**
|
||||
* @param {StatsOptions} all stats option
|
||||
* @param {CreateStatsOptionsContext} forToString stats options context
|
||||
* @returns {boolean | "auto"} true when enabled, otherwise false
|
||||
*/
|
||||
const AUTO_FOR_TO_STRING = ({ all }, { forToString }) => {
|
||||
if (all === false) return false;
|
||||
if (all === true) return true;
|
||||
@@ -139,13 +171,19 @@ const AUTO_FOR_TO_STRING = ({ all }, { forToString }) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
/** @type {Record<string, (options: StatsOptions, context: CreateStatsOptionsContext, compilation: Compilation) => any>} */
|
||||
/** @typedef {Record<string, (options: StatsOptions, context: CreateStatsOptionsContext, compilation: Compilation) => StatsOptions[keyof StatsOptions] | RequestShortener>} Defaults */
|
||||
|
||||
/** @type {Defaults} */
|
||||
const DEFAULTS = {
|
||||
context: (options, context, compilation) => compilation.compiler.context,
|
||||
requestShortener: (options, context, compilation) =>
|
||||
compilation.compiler.context === options.context
|
||||
? compilation.requestShortener
|
||||
: new RequestShortener(options.context, compilation.compiler.root),
|
||||
: new RequestShortener(
|
||||
/** @type {string} */
|
||||
(options.context),
|
||||
compilation.compiler.root
|
||||
),
|
||||
performance: NORMAL_ON,
|
||||
hash: OFF_FOR_TO_STRING,
|
||||
env: NORMAL_OFF,
|
||||
@@ -235,6 +273,10 @@ const DEFAULTS = {
|
||||
colors: () => false
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string | ({ test: function(string): boolean }) | (function(string): boolean) | boolean} item item to normalize
|
||||
* @returns {(function(string): boolean) | undefined} normalize fn
|
||||
*/
|
||||
const normalizeFilter = item => {
|
||||
if (typeof item === "string") {
|
||||
const regExp = new RegExp(
|
||||
@@ -253,6 +295,7 @@ const normalizeFilter = item => {
|
||||
}
|
||||
};
|
||||
|
||||
/** @type {Record<string, function(any): any[]>} */
|
||||
const NORMALIZER = {
|
||||
excludeModules: value => {
|
||||
if (!Array.isArray(value)) {
|
||||
@@ -270,20 +313,32 @@ const NORMALIZER = {
|
||||
if (!Array.isArray(value)) {
|
||||
value = value ? [value] : [];
|
||||
}
|
||||
return value.map(filter => {
|
||||
if (typeof filter === "string") {
|
||||
return (warning, warningString) => warningString.includes(filter);
|
||||
/**
|
||||
* @callback WarningFilterFn
|
||||
* @param {StatsError} warning warning
|
||||
* @param {string} warningString warning string
|
||||
* @returns {boolean} result
|
||||
*/
|
||||
return value.map(
|
||||
/**
|
||||
* @param {StatsOptions["warningsFilter"]} filter a warning filter
|
||||
* @returns {WarningFilterFn} result
|
||||
*/
|
||||
filter => {
|
||||
if (typeof filter === "string") {
|
||||
return (warning, warningString) => warningString.includes(filter);
|
||||
}
|
||||
if (filter instanceof RegExp) {
|
||||
return (warning, warningString) => filter.test(warningString);
|
||||
}
|
||||
if (typeof filter === "function") {
|
||||
return filter;
|
||||
}
|
||||
throw new Error(
|
||||
`Can only filter warnings with Strings or RegExps. (Given: ${filter})`
|
||||
);
|
||||
}
|
||||
if (filter instanceof RegExp) {
|
||||
return (warning, warningString) => filter.test(warningString);
|
||||
}
|
||||
if (typeof filter === "function") {
|
||||
return filter;
|
||||
}
|
||||
throw new Error(
|
||||
`Can only filter warnings with Strings or RegExps. (Given: ${filter})`
|
||||
);
|
||||
});
|
||||
);
|
||||
},
|
||||
logging: value => {
|
||||
if (value === true) value = "log";
|
||||
@@ -306,7 +361,7 @@ class DefaultStatsPresetPlugin {
|
||||
apply(compiler) {
|
||||
compiler.hooks.compilation.tap("DefaultStatsPresetPlugin", compilation => {
|
||||
for (const key of Object.keys(NAMED_PRESETS)) {
|
||||
const defaults = NAMED_PRESETS[key];
|
||||
const defaults = NAMED_PRESETS[/** @type {keyof NamedPresets} */ (key)];
|
||||
compilation.hooks.statsPreset
|
||||
.for(key)
|
||||
.tap("DefaultStatsPresetPlugin", (options, context) => {
|
||||
|
||||
Reference in New Issue
Block a user