Skip to content

Commit 0617b7d

Browse files
authored
refactor: restructure dependOn relationship retrieval (#19800)
1 parent cbfba9a commit 0617b7d

File tree

6 files changed

+52
-34
lines changed

6 files changed

+52
-34
lines changed

lib/ChunkGraph.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ class ChunkGraph {
12421242
* @param {Chunk} chunk the chunk
12431243
* @returns {Iterable<Chunk>} iterable of chunks and include chunks from children entrypoints
12441244
*/
1245-
getChunkEntryDependOnChunksIterable(chunk) {
1245+
getRuntimeChunkDependentChunksIterable(chunk) {
12461246
/** @type {Set<Chunk>} */
12471247
const set = new Set();
12481248

@@ -1259,7 +1259,7 @@ class ChunkGraph {
12591259

12601260
let hasChildrenEntrypoint = false;
12611261
for (const child of current.childrenIterable) {
1262-
if (child.isInitial()) {
1262+
if (child instanceof Entrypoint && child.dependOn(current)) {
12631263
hasChildrenEntrypoint = true;
12641264
queue.push(/** @type {Entrypoint} */ (child));
12651265
}
@@ -1277,6 +1277,18 @@ class ChunkGraph {
12771277
}
12781278
}
12791279
}
1280+
1281+
for (const entrypoint of entrypoints) {
1282+
const entrypointChunk = entrypoint.getEntrypointChunk();
1283+
const cgc = this._getChunkGraphChunk(entrypointChunk);
1284+
for (const chunkGroup of cgc.entryModules.values()) {
1285+
for (const c of chunkGroup.chunks) {
1286+
if (c !== chunk && c !== entrypointChunk && !c.hasRuntime()) {
1287+
set.add(c);
1288+
}
1289+
}
1290+
}
1291+
}
12801292
return set;
12811293
}
12821294

lib/Compilation.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ const ErrorHelpers = require("./ErrorHelpers");
3232
const FileSystemInfo = require("./FileSystemInfo");
3333
const {
3434
connectChunkGroupAndChunk,
35-
connectChunkGroupParentAndChild
35+
connectChunkGroupParentAndChild,
36+
connectEntrypointAndDependOn
3637
} = require("./GraphHelpers");
3738
const {
3839
makeWebpackError,
@@ -3207,7 +3208,6 @@ Remove the 'runtime' option from the entrypoint.`);
32073208
const referencedChunks = entry
32083209
.getEntrypointChunk()
32093210
.getAllReferencedChunks();
3210-
const dependOnEntries = [];
32113211
for (const dep of dependOn) {
32123212
const dependency = this.entrypoints.get(dep);
32133213
if (!dependency) {
@@ -3225,9 +3225,7 @@ Remove the 'runtime' option from the entrypoint.`);
32253225
entry.setRuntimeChunk(entryChunk);
32263226
continue outer;
32273227
}
3228-
dependOnEntries.push(dependency);
3229-
}
3230-
for (const dependency of dependOnEntries) {
3228+
connectEntrypointAndDependOn(entry, dependency);
32313229
connectChunkGroupParentAndChild(dependency, entry);
32323230
}
32333231
} else if (runtime) {
@@ -3278,26 +3276,6 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
32783276
}
32793277
this.hooks.afterOptimizeChunks.call(this.chunks, this.chunkGroups);
32803278

3281-
for (const [
3282-
name,
3283-
{
3284-
options: { dependOn }
3285-
}
3286-
] of this.entries) {
3287-
if (dependOn) {
3288-
const entry = /** @type {Entrypoint} */ (this.entrypoints.get(name));
3289-
for (const dep of dependOn) {
3290-
const depEntry = /** @type {Entrypoint} */ (
3291-
this.entrypoints.get(dep)
3292-
);
3293-
const runtimeChunk = depEntry.getRuntimeChunk();
3294-
if (runtimeChunk) {
3295-
runtimeChunk.addGroup(entry);
3296-
}
3297-
}
3298-
}
3299-
}
3300-
33013279
this.hooks.optimizeTree.callAsync(this.chunks, this.modules, (err) => {
33023280
if (err) {
33033281
return finalCallback(

lib/Entrypoint.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"use strict";
77

88
const ChunkGroup = require("./ChunkGroup");
9+
const SortableSet = require("./util/SortableSet");
910

1011
/** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescription */
1112
/** @typedef {import("./Chunk")} Chunk */
@@ -38,6 +39,8 @@ class Entrypoint extends ChunkGroup {
3839
this._entrypointChunk = undefined;
3940
/** @type {boolean} */
4041
this._initial = initial;
42+
/** @type {SortableSet<Entrypoint>} */
43+
this._dependOn = new SortableSet();
4144
}
4245

4346
/**
@@ -96,6 +99,22 @@ class Entrypoint extends ChunkGroup {
9699
if (this._entrypointChunk === oldChunk) this._entrypointChunk = newChunk;
97100
return super.replaceChunk(oldChunk, newChunk);
98101
}
102+
103+
/**
104+
* @param {Entrypoint} entrypoint the entrypoint
105+
* @returns {void}
106+
*/
107+
addDependOn(entrypoint) {
108+
this._dependOn.add(entrypoint);
109+
}
110+
111+
/**
112+
* @param {Entrypoint} entrypoint the entrypoint
113+
* @returns {boolean} true if the entrypoint is in the dependOn set
114+
*/
115+
dependOn(entrypoint) {
116+
return this._dependOn.has(entrypoint);
117+
}
99118
}
100119

101120
module.exports = Entrypoint;

lib/GraphHelpers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/** @typedef {import("./ChunkGroup")} ChunkGroup */
1111
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
1212
/** @typedef {import("./Module")} Module */
13+
/** @typedef {import(".").Entrypoint} Entrypoint */
1314

1415
/**
1516
* @param {ChunkGroup} chunkGroup the ChunkGroup to connect
@@ -33,6 +34,16 @@ const connectChunkGroupParentAndChild = (parent, child) => {
3334
}
3435
};
3536

37+
/**
38+
* @param {Entrypoint} entrypoint the entrypoint
39+
* @param {Entrypoint} dependOnEntrypoint the dependOnEntrypoint
40+
* @returns {void}
41+
*/
42+
const connectEntrypointAndDependOn = (entrypoint, dependOnEntrypoint) => {
43+
entrypoint.addDependOn(dependOnEntrypoint);
44+
};
45+
3646
module.exports.connectChunkGroupAndChunk = connectChunkGroupAndChunk;
3747
module.exports.connectChunkGroupParentAndChild =
3848
connectChunkGroupParentAndChild;
49+
module.exports.connectEntrypointAndDependOn = connectEntrypointAndDependOn;

lib/runtime/GetChunkFilenameRuntimeModule.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,12 @@ class GetChunkFilenameRuntimeModule extends RuntimeModule {
101101
.getTreeRuntimeRequirements(chunk)
102102
.has(RuntimeGlobals.ensureChunkIncludeEntries);
103103
if (includeEntries) {
104-
includedChunksMessages.push("sibling chunks for the entrypoint");
105-
for (const c of chunkGraph.getChunkEntryDependentChunksIterable(
104+
includedChunksMessages.push("chunks that the entrypoint depends on");
105+
for (const c of chunkGraph.getRuntimeChunkDependentChunksIterable(
106106
chunk
107107
)) {
108108
addChunk(c);
109109
}
110-
includedChunksMessages.push("chunks that the entrypoint depends on");
111-
for (const c of chunkGraph.getChunkEntryDependOnChunksIterable(chunk)) {
112-
addChunk(c);
113-
}
114110
}
115111
}
116112
for (const entrypoint of chunk.getAllReferencedAsyncEntrypoints()) {

types.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ declare class ChunkGraph {
13731373
getNumberOfRuntimeModules(chunk: Chunk): number;
13741374
getChunkEntryModulesIterable(chunk: Chunk): Iterable<Module>;
13751375
getChunkEntryDependentChunksIterable(chunk: Chunk): Iterable<Chunk>;
1376-
getChunkEntryDependOnChunksIterable(chunk: Chunk): Iterable<Chunk>;
1376+
getRuntimeChunkDependentChunksIterable(chunk: Chunk): Iterable<Chunk>;
13771377
hasChunkEntryDependentChunks(chunk: Chunk): boolean;
13781378
getChunkRuntimeModulesIterable(chunk: Chunk): Iterable<RuntimeModule>;
13791379
getChunkRuntimeModulesInOrder(chunk: Chunk): RuntimeModule[];
@@ -4534,6 +4534,8 @@ declare abstract class Entrypoint extends ChunkGroup {
45344534
* (or at least the execution of them)
45354535
*/
45364536
getEntrypointChunk(): Chunk;
4537+
addDependOn(entrypoint: Entrypoint): void;
4538+
dependOn(entrypoint: Entrypoint): boolean;
45374539
}
45384540
type EnumValue =
45394541
| null

0 commit comments

Comments
 (0)