webpack/lib/ChunkGraph.js

538 lines
15 KiB
JavaScript

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const SortableSet = require("./util/SortableSet");
const { compareModulesById } = require("./util/comparators");
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./ChunkGroup")} ChunkGroup */
/** @typedef {import("./Module")} Module */
/** @typedef {(m: Module) => boolean} ModuleFilterPredicate */
/**
* @param {Chunk} a chunk
* @param {Chunk} b chunk
* @returns {number} compare result
*/
const sortChunksByDebugId = (a, b) => {
return a.debugId - b.debugId;
};
/** @template T @typedef {(set: SortableSet<T>) => T[]} SetToArrayFunction<T> */
/**
* @template T
* @param {SortableSet<T>} set the set
* @returns {T[]} set as array
*/
const getArray = set => {
return Array.from(set);
};
/** @type {WeakMap<Function, any>} */
const createOrderedArrayFunctionMap = new WeakMap();
/**
* @template T
* @param {function(T, T): -1|0|1} comparator comparator function
* @returns {SetToArrayFunction<T>} set as ordered array
*/
const createOrderedArrayFunction = comparator => {
/** @type {SetToArrayFunction<T>} */
let fn = createOrderedArrayFunctionMap.get(comparator);
if (fn !== undefined) return fn;
fn = set => {
set.sortWith(comparator);
return Array.from(set);
};
createOrderedArrayFunctionMap.set(comparator, fn);
return fn;
};
/**
* @param {SortableSet<Module>} set the sortable Set to get the count/size of
* @returns {number} the size of the modules
*/
const getModulesSize = set => {
let size = 0;
for (const module of set) {
size += module.size();
}
return size;
};
class ChunkGraphModule {
constructor() {
/** @type {SortableSet<Chunk>} */
this.chunks = new SortableSet();
}
}
class ChunkGraphChunk {
constructor() {
/** @type {SortableSet<Module>} */
this.modules = new SortableSet();
}
}
class ChunkGraph {
constructor() {
/** @private @type {WeakMap<Module, ChunkGraphModule>} */
this._modules = new WeakMap();
/** @private @type {WeakMap<Chunk, ChunkGraphChunk>} */
this._chunks = new WeakMap();
}
/**
* @private
* @param {Module} module the module
* @returns {ChunkGraphModule} internal module
*/
_getChunkGraphModule(module) {
let m = this._modules.get(module);
if (m === undefined) {
m = new ChunkGraphModule();
this._modules.set(module, m);
}
return m;
}
/**
* @private
* @param {Chunk} chunk the chunk
* @returns {ChunkGraphChunk} internal chunk
*/
_getChunkGraphChunk(chunk) {
let c = this._chunks.get(chunk);
if (c === undefined) {
c = new ChunkGraphChunk();
this._chunks.set(chunk, c);
}
return c;
}
/**
* @param {Chunk} chunk the new chunk
* @param {Module} module the module
* @returns {boolean} true, if the chunk could be added. false if it was already added
*/
connectChunkAndModule(chunk, module) {
const cgm = this._getChunkGraphModule(module);
const cgc = this._getChunkGraphChunk(chunk);
// TODO refactor to remove return value
if (cgm.chunks.has(chunk) && cgc.modules.has(module)) return false;
cgm.chunks.add(chunk);
cgc.modules.add(module);
return true;
}
/**
* @param {Chunk} chunk the chunk
* @param {Module} module the module
* @returns {void}
*/
disconnectChunkAndModule(chunk, module) {
const cgm = this._getChunkGraphModule(module);
const cgc = this._getChunkGraphChunk(chunk);
cgc.modules.delete(module);
cgm.chunks.delete(chunk);
}
/**
* @param {Chunk} chunk the chunk which will be disconnected
* @returns {void}
*/
disconnectChunk(chunk) {
const cgc = this._getChunkGraphChunk(chunk);
for (const module of cgc.modules) {
const cgm = this._getChunkGraphModule(module);
cgm.chunks.delete(chunk);
}
cgc.modules.clear();
chunk.disconnectFromGroups();
}
/**
* @param {Chunk} chunk the chunk
* @param {Iterable<Module>} modules the modules
* @returns {void}
*/
attachModules(chunk, modules) {
const cgc = this._getChunkGraphChunk(chunk);
for (const module of modules) {
cgc.modules.add(module);
}
}
/**
* @param {Module} oldModule the replaced module
* @param {Module} newModule the replacing module
* @returns {void}
*/
replaceModule(oldModule, newModule) {
const oldCgm = this._getChunkGraphModule(oldModule);
const newCgm = this._getChunkGraphModule(newModule);
const chunks = this.getModuleChunks(oldModule);
for (const chunk of chunks) {
const cgc = this._getChunkGraphChunk(chunk);
cgc.modules.delete(oldModule);
cgc.modules.add(newModule);
oldCgm.chunks.delete(chunk);
newCgm.chunks.add(chunk);
}
}
/**
* @param {Module} module the checked module
* @param {Chunk} chunk the checked chunk
* @returns {boolean} true, if the chunk contains the module
*/
isModuleInChunk(module, chunk) {
const cgc = this._getChunkGraphChunk(chunk);
return cgc.modules.has(module);
}
/**
* @param {Module} module the checked module
* @param {ChunkGroup} chunkGroup the checked chunk group
* @returns {boolean} true, if the chunk contains the module
*/
isModuleInChunkGroup(module, chunkGroup) {
for (const chunk of chunkGroup.chunks) {
if (this.isModuleInChunk(module, chunk)) return true;
}
return false;
}
/**
* @param {Module} module the checked module
* @returns {boolean} true, if the module is entry of any chunk
*/
isEntryModule(module) {
const cgm = this._getChunkGraphModule(module);
for (const chunk of cgm.chunks) {
if (chunk.entryModule === module) return true;
}
return false;
}
/**
* @param {Module} module the module
* @returns {Iterable<Chunk>} iterable of chunks (do not modify)
*/
getModuleChunksIterable(module) {
const cgm = this._getChunkGraphModule(module);
return cgm.chunks;
}
/**
* @param {Module} module the module
* @param {function(Chunk, Chunk): -1|0|1} sortFn sort function
* @returns {Iterable<Chunk>} iterable of chunks (do not modify)
*/
getOrderedModuleChunksIterable(module, sortFn) {
const cgm = this._getChunkGraphModule(module);
cgm.chunks.sortWith(sortFn);
return cgm.chunks;
}
/**
* @param {Module} module the module
* @returns {Chunk[]} array of chunks (cached, do not modify)
*/
getModuleChunks(module) {
const cgm = this._getChunkGraphModule(module);
return cgm.chunks.getFromCache(getArray);
}
/**
* @param {Module} module the module
* @returns {number} the number of chunk which contain the module
*/
getNumberOfModuleChunks(module) {
const cgm = this._getChunkGraphModule(module);
return cgm.chunks.size;
}
/**
* @param {Module} moduleA some module
* @param {Module} moduleB some module
* @returns {boolean} true, if modules are in the same chunks
*/
haveModulesEqualChunks(moduleA, moduleB) {
const cgmA = this._getChunkGraphModule(moduleA);
const cgmB = this._getChunkGraphModule(moduleB);
if (cgmA.chunks.size !== cgmB.chunks.size) return false;
cgmA.chunks.sortWith(sortChunksByDebugId);
cgmB.chunks.sortWith(sortChunksByDebugId);
const a = cgmA.chunks[Symbol.iterator]();
const b = cgmB.chunks[Symbol.iterator]();
// eslint-disable-next-line no-constant-condition
while (true) {
const aItem = a.next();
if (aItem.done) return true;
const bItem = b.next();
if (aItem.value !== bItem.value) return false;
}
}
/**
* @param {Chunk} chunk the chunk
* @returns {number} the number of module which are contained in this chunk
*/
getNumberOfChunkModules(chunk) {
const cgc = this._getChunkGraphChunk(chunk);
return cgc.modules.size;
}
/**
* @param {Chunk} chunk the chunk
* @returns {Iterable<Module>} return the modules for this chunk
*/
getChunkModulesIterable(chunk) {
const cgc = this._getChunkGraphChunk(chunk);
return cgc.modules;
}
/**
* @param {Chunk} chunk the chunk
* @param {function(Module, Module): -1|0|1} comparator comparator function
* @returns {Iterable<Module>} return the modules for this chunk
*/
getOrderedChunkModulesIterable(chunk, comparator) {
const cgc = this._getChunkGraphChunk(chunk);
cgc.modules.sortWith(comparator);
return cgc.modules;
}
/**
* @param {Chunk} chunk the chunk
* @returns {Module[]} return the modules for this chunk (cached, do not modify)
*/
getChunkModules(chunk) {
const cgc = this._getChunkGraphChunk(chunk);
return cgc.modules.getFromUnorderedCache(getArray);
}
/**
* @param {Chunk} chunk the chunk
* @param {function(Module, Module): -1|0|1} comparator comparator function
* @returns {Module[]} return the modules for this chunk (cached, do not modify)
*/
getOrderedChunkModules(chunk, comparator) {
const cgc = this._getChunkGraphChunk(chunk);
const arrayFunction = createOrderedArrayFunction(comparator);
return cgc.modules.getFromUnorderedCache(arrayFunction);
}
/**
* @typedef {Object} ChunkModuleMaps
* @property {Record<string|number, (string|number)[]>} id
* @property {Record<string|number, string>} hash
*/
/**
* @param {Chunk} chunk the chunk
* @param {ModuleFilterPredicate} filterFn function used to filter modules
* @returns {ChunkModuleMaps} module map information
*/
getChunkModuleMaps(chunk, filterFn) {
/** @type {Record<string|number, (string|number)[]>} */
const chunkModuleIdMap = Object.create(null);
/** @type {Record<string|number, string>} */
const chunkModuleHashMap = Object.create(null);
for (const asyncChunk of chunk.getAllAsyncChunks()) {
/** @type {(string|number)[]} */
let array;
for (const module of this.getOrderedChunkModulesIterable(
asyncChunk,
compareModulesById
)) {
if (filterFn(module)) {
if (array === undefined) {
array = [];
chunkModuleIdMap[asyncChunk.id] = array;
}
array.push(module.id);
chunkModuleHashMap[module.id] = module.renderedHash;
}
}
if (array !== undefined) {
array.sort();
}
}
return {
id: chunkModuleIdMap,
hash: chunkModuleHashMap
};
}
/**
* @param {Chunk} chunk the chunk
* @param {function(Module): boolean} filterFn predicate function used to filter modules
* @param {(function(Chunk): boolean)=} filterChunkFn predicate function used to filter chunks
* @returns {boolean} return true if module exists in graph
*/
hasModuleInGraph(chunk, filterFn, filterChunkFn) {
const queue = new Set(chunk.groupsIterable);
const chunksProcessed = new Set();
for (const chunkGroup of queue) {
for (const innerChunk of chunkGroup.chunks) {
if (!chunksProcessed.has(innerChunk)) {
chunksProcessed.add(innerChunk);
if (!filterChunkFn || filterChunkFn(innerChunk)) {
for (const module of this.getChunkModulesIterable(innerChunk)) {
if (filterFn(module)) {
return true;
}
}
}
}
}
for (const child of chunkGroup.childrenIterable) {
queue.add(child);
}
}
return false;
}
/**
* @param {Chunk} chunkA first chunk
* @param {Chunk} chunkB second chunk
* @returns {-1|0|1} this is a comparitor function like sort and returns -1, 0, or 1 based on sort order
*/
compareChunks(chunkA, chunkB) {
const cgcA = this._getChunkGraphChunk(chunkA);
const cgcB = this._getChunkGraphChunk(chunkB);
if (cgcA.modules.size > cgcB.modules.size) return -1;
if (cgcA.modules.size < cgcB.modules.size) return 1;
cgcA.modules.sortWith(compareModulesById);
cgcB.modules.sortWith(compareModulesById);
const a = cgcA.modules[Symbol.iterator]();
const b = cgcB.modules[Symbol.iterator]();
// eslint-disable-next-line no-constant-condition
while (true) {
const aItem = a.next();
if (aItem.done) return 0;
const bItem = b.next();
const aModuleIdentifier = aItem.value.identifier();
const bModuleIdentifier = bItem.value.identifier();
if (aModuleIdentifier < bModuleIdentifier) return -1;
if (aModuleIdentifier > bModuleIdentifier) return 1;
}
}
/**
* @param {Chunk} chunk the chunk
* @returns {number} total size of all modules in the chunk
*/
getChunkModulesSize(chunk) {
const cgc = this._getChunkGraphChunk(chunk);
return cgc.modules.getFromUnorderedCache(getModulesSize);
}
/**
* @typedef {Object} ChunkSizeOptions
* @property {number=} chunkOverhead constant overhead for a chunk
* @property {number=} entryChunkMultiplicator multiplicator for initial chunks
*/
/**
* @param {Chunk} chunk the chunk
* @param {ChunkSizeOptions} options options object
* @returns {number} total size of the chunk
*/
getChunkSize(chunk, options) {
const cgc = this._getChunkGraphChunk(chunk);
const modulesSize = cgc.modules.getFromUnorderedCache(getModulesSize);
const chunkOverhead =
typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
const entryChunkMultiplicator =
typeof options.entryChunkMultiplicator === "number"
? options.entryChunkMultiplicator
: 10;
return (
chunkOverhead +
modulesSize * (chunk.canBeInitial() ? entryChunkMultiplicator : 1)
);
}
/**
* @param {Chunk} chunkA chunk
* @param {Chunk} chunkB chunk
* @param {ChunkSizeOptions} options options object
* @returns {number} total size of the chunk or false if chunks can't be integrated
*/
getIntegratedChunksSize(chunkA, chunkB, options) {
const cgcA = this._getChunkGraphChunk(chunkA);
const cgcB = this._getChunkGraphChunk(chunkB);
const allModules = new Set(cgcA.modules);
for (const m of cgcB.modules) allModules.add(m);
let modulesSize = 0;
for (const module of allModules) {
modulesSize += module.size();
}
const chunkOverhead =
typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
const entryChunkMultiplicator =
typeof options.entryChunkMultiplicator === "number"
? options.entryChunkMultiplicator
: 10;
return (
chunkOverhead +
modulesSize *
(chunkA.canBeInitial() || chunkB.canBeInitial()
? entryChunkMultiplicator
: 1)
);
}
/**
* @param {Chunk} chunkA chunk
* @param {Chunk} chunkB chunk
* @returns {boolean} true, if chunks could be integrated
*/
canChunksBeIntegrated(chunkA, chunkB) {
return chunkA.canBeIntegrated(chunkB);
}
/**
* @param {Chunk} chunkA the target chunk
* @param {Chunk} chunkB the chunk to integrate
* @returns {void}
*/
integrateChunks(chunkA, chunkB) {
// getChunkModules is used here to create a clone, because disconnectChunkAndModule modifies
for (const module of this.getChunkModules(chunkB)) {
this.disconnectChunkAndModule(chunkB, module);
this.connectChunkAndModule(chunkA, module);
}
for (const chunkGroup of chunkB.groupsIterable) {
chunkGroup.replaceChunk(chunkB, chunkA);
chunkA.addGroup(chunkGroup);
chunkB.removeGroup(chunkGroup);
}
// Decide for one name (deterministic)
if (chunkA.name && chunkB.name) {
if (chunkA.name.length !== chunkB.name.length) {
chunkA.name =
chunkA.name.length < chunkB.name.length ? chunkA.name : chunkB.name;
} else {
chunkA.name = chunkA.name < chunkB.name ? chunkA.name : chunkB.name;
}
}
}
}
module.exports = ChunkGraph;