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

@@ -17,7 +17,7 @@ const memoize = require("./util/memoize");
/** @typedef {string | RegExp | (string | RegExp)[]} Matcher */
/** @typedef {{test?: Matcher, include?: Matcher, exclude?: Matcher }} MatchObject */
const ModuleFilenameHelpers = exports;
const ModuleFilenameHelpers = module.exports;
// TODO webpack 6: consider removing these
ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
@@ -54,12 +54,10 @@ ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
* @param {string} token the token to search for
* @returns {ReturnStringCallback} a function that returns the part of the string after the token
*/
const getAfter = (strFn, token) => {
return () => {
const str = strFn();
const idx = str.indexOf(token);
return idx < 0 ? "" : str.slice(idx);
};
const getAfter = (strFn, token) => () => {
const str = strFn();
const idx = str.indexOf(token);
return idx < 0 ? "" : str.slice(idx);
};
/**
@@ -68,12 +66,10 @@ const getAfter = (strFn, token) => {
* @param {string} token the token to search for
* @returns {ReturnStringCallback} a function that returns the part of the string before the token
*/
const getBefore = (strFn, token) => {
return () => {
const str = strFn();
const idx = str.lastIndexOf(token);
return idx < 0 ? "" : str.slice(0, idx);
};
const getBefore = (strFn, token) => () => {
const str = strFn();
const idx = str.lastIndexOf(token);
return idx < 0 ? "" : str.slice(0, idx);
};
/**
@@ -82,14 +78,14 @@ const getBefore = (strFn, token) => {
* @param {string | Hash=} hashFunction the hash function to use
* @returns {ReturnStringCallback} a function that returns the hash of the string
*/
const getHash = (strFn, hashFunction = "md4") => {
return () => {
const getHash =
(strFn, hashFunction = "md4") =>
() => {
const hash = createHash(hashFunction);
hash.update(strFn());
const digest = /** @type {string} */ (hash.digest("hex"));
return digest.slice(0, 4);
};
};
/**
* Returns a function that returns the string with the token replaced with the replacement
@@ -108,7 +104,7 @@ const asRegExp = test => {
if (typeof test === "string") {
// Escape special characters in the string to prevent them from being interpreted as special characters in a regular expression. Do this by
// adding a backslash before each special character
test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
test = new RegExp(`^${test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")}`);
}
return test;
};
@@ -143,7 +139,6 @@ const lazyObject = obj => {
const SQUARE_BRACKET_TAG_REGEXP = /\[\\*([\w-]+)\\*\]/gi;
/**
*
* @param {Module | string} module the module
* @param {TODO} options options
* @param {object} contextInfo context info
@@ -153,6 +148,7 @@ const SQUARE_BRACKET_TAG_REGEXP = /\[\\*([\w-]+)\\*\]/gi;
* @returns {string} the filename
*/
ModuleFilenameHelpers.createFilename = (
// eslint-disable-next-line default-param-last
module = "",
options,
{ requestShortener, chunkGraph, hashFunction = "md4" }
@@ -213,9 +209,9 @@ ModuleFilenameHelpers.createFilename = (
if (typeof opts.moduleFilenameTemplate === "function") {
return opts.moduleFilenameTemplate(
lazyObject({
identifier: identifier,
shortIdentifier: shortIdentifier,
resource: resource,
identifier,
shortIdentifier,
resource,
resourcePath: memoize(resourcePath),
absoluteResourcePath: memoize(absoluteResourcePath),
loaders: memoize(loaders),
@@ -285,13 +281,11 @@ ModuleFilenameHelpers.createFilename = (
* Replaces duplicate items in an array with new values generated by a callback function.
* The callback function is called with the duplicate item, the index of the duplicate item, and the number of times the item has been replaced.
* The callback function should return the new value for the duplicate item.
*
* @template T
* @param {T[]} array the array with duplicates to be replaced
* @param {(duplicateItem: T, duplicateItemIndex: number, numberOfTimesReplaced: number) => T} fn callback function to generate new values for the duplicate items
* @param {(firstElement:T, nextElement:T) => -1 | 0 | 1} [comparator] optional comparator function to sort the duplicate items
* @returns {T[]} the array with duplicates replaced
*
* @example
* ```js
* const array = ["a", "b", "c", "a", "b", "a"];
@@ -303,33 +297,30 @@ ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
const countMap = Object.create(null);
const posMap = Object.create(null);
array.forEach((item, idx) => {
for (const [idx, item] of array.entries()) {
countMap[item] = countMap[item] || [];
countMap[item].push(idx);
posMap[item] = 0;
});
}
if (comparator) {
Object.keys(countMap).forEach(item => {
for (const item of Object.keys(countMap)) {
countMap[item].sort(comparator);
});
}
}
return array.map((item, i) => {
if (countMap[item].length > 1) {
if (comparator && countMap[item][0] === i) return item;
return fn(item, i, posMap[item]++);
} else {
return item;
}
return item;
});
};
/**
* Tests if a string matches a RegExp or an array of RegExp.
*
* @param {string} str string to test
* @param {Matcher} test value which will be used to match against the string
* @returns {boolean} true, when the RegExp matches
*
* @example
* ```js
* ModuleFilenameHelpers.matchPart("foo.js", "foo"); // true
@@ -349,9 +340,8 @@ ModuleFilenameHelpers.matchPart = (str, test) => {
if (Array.isArray(test)) {
return test.map(asRegExp).some(regExp => regExp.test(str));
} else {
return asRegExp(test).test(str);
}
return asRegExp(test).test(str);
};
/**
@@ -361,7 +351,6 @@ ModuleFilenameHelpers.matchPart = (str, test) => {
* - `exclude`: a RegExp or an array of RegExp
*
* The `test` property is tested first, then `include` and then `exclude`.
*
* @param {MatchObject} obj a match object to test against the string
* @param {string} str string to test against the matching object
* @returns {boolean} true, when the object matches
@@ -381,20 +370,14 @@ ModuleFilenameHelpers.matchPart = (str, test) => {
* ```
*/
ModuleFilenameHelpers.matchObject = (obj, str) => {
if (obj.test) {
if (!ModuleFilenameHelpers.matchPart(str, obj.test)) {
return false;
}
if (obj.test && !ModuleFilenameHelpers.matchPart(str, obj.test)) {
return false;
}
if (obj.include) {
if (!ModuleFilenameHelpers.matchPart(str, obj.include)) {
return false;
}
if (obj.include && !ModuleFilenameHelpers.matchPart(str, obj.include)) {
return false;
}
if (obj.exclude) {
if (ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
return false;
}
if (obj.exclude && ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
return false;
}
return true;
};