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

@@ -30,8 +30,11 @@ const {
/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./Chunk").ChunkId} ChunkId */
/** @typedef {import("./ChunkGroup")} ChunkGroup */
/** @typedef {import("./Generator").SourceTypes} SourceTypes */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
/** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
/** @typedef {import("./RuntimeModule")} RuntimeModule */
@@ -73,9 +76,7 @@ class ModuleHashInfo {
* @param {SortableSet<T>} set the set
* @returns {T[]} set as array
*/
const getArray = set => {
return Array.from(set);
};
const getArray = set => Array.from(set);
/**
* @param {SortableSet<Chunk>} chunks the chunks
@@ -120,7 +121,10 @@ const modulesBySourceType = sourceTypesByModule => set => {
};
const defaultModulesBySourceType = modulesBySourceType(undefined);
/** @type {WeakMap<Function, any>} */
/**
* @template T
* @type {WeakMap<Function, any>}
*/
const createOrderedArrayFunctionMap = new WeakMap();
/**
@@ -159,7 +163,7 @@ const getModulesSize = modules => {
* @returns {Record<string, number>} the sizes of the modules
*/
const getModulesSizes = modules => {
let sizes = Object.create(null);
const sizes = Object.create(null);
for (const module of modules) {
for (const type of module.getSourceTypes()) {
sizes[type] = (sizes[type] || 0) + module.size(type);
@@ -199,7 +203,7 @@ class ChunkGraphModule {
this.runtimeInChunks = undefined;
/** @type {RuntimeSpecMap<ModuleHashInfo> | undefined} */
this.hashes = undefined;
/** @type {string | number} */
/** @type {ModuleId | null} */
this.id = null;
/** @type {RuntimeSpecMap<Set<string>> | undefined} */
this.runtimeRequirements = undefined;
@@ -625,7 +629,7 @@ class ChunkGraph {
/**
* @param {Chunk} chunk chunk
* @param {Module} module chunk module
* @returns {Set<string>} source types
* @returns {SourceTypes} source types
*/
getChunkModuleSourceTypes(chunk, module) {
const cgc = this._getChunkGraphChunk(chunk);
@@ -637,7 +641,7 @@ class ChunkGraph {
/**
* @param {Module} module module
* @returns {Set<string>} source types
* @returns {SourceTypes} source types
*/
getModuleSourceTypes(module) {
return (
@@ -702,7 +706,7 @@ class ChunkGraph {
const modulesWithSourceType = cgc.modules
.getFromUnorderedCache(cgc._modulesBySourceType)
.get(sourceType);
if (modulesWithSourceType === undefined) return undefined;
if (modulesWithSourceType === undefined) return;
modulesWithSourceType.sortWith(comparator);
return modulesWithSourceType;
}
@@ -749,9 +753,9 @@ class ChunkGraph {
if (filterFn(module)) {
if (array === undefined) {
array = [];
chunkModuleIdMap[asyncChunk.id] = array;
chunkModuleIdMap[/** @type {ChunkId} */ (asyncChunk.id)] = array;
}
const moduleId = this.getModuleId(module);
const moduleId = /** @type {ModuleId} */ (this.getModuleId(module));
array.push(moduleId);
}
}
@@ -773,13 +777,15 @@ class ChunkGraph {
hashLength = 0,
includeAllChunks = false
) {
/** @type {Record<string|number, Record<string|number, string>>} */
/** @type {Record<ChunkId, Record<string|number, string>>} */
const chunkModuleHashMap = Object.create(null);
/** @typedef {Record<string|number, string>} IdToHashMap */
for (const asyncChunk of includeAllChunks
? chunk.getAllReferencedChunks()
: chunk.getAllAsyncChunks()) {
/** @type {Record<string|number, string> | undefined} */
/** @type {IdToHashMap | undefined} */
let idToHashMap;
for (const module of this.getOrderedChunkModulesIterable(
asyncChunk,
@@ -788,11 +794,15 @@ class ChunkGraph {
if (filterFn(module)) {
if (idToHashMap === undefined) {
idToHashMap = Object.create(null);
chunkModuleHashMap[asyncChunk.id] = idToHashMap;
chunkModuleHashMap[/** @type {ChunkId} */ (asyncChunk.id)] =
/** @type {IdToHashMap} */ (idToHashMap);
}
const moduleId = this.getModuleId(module);
const hash = this.getRenderedModuleHash(module, asyncChunk.runtime);
idToHashMap[moduleId] = hashLength ? hash.slice(0, hashLength) : hash;
/** @type {IdToHashMap} */
(idToHashMap)[/** @type {ModuleId} */ (moduleId)] = hashLength
? hash.slice(0, hashLength)
: hash;
}
}
}
@@ -808,7 +818,7 @@ class ChunkGraph {
getChunkConditionMap(chunk, filterFn) {
const map = Object.create(null);
for (const c of chunk.getAllReferencedChunks()) {
map[c.id] = filterFn(c, this);
map[/** @type {ChunkId} */ (c.id)] = filterFn(c, this);
}
return map;
}
@@ -916,7 +926,7 @@ class ChunkGraph {
const cgcB = this._getChunkGraphChunk(chunkB);
const allModules = new Set(cgcA.modules);
for (const m of cgcB.modules) allModules.add(m);
let modulesSize = getModulesSize(allModules);
const modulesSize = getModulesSize(allModules);
const chunkOverhead =
typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
const entryChunkMultiplicator =
@@ -950,9 +960,9 @@ class ChunkGraph {
return isAvailableChunk(chunkA, chunkB);
} else if (hasRuntimeB) {
return isAvailableChunk(chunkB, chunkA);
} else {
return false;
}
return false;
}
if (
@@ -1011,7 +1021,12 @@ class ChunkGraph {
this.getChunkEntryModulesWithChunkGroupIterable(chunkB)
)) {
this.disconnectChunkAndEntryModule(chunkB, module);
this.connectChunkAndEntryModule(chunkA, module, chunkGroup);
this.connectChunkAndEntryModule(
chunkA,
module,
/** @type {Entrypoint} */
(chunkGroup)
);
}
for (const chunkGroup of chunkB.groupsIterable) {
@@ -1052,7 +1067,7 @@ class ChunkGraph {
/**
* @param {Chunk} chunk the new chunk
* @param {Module} module the entry module
* @param {Entrypoint=} entrypoint the chunk group which must be loaded before the module is executed
* @param {Entrypoint} entrypoint the chunk group which must be loaded before the module is executed
* @returns {void}
*/
connectChunkAndEntryModule(chunk, module, entrypoint) {
@@ -1111,8 +1126,9 @@ class ChunkGraph {
disconnectChunkAndEntryModule(chunk, module) {
const cgm = this._getChunkGraphModule(module);
const cgc = this._getChunkGraphChunk(chunk);
cgm.entryInChunks.delete(chunk);
if (cgm.entryInChunks.size === 0) {
/** @type {EntryInChunks} */
(cgm.entryInChunks).delete(chunk);
if (/** @type {EntryInChunks} */ (cgm.entryInChunks).size === 0) {
cgm.entryInChunks = undefined;
}
cgc.entryModules.delete(module);
@@ -1126,8 +1142,9 @@ class ChunkGraph {
disconnectChunkAndRuntimeModule(chunk, module) {
const cgm = this._getChunkGraphModule(module);
const cgc = this._getChunkGraphChunk(chunk);
cgm.runtimeInChunks.delete(chunk);
if (cgm.runtimeInChunks.size === 0) {
/** @type {RuntimeInChunks} */
(cgm.runtimeInChunks).delete(chunk);
if (/** @type {RuntimeInChunks} */ (cgm.runtimeInChunks).size === 0) {
cgm.runtimeInChunks = undefined;
}
cgc.runtimeModules.delete(module);
@@ -1154,8 +1171,9 @@ class ChunkGraph {
const cgc = this._getChunkGraphChunk(chunk);
for (const module of cgc.entryModules.keys()) {
const cgm = this._getChunkGraphModule(module);
cgm.entryInChunks.delete(chunk);
if (cgm.entryInChunks.size === 0) {
/** @type {EntryInChunks} */
(cgm.entryInChunks).delete(chunk);
if (/** @type {EntryInChunks} */ (cgm.entryInChunks).size === 0) {
cgm.entryInChunks = undefined;
}
}
@@ -1322,7 +1340,7 @@ class ChunkGraph {
/**
* @param {Module} module the module
* @returns {ModuleId} the id of the module
* @returns {ModuleId | null} the id of the module
*/
getModuleId(module) {
const cgm = this._getChunkGraphModule(module);
@@ -1344,7 +1362,7 @@ class ChunkGraph {
* @returns {string | number} the id of the runtime
*/
getRuntimeId(runtime) {
return this._runtimeIds.get(runtime);
return /** @type {string | number} */ (this._runtimeIds.get(runtime));
}
/**
@@ -1474,10 +1492,10 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
} else if (!transferOwnership || runtimeRequirements.size >= items.size) {
for (const item of items) runtimeRequirements.add(item);
return runtimeRequirements;
} else {
for (const item of runtimeRequirements) items.add(item);
return items;
}
for (const item of runtimeRequirements) items.add(item);
return items;
});
}
@@ -1513,7 +1531,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
/**
* @param {Module} module the module
* @param {RuntimeSpec} runtime the runtime
* @returns {ReadonlySet<string>} runtime requirements
* @returns {ReadOnlyRuntimeRequirements} runtime requirements
*/
getModuleRuntimeRequirements(module, runtime) {
const cgm = this._getChunkGraphModule(module);
@@ -1524,7 +1542,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
/**
* @param {Chunk} chunk the chunk
* @returns {ReadonlySet<string>} runtime requirements
* @returns {ReadOnlyRuntimeRequirements} runtime requirements
*/
getChunkRuntimeRequirements(chunk) {
const cgc = this._getChunkGraphChunk(chunk);
@@ -1593,6 +1611,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
if (cgm.graphHashesWithConnections === undefined) {
cgm.graphHashesWithConnections = new RuntimeSpecMap();
}
/**
* @param {ConnectionState} state state
* @returns {"F" | "T" | "O"} result
@@ -1615,6 +1634,10 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
const activeNamespaceModules = new Set();
/** @type {Map<string, Module | Set<Module>>} */
const connectedModules = new Map();
/**
* @param {ModuleGraphConnection} connection connection
* @param {string} stateInfo state info
*/
const processConnection = (connection, stateInfo) => {
const module = connection.module;
stateInfo += module.getExportsType(this.moduleGraph, strict);
@@ -1696,7 +1719,9 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
hash.update(xor.toString(16));
};
if (activeNamespaceModules.size === 1)
addModuleToHash(activeNamespaceModules.values().next().value);
addModuleToHash(
/** @type {Module} */ (activeNamespaceModules.values().next().value)
);
else if (activeNamespaceModules.size > 1)
addModulesToHash(activeNamespaceModules);
for (const [stateInfo, modules] of connectedModulesInOrder) {
@@ -1714,7 +1739,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
/**
* @param {Chunk} chunk the chunk
* @returns {ReadonlySet<string>} runtime requirements
* @returns {ReadOnlyRuntimeRequirements} runtime requirements
*/
getTreeRuntimeRequirements(chunk) {
const cgc = this._getChunkGraphChunk(chunk);
@@ -1740,12 +1765,13 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
const chunkGraph = chunkGraphForModuleMap.get(module);
if (!chunkGraph)
throw new Error(
deprecateMessage +
": There was no ChunkGraph assigned to the Module for backward-compat (Use the new API)"
`${
deprecateMessage
}: There was no ChunkGraph assigned to the Module for backward-compat (Use the new API)`
);
return chunkGraph;
},
deprecateMessage + ": Use new ChunkGraph API",
`${deprecateMessage}: Use new ChunkGraph API`,
deprecationCode
);
deprecateGetChunkGraphForModuleMap.set(deprecateMessage, newFn);
@@ -1790,12 +1816,13 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
const chunkGraph = chunkGraphForChunkMap.get(chunk);
if (!chunkGraph)
throw new Error(
deprecateMessage +
"There was no ChunkGraph assigned to the Chunk for backward-compat (Use the new API)"
`${
deprecateMessage
}There was no ChunkGraph assigned to the Chunk for backward-compat (Use the new API)`
);
return chunkGraph;
},
deprecateMessage + ": Use new ChunkGraph API",
`${deprecateMessage}: Use new ChunkGraph API`,
deprecationCode
);
deprecateGetChunkGraphForChunkMap.set(deprecateMessage, newFn);