feat: refactoring project
This commit is contained in:
109
node_modules/webpack/lib/util/comparators.js
generated
vendored
109
node_modules/webpack/lib/util/comparators.js
generated
vendored
@@ -10,14 +10,26 @@ const { compareRuntime } = require("./runtime");
|
||||
/** @typedef {import("../Chunk")} Chunk */
|
||||
/** @typedef {import("../Chunk").ChunkId} ChunkId */
|
||||
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
||||
/** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
|
||||
/** @typedef {import("../ChunkGroup")} ChunkGroup */
|
||||
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
||||
/** @typedef {import("../Module")} Module */
|
||||
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
||||
|
||||
/** @template T @typedef {function(T, T): -1|0|1} Comparator */
|
||||
/** @template TArg @template T @typedef {function(TArg, T, T): -1|0|1} RawParameterizedComparator */
|
||||
/** @template TArg @template T @typedef {function(TArg): Comparator<T>} ParameterizedComparator */
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {function(T, T): -1|0|1} Comparator
|
||||
*/
|
||||
/**
|
||||
* @template TArg
|
||||
* @template T
|
||||
* @typedef {function(TArg, T, T): -1|0|1} RawParameterizedComparator
|
||||
*/
|
||||
/**
|
||||
* @template TArg
|
||||
* @template T
|
||||
* @typedef {function(TArg): Comparator<T>} ParameterizedComparator
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
@@ -46,21 +58,16 @@ const createCachedParameterizedComparator = fn => {
|
||||
* @param {Chunk} b chunk
|
||||
* @returns {-1|0|1} compare result
|
||||
*/
|
||||
exports.compareChunksById = (a, b) => {
|
||||
return compareIds(
|
||||
/** @type {ChunkId} */ (a.id),
|
||||
/** @type {ChunkId} */ (b.id)
|
||||
);
|
||||
};
|
||||
module.exports.compareChunksById = (a, b) =>
|
||||
compareIds(/** @type {ChunkId} */ (a.id), /** @type {ChunkId} */ (b.id));
|
||||
|
||||
/**
|
||||
* @param {Module} a module
|
||||
* @param {Module} b module
|
||||
* @returns {-1|0|1} compare result
|
||||
*/
|
||||
exports.compareModulesByIdentifier = (a, b) => {
|
||||
return compareIds(a.identifier(), b.identifier());
|
||||
};
|
||||
module.exports.compareModulesByIdentifier = (a, b) =>
|
||||
compareIds(a.identifier(), b.identifier());
|
||||
|
||||
/**
|
||||
* @param {ChunkGraph} chunkGraph the chunk graph
|
||||
@@ -68,11 +75,13 @@ exports.compareModulesByIdentifier = (a, b) => {
|
||||
* @param {Module} b module
|
||||
* @returns {-1|0|1} compare result
|
||||
*/
|
||||
const compareModulesById = (chunkGraph, a, b) => {
|
||||
return compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b));
|
||||
};
|
||||
const compareModulesById = (chunkGraph, a, b) =>
|
||||
compareIds(
|
||||
/** @type {ModuleId} */ (chunkGraph.getModuleId(a)),
|
||||
/** @type {ModuleId} */ (chunkGraph.getModuleId(b))
|
||||
);
|
||||
/** @type {ParameterizedComparator<ChunkGraph, Module>} */
|
||||
exports.compareModulesById =
|
||||
module.exports.compareModulesById =
|
||||
createCachedParameterizedComparator(compareModulesById);
|
||||
|
||||
/**
|
||||
@@ -88,7 +97,7 @@ const compareNumbers = (a, b) => {
|
||||
if (a > b) return 1;
|
||||
return 0;
|
||||
};
|
||||
exports.compareNumbers = compareNumbers;
|
||||
module.exports.compareNumbers = compareNumbers;
|
||||
|
||||
/**
|
||||
* @param {string} a string
|
||||
@@ -160,7 +169,7 @@ const compareStringsNumeric = (a, b) => {
|
||||
|
||||
return 0;
|
||||
};
|
||||
exports.compareStringsNumeric = compareStringsNumeric;
|
||||
module.exports.compareStringsNumeric = compareStringsNumeric;
|
||||
|
||||
/**
|
||||
* @param {ModuleGraph} moduleGraph the module graph
|
||||
@@ -177,7 +186,7 @@ const compareModulesByPostOrderIndexOrIdentifier = (moduleGraph, a, b) => {
|
||||
return compareIds(a.identifier(), b.identifier());
|
||||
};
|
||||
/** @type {ParameterizedComparator<ModuleGraph, Module>} */
|
||||
exports.compareModulesByPostOrderIndexOrIdentifier =
|
||||
module.exports.compareModulesByPostOrderIndexOrIdentifier =
|
||||
createCachedParameterizedComparator(
|
||||
compareModulesByPostOrderIndexOrIdentifier
|
||||
);
|
||||
@@ -197,7 +206,7 @@ const compareModulesByPreOrderIndexOrIdentifier = (moduleGraph, a, b) => {
|
||||
return compareIds(a.identifier(), b.identifier());
|
||||
};
|
||||
/** @type {ParameterizedComparator<ModuleGraph, Module>} */
|
||||
exports.compareModulesByPreOrderIndexOrIdentifier =
|
||||
module.exports.compareModulesByPreOrderIndexOrIdentifier =
|
||||
createCachedParameterizedComparator(
|
||||
compareModulesByPreOrderIndexOrIdentifier
|
||||
);
|
||||
@@ -209,14 +218,16 @@ exports.compareModulesByPreOrderIndexOrIdentifier =
|
||||
* @returns {-1|0|1} compare result
|
||||
*/
|
||||
const compareModulesByIdOrIdentifier = (chunkGraph, a, b) => {
|
||||
const cmp = compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b));
|
||||
const cmp = compareIds(
|
||||
/** @type {ModuleId} */ (chunkGraph.getModuleId(a)),
|
||||
/** @type {ModuleId} */ (chunkGraph.getModuleId(b))
|
||||
);
|
||||
if (cmp !== 0) return cmp;
|
||||
return compareIds(a.identifier(), b.identifier());
|
||||
};
|
||||
/** @type {ParameterizedComparator<ChunkGraph, Module>} */
|
||||
exports.compareModulesByIdOrIdentifier = createCachedParameterizedComparator(
|
||||
compareModulesByIdOrIdentifier
|
||||
);
|
||||
module.exports.compareModulesByIdOrIdentifier =
|
||||
createCachedParameterizedComparator(compareModulesByIdOrIdentifier);
|
||||
|
||||
/**
|
||||
* @param {ChunkGraph} chunkGraph the chunk graph
|
||||
@@ -224,11 +235,10 @@ exports.compareModulesByIdOrIdentifier = createCachedParameterizedComparator(
|
||||
* @param {Chunk} b chunk
|
||||
* @returns {-1|0|1} compare result
|
||||
*/
|
||||
const compareChunks = (chunkGraph, a, b) => {
|
||||
return chunkGraph.compareChunks(a, b);
|
||||
};
|
||||
const compareChunks = (chunkGraph, a, b) => chunkGraph.compareChunks(a, b);
|
||||
/** @type {ParameterizedComparator<ChunkGraph, Chunk>} */
|
||||
exports.compareChunks = createCachedParameterizedComparator(compareChunks);
|
||||
module.exports.compareChunks =
|
||||
createCachedParameterizedComparator(compareChunks);
|
||||
|
||||
/**
|
||||
* @param {string|number} a first id
|
||||
@@ -244,7 +254,7 @@ const compareIds = (a, b) => {
|
||||
return 0;
|
||||
};
|
||||
|
||||
exports.compareIds = compareIds;
|
||||
module.exports.compareIds = compareIds;
|
||||
|
||||
/**
|
||||
* @param {string} a first string
|
||||
@@ -257,20 +267,16 @@ const compareStrings = (a, b) => {
|
||||
return 0;
|
||||
};
|
||||
|
||||
exports.compareStrings = compareStrings;
|
||||
module.exports.compareStrings = compareStrings;
|
||||
|
||||
/**
|
||||
* @param {ChunkGroup} a first chunk group
|
||||
* @param {ChunkGroup} b second chunk group
|
||||
* @returns {-1|0|1} compare result
|
||||
*/
|
||||
const compareChunkGroupsByIndex = (a, b) => {
|
||||
return /** @type {number} */ (a.index) < /** @type {number} */ (b.index)
|
||||
? -1
|
||||
: 1;
|
||||
};
|
||||
|
||||
exports.compareChunkGroupsByIndex = compareChunkGroupsByIndex;
|
||||
const compareChunkGroupsByIndex = (a, b) =>
|
||||
/** @type {number} */ (a.index) < /** @type {number} */ (b.index) ? -1 : 1;
|
||||
module.exports.compareChunkGroupsByIndex = compareChunkGroupsByIndex;
|
||||
|
||||
/**
|
||||
* @template K1 {Object}
|
||||
@@ -294,7 +300,7 @@ class TwoKeyWeakMap {
|
||||
get(key1, key2) {
|
||||
const childMap = this._map.get(key1);
|
||||
if (childMap === undefined) {
|
||||
return undefined;
|
||||
return;
|
||||
}
|
||||
return childMap.get(key2);
|
||||
}
|
||||
@@ -347,7 +353,7 @@ const concatComparators = (c1, c2, ...cRest) => {
|
||||
concatComparatorsCache.set(c1, c2, result);
|
||||
return result;
|
||||
};
|
||||
exports.concatComparators = concatComparators;
|
||||
module.exports.concatComparators = concatComparators;
|
||||
|
||||
/**
|
||||
* @template A, B
|
||||
@@ -380,17 +386,16 @@ const compareSelect = (getter, comparator) => {
|
||||
return comparator(aValue, bValue);
|
||||
}
|
||||
return -1;
|
||||
} else {
|
||||
if (bValue !== undefined && bValue !== null) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (bValue !== undefined && bValue !== null) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
compareSelectCache.set(getter, comparator, result);
|
||||
return result;
|
||||
};
|
||||
exports.compareSelect = compareSelect;
|
||||
module.exports.compareSelect = compareSelect;
|
||||
|
||||
/** @type {WeakMap<Comparator<any>, Comparator<Iterable<any>>>} */
|
||||
const compareIteratorsCache = new WeakMap();
|
||||
@@ -426,7 +431,7 @@ const compareIterables = elementComparator => {
|
||||
compareIteratorsCache.set(elementComparator, result);
|
||||
return result;
|
||||
};
|
||||
exports.compareIterables = compareIterables;
|
||||
module.exports.compareIterables = compareIterables;
|
||||
|
||||
// TODO this is no longer needed when minimum node.js version is >= 12
|
||||
// since these versions ship with a stable sort function
|
||||
@@ -435,7 +440,7 @@ exports.compareIterables = compareIterables;
|
||||
* @param {Iterable<T>} iterable original ordered list
|
||||
* @returns {Comparator<T>} comparator
|
||||
*/
|
||||
exports.keepOriginalOrder = iterable => {
|
||||
module.exports.keepOriginalOrder = iterable => {
|
||||
/** @type {Map<T, number>} */
|
||||
const map = new Map();
|
||||
let i = 0;
|
||||
@@ -453,8 +458,8 @@ exports.keepOriginalOrder = iterable => {
|
||||
* @param {ChunkGraph} chunkGraph the chunk graph
|
||||
* @returns {Comparator<Chunk>} comparator
|
||||
*/
|
||||
exports.compareChunksNatural = chunkGraph => {
|
||||
const cmpFn = exports.compareModulesById(chunkGraph);
|
||||
module.exports.compareChunksNatural = chunkGraph => {
|
||||
const cmpFn = module.exports.compareModulesById(chunkGraph);
|
||||
const cmpIterableFn = compareIterables(cmpFn);
|
||||
return concatComparators(
|
||||
compareSelect(
|
||||
@@ -479,9 +484,9 @@ exports.compareChunksNatural = chunkGraph => {
|
||||
* @param {DependencyLocation} b A location node
|
||||
* @returns {-1|0|1} sorting comparator value
|
||||
*/
|
||||
exports.compareLocations = (a, b) => {
|
||||
let isObjectA = typeof a === "object" && a !== null;
|
||||
let isObjectB = typeof b === "object" && b !== null;
|
||||
module.exports.compareLocations = (a, b) => {
|
||||
const isObjectA = typeof a === "object" && a !== null;
|
||||
const isObjectB = typeof b === "object" && b !== null;
|
||||
if (!isObjectA || !isObjectB) {
|
||||
if (isObjectA) return 1;
|
||||
if (isObjectB) return -1;
|
||||
|
||||
Reference in New Issue
Block a user