feat: refactoring project
This commit is contained in:
63
node_modules/webpack/lib/optimize/AggressiveSplittingPlugin.js
generated
vendored
63
node_modules/webpack/lib/optimize/AggressiveSplittingPlugin.js
generated
vendored
@@ -36,11 +36,9 @@ const validate = createSchemaValidation(
|
||||
* @param {Chunk} newChunk the new chunk
|
||||
* @returns {(module: Module) => void} function to move module between chunks
|
||||
*/
|
||||
const moveModuleBetween = (chunkGraph, oldChunk, newChunk) => {
|
||||
return module => {
|
||||
chunkGraph.disconnectChunkAndModule(oldChunk, module);
|
||||
chunkGraph.connectChunkAndModule(newChunk, module);
|
||||
};
|
||||
const moveModuleBetween = (chunkGraph, oldChunk, newChunk) => module => {
|
||||
chunkGraph.disconnectChunkAndModule(oldChunk, module);
|
||||
chunkGraph.connectChunkAndModule(newChunk, module);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -48,11 +46,8 @@ const moveModuleBetween = (chunkGraph, oldChunk, newChunk) => {
|
||||
* @param {Chunk} chunk the chunk
|
||||
* @returns {function(Module): boolean} filter for entry module
|
||||
*/
|
||||
const isNotAEntryModule = (chunkGraph, chunk) => {
|
||||
return module => {
|
||||
return !chunkGraph.isEntryModuleInChunk(module, chunk);
|
||||
};
|
||||
};
|
||||
const isNotAEntryModule = (chunkGraph, chunk) => module =>
|
||||
!chunkGraph.isEntryModuleInChunk(module, chunk);
|
||||
|
||||
/** @type {WeakSet<Chunk>} */
|
||||
const recordedChunks = new WeakSet();
|
||||
@@ -97,10 +92,12 @@ class AggressiveSplittingPlugin {
|
||||
"AggressiveSplittingPlugin",
|
||||
compilation => {
|
||||
let needAdditionalSeal = false;
|
||||
/** @typedef {{ id?: NonNullable<Chunk["id"]>, hash?: NonNullable<Chunk["hash"]>, modules: Module[], size: number }} SplitData */
|
||||
/** @type {SplitData[]} */
|
||||
let newSplits;
|
||||
/** @type {Set<Chunk>} */
|
||||
let fromAggressiveSplittingSet;
|
||||
/** @type {Map<Chunk, TODO>} */
|
||||
/** @type {Map<Chunk, SplitData>} */
|
||||
let chunkSplitDataMap;
|
||||
compilation.hooks.optimize.tap("AggressiveSplittingPlugin", () => {
|
||||
newSplits = [];
|
||||
@@ -144,6 +141,10 @@ class AggressiveSplittingPlugin {
|
||||
const minSize = /** @type {number} */ (this.options.minSize);
|
||||
const maxSize = /** @type {number} */ (this.options.maxSize);
|
||||
|
||||
/**
|
||||
* @param {SplitData} splitData split data
|
||||
* @returns {boolean} true when applied, otherwise false
|
||||
*/
|
||||
const applySplit = splitData => {
|
||||
// Cannot split if id is already taken
|
||||
if (splitData.id !== undefined && usedIds.has(splitData.id)) {
|
||||
@@ -191,11 +192,11 @@ class AggressiveSplittingPlugin {
|
||||
const newChunk = compilation.addChunk();
|
||||
newChunk.chunkReason = "aggressive splitted";
|
||||
for (const chunk of selectedChunks) {
|
||||
selectedModules.forEach(
|
||||
moveModuleBetween(chunkGraph, chunk, newChunk)
|
||||
);
|
||||
for (const module of selectedModules) {
|
||||
moveModuleBetween(chunkGraph, chunk, newChunk)(module);
|
||||
}
|
||||
chunk.split(newChunk);
|
||||
chunk.name = null;
|
||||
chunk.name = /** @type {TODO} */ (null);
|
||||
}
|
||||
fromAggressiveSplittingSet.add(newChunk);
|
||||
chunkSplitDataMap.set(newChunk, splitData);
|
||||
@@ -250,6 +251,7 @@ class AggressiveSplittingPlugin {
|
||||
selectedModules.push(module);
|
||||
}
|
||||
if (selectedModules.length === 0) continue;
|
||||
/** @type {SplitData} */
|
||||
const splitData = {
|
||||
modules: selectedModules
|
||||
.map(m => moduleToNameMap.get(m))
|
||||
@@ -271,33 +273,42 @@ class AggressiveSplittingPlugin {
|
||||
records => {
|
||||
// 4. save made splittings to records
|
||||
const allSplits = new Set();
|
||||
/** @type {Set<SplitData>} */
|
||||
const invalidSplits = new Set();
|
||||
|
||||
// Check if some splittings are invalid
|
||||
// We remove invalid splittings and try again
|
||||
for (const chunk of compilation.chunks) {
|
||||
const splitData = chunkSplitDataMap.get(chunk);
|
||||
if (splitData !== undefined) {
|
||||
if (splitData.hash && chunk.hash !== splitData.hash) {
|
||||
// Split was successful, but hash doesn't equal
|
||||
// We can throw away the split since it's useless now
|
||||
invalidSplits.add(splitData);
|
||||
}
|
||||
if (
|
||||
splitData !== undefined &&
|
||||
splitData.hash &&
|
||||
chunk.hash !== splitData.hash
|
||||
) {
|
||||
// Split was successful, but hash doesn't equal
|
||||
// We can throw away the split since it's useless now
|
||||
invalidSplits.add(splitData);
|
||||
}
|
||||
}
|
||||
|
||||
if (invalidSplits.size > 0) {
|
||||
records.aggressiveSplits = records.aggressiveSplits.filter(
|
||||
splitData => !invalidSplits.has(splitData)
|
||||
);
|
||||
records.aggressiveSplits =
|
||||
/** @type {SplitData[]} */
|
||||
(records.aggressiveSplits).filter(
|
||||
splitData => !invalidSplits.has(splitData)
|
||||
);
|
||||
needAdditionalSeal = true;
|
||||
} else {
|
||||
// set hash and id values on all (new) splittings
|
||||
for (const chunk of compilation.chunks) {
|
||||
const splitData = chunkSplitDataMap.get(chunk);
|
||||
if (splitData !== undefined) {
|
||||
splitData.hash = chunk.hash;
|
||||
splitData.id = chunk.id;
|
||||
splitData.hash =
|
||||
/** @type {NonNullable<Chunk["hash"]>} */
|
||||
(chunk.hash);
|
||||
splitData.id =
|
||||
/** @type {NonNullable<Chunk["id"]>} */
|
||||
(chunk.id);
|
||||
allSplits.add(splitData);
|
||||
// set flag for stats
|
||||
recordedChunks.add(chunk);
|
||||
|
||||
Reference in New Issue
Block a user