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

@@ -5,6 +5,7 @@
"use strict";
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
/** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
class BasicEffectRulePlugin {
@@ -28,7 +29,8 @@ class BasicEffectRulePlugin {
if (unhandledProperties.has(this.ruleProperty)) {
unhandledProperties.delete(this.ruleProperty);
const value = rule[this.ruleProperty];
const value =
rule[/** @type {keyof RuleSetRule} */ (this.ruleProperty)];
result.effects.push({
type: this.effectType,

View File

@@ -5,6 +5,7 @@
"use strict";
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
/** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
/** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */
@@ -30,7 +31,8 @@ class BasicMatcherRulePlugin {
(path, rule, unhandledProperties, result) => {
if (unhandledProperties.has(this.ruleProperty)) {
unhandledProperties.delete(this.ruleProperty);
const value = rule[this.ruleProperty];
const value =
rule[/** @type {keyof RuleSetRule} */ (this.ruleProperty)];
const condition = ruleSetCompiler.compileCondition(
`${path}.${this.ruleProperty}`,
value

View File

@@ -5,6 +5,7 @@
"use strict";
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
/** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
/** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */
/** @typedef {import("./RuleSetCompiler").RuleConditionFunction} RuleConditionFunction */
@@ -32,7 +33,9 @@ class ObjectMatcherRulePlugin {
(path, rule, unhandledProperties, result) => {
if (unhandledProperties.has(ruleProperty)) {
unhandledProperties.delete(ruleProperty);
const value = rule[ruleProperty];
const value =
/** @type {Record<string, any>} */
(rule[/** @type {keyof RuleSetRule} */ (ruleProperty)]);
for (const property of Object.keys(value)) {
const nestedDataProperties = property.split(".");
const condition = ruleSetCompiler.compileCondition(

View File

@@ -7,7 +7,10 @@
const { SyncHook } = require("tapable");
/** @typedef {function(string): boolean} RuleConditionFunction */
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRules} RuleSetRules */
/** @typedef {function(string | EffectData): boolean} RuleConditionFunction */
/**
* @typedef {object} RuleCondition
@@ -22,10 +25,14 @@ const { SyncHook } = require("tapable");
* @property {RuleConditionFunction} fn
*/
/**
* @typedef {Record<string, TODO>} EffectData
*/
/**
* @typedef {object} CompiledRule
* @property {RuleCondition[]} conditions
* @property {(Effect|function(object): Effect[])[]} effects
* @property {(Effect|function(EffectData): Effect[])[]} effects
* @property {CompiledRule[]=} rules
* @property {CompiledRule[]=} oneOf
*/
@@ -39,13 +46,18 @@ const { SyncHook } = require("tapable");
/**
* @typedef {object} RuleSet
* @property {Map<string, any>} references map of references in the rule set (may grow over time)
* @property {function(object): Effect[]} exec execute the rule set
* @property {function(EffectData): Effect[]} exec execute the rule set
*/
/** @typedef {{ apply: (function(RuleSetCompiler): void) }} RuleSetPlugin */
class RuleSetCompiler {
/**
* @param {RuleSetPlugin[]} plugins plugins
*/
constructor(plugins) {
this.hooks = Object.freeze({
/** @type {SyncHook<[string, object, Set<string>, CompiledRule, Map<string, any>]>} */
/** @type {SyncHook<[string, RuleSetRule, Set<string>, CompiledRule, Map<string | undefined, any>]>} */
rule: new SyncHook([
"path",
"rule",
@@ -62,7 +74,7 @@ class RuleSetCompiler {
}
/**
* @param {object[]} ruleSet raw user provided rules
* @param {TODO[]} ruleSet raw user provided rules
* @returns {RuleSet} compiled RuleSet
*/
compile(ruleSet) {
@@ -70,7 +82,7 @@ class RuleSetCompiler {
const rules = this.compileRules("ruleSet", ruleSet, refs);
/**
* @param {object} data data passed in
* @param {EffectData} data data passed in
* @param {CompiledRule} rule the compiled rule
* @param {Effect[]} effects an array where effects are pushed to
* @returns {boolean} true, if the rule has matched
@@ -79,6 +91,7 @@ class RuleSetCompiler {
for (const condition of rule.conditions) {
const p = condition.property;
if (Array.isArray(p)) {
/** @type {EffectData | string | undefined} */
let current = data;
for (const subProperty of p) {
if (
@@ -147,25 +160,33 @@ class RuleSetCompiler {
/**
* @param {string} path current path
* @param {object[]} rules the raw rules provided by user
* @param {RuleSetRules} rules the raw rules provided by user
* @param {Map<string, any>} refs references
* @returns {CompiledRule[]} rules
*/
compileRules(path, rules, refs) {
return rules
.filter(Boolean)
.map((rule, i) => this.compileRule(`${path}[${i}]`, rule, refs));
.map((rule, i) =>
this.compileRule(
`${path}[${i}]`,
/** @type {RuleSetRule} */ (rule),
refs
)
);
}
/**
* @param {string} path current path
* @param {object} rule the raw rule provided by user
* @param {RuleSetRule} rule the raw rule provided by user
* @param {Map<string, any>} refs references
* @returns {CompiledRule} normalized and compiled rule for processing
*/
compileRule(path, rule, refs) {
const unhandledProperties = new Set(
Object.keys(rule).filter(key => rule[key] !== undefined)
Object.keys(rule).filter(
key => rule[/** @type {keyof RuleSetRule} */ (key)] !== undefined
)
);
/** @type {CompiledRule} */
@@ -236,7 +257,7 @@ class RuleSetCompiler {
matchWhenEmpty: condition(""),
fn: condition
};
} catch (err) {
} catch (_err) {
throw this.error(
path,
condition,
@@ -303,7 +324,7 @@ class RuleSetCompiler {
const fn = matcher.fn;
conditions.push({
matchWhenEmpty: !matcher.matchWhenEmpty,
fn: v => !fn(v)
fn: /** @type {RuleConditionFunction} */ (v => !fn(v))
});
}
break;
@@ -337,12 +358,11 @@ class RuleSetCompiler {
};
} else if (conditions.length === 1) {
return conditions[0];
} else {
return {
matchWhenEmpty: conditions.some(c => c.matchWhenEmpty),
fn: v => conditions.some(c => c.fn(v))
};
}
return {
matchWhenEmpty: conditions.some(c => c.matchWhenEmpty),
fn: v => conditions.some(c => c.fn(v))
};
}
/**
@@ -357,12 +377,11 @@ class RuleSetCompiler {
};
} else if (conditions.length === 1) {
return conditions[0];
} else {
return {
matchWhenEmpty: conditions.every(c => c.matchWhenEmpty),
fn: v => conditions.every(c => c.fn(v))
};
}
return {
matchWhenEmpty: conditions.every(c => c.matchWhenEmpty),
fn: v => conditions.every(c => c.fn(v))
};
}
/**

View File

@@ -7,6 +7,9 @@
const util = require("util");
/** @typedef {import("../../declarations/WebpackOptions").RuleSetLoader} RuleSetLoader */
/** @typedef {import("../../declarations/WebpackOptions").RuleSetLoaderOptions} RuleSetLoaderOptions */
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
/** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
/** @typedef {import("./RuleSetCompiler").Effect} Effect */
@@ -19,6 +22,10 @@ class UseEffectRulePlugin {
ruleSetCompiler.hooks.rule.tap(
"UseEffectRulePlugin",
(path, rule, unhandledProperties, result, references) => {
/**
* @param {keyof RuleSetRule} property property
* @param {string} correctProperty correct property
*/
const conflictWith = (property, correctProperty) => {
if (unhandledProperties.has(property)) {
throw ruleSetCompiler.error(
@@ -42,7 +49,6 @@ class UseEffectRulePlugin {
const type = enforce ? `use-${enforce}` : "use";
/**
*
* @param {string} path options path
* @param {string} defaultIdent default ident when none is provided
* @param {object} item user provided use value
@@ -51,16 +57,14 @@ class UseEffectRulePlugin {
const useToEffect = (path, defaultIdent, item) => {
if (typeof item === "function") {
return data => useToEffectsWithoutIdent(path, item(data));
} else {
return useToEffectRaw(path, defaultIdent, item);
}
return useToEffectRaw(path, defaultIdent, item);
};
/**
*
* @param {string} path options path
* @param {string} defaultIdent default ident when none is provided
* @param {object} item user provided use value
* @param {{ ident?: string, loader?: RuleSetLoader, options?: RuleSetLoaderOptions }} item user provided use value
* @returns {Effect} effect
*/
const useToEffectRaw = (path, defaultIdent, item) => {
@@ -73,30 +77,29 @@ class UseEffectRulePlugin {
ident: undefined
}
};
} else {
const loader = item.loader;
const options = item.options;
let ident = item.ident;
if (options && typeof options === "object") {
if (!ident) ident = defaultIdent;
references.set(ident, options);
}
if (typeof options === "string") {
util.deprecate(
() => {},
`Using a string as loader options is deprecated (${path}.options)`,
"DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING"
)();
}
return {
type: enforce ? `use-${enforce}` : "use",
value: {
loader,
options,
ident
}
};
}
const loader = item.loader;
const options = item.options;
let ident = item.ident;
if (options && typeof options === "object") {
if (!ident) ident = defaultIdent;
references.set(ident, options);
}
if (typeof options === "string") {
util.deprecate(
() => {},
`Using a string as loader options is deprecated (${path}.options)`,
"DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING"
)();
}
return {
type: enforce ? `use-${enforce}` : "use",
value: {
loader,
options,
ident
}
};
};
/**
@@ -132,7 +135,10 @@ class UseEffectRulePlugin {
if (typeof use === "function") {
result.effects.push(data =>
useToEffectsWithoutIdent(`${path}.use`, use(data))
useToEffectsWithoutIdent(
`${path}.use`,
use(/** @type {TODO} */ (data))
)
);
} else {
for (const effect of useToEffects(`${path}.use`, use)) {
@@ -146,7 +152,7 @@ class UseEffectRulePlugin {
unhandledProperties.delete("options");
unhandledProperties.delete("enforce");
const loader = rule.loader;
const loader = /** @type {RuleSetLoader} */ (rule.loader);
const options = rule.options;
const enforce = rule.enforce;
@@ -189,8 +195,6 @@ class UseEffectRulePlugin {
}
);
}
useItemToEffects(path, item) {}
}
module.exports = UseEffectRulePlugin;