Skip to content

Commit b120511

Browse files
feat(enhanced): improve async boundary for sibling chunk dep (#2350)
Co-authored-by: ScriptedAlchemy <[email protected]>
1 parent 56eb56a commit b120511

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

.changeset/pink-bulldogs-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/enhanced': patch
3+
---
4+
5+
Improve Async Boundary Plugin on entry that use dependOn and improve chunk dep search

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ jobs:
6868
run: pnpm app:next:build
6969

7070
- name: E2E Node Federation
71-
run: npx nx run-many --target=serve --projects=node-local-remote,node-remote --parallel=3 & echo "done" && sleep 10 && npx nx run-many --target=serve --projects=node-host & echo "done" && npx nx run node-host-e2e:test:e2e
71+
run: npx nx run-many --target=serve --projects=node-local-remote,node-remote --parallel=3 & echo "done" && sleep 10 && npx nx run-many --target=serve --projects=node-host & npx wait-on tcp:3333 && npx nx run node-host-e2e:test:e2e

packages/enhanced/src/lib/container/AsyncBoundaryPlugin.ts

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,14 @@ class AsyncEntryStartupPlugin {
7070
getChunkByName(
7171
compilation: Compilation,
7272
dependOn: string[],
73-
): (string | number | undefined)[] {
74-
const byname = [];
73+
byname: Set<Chunk>,
74+
) {
7575
for (const name of dependOn) {
7676
const chunk = compilation.namedChunks.get(name);
7777
if (chunk) {
78-
byname.push(chunk.id || chunk.name);
78+
byname.add(chunk);
7979
}
8080
}
81-
return byname;
8281
}
8382

8483
private _handleRenderStartup(compiler: Compiler, compilation: Compilation) {
@@ -118,36 +117,59 @@ class AsyncEntryStartupPlugin {
118117

119118
const requirements =
120119
compilation.chunkGraph.getTreeRuntimeRequirements(runtimeItem);
121-
const hasRemoteModules =
122-
compilation.chunkGraph.getChunkModulesIterableBySourceType(
123-
upperContext.chunk,
124-
'remote',
125-
);
126-
const consumeShares =
127-
compilation.chunkGraph.getChunkModulesIterableBySourceType(
128-
upperContext.chunk,
129-
'consume-shared',
130-
);
120+
131121
const entryOptions = upperContext.chunk.getEntryOptions();
132-
const initialChunks = Array.from(
133-
upperContext.chunk.getAllInitialChunks(),
134-
).map((chunk: Chunk) => chunk.id);
122+
const chunkInitialsSet = new Set(
123+
compilation.chunkGraph.getChunkEntryDependentChunksIterable(
124+
upperContext.chunk,
125+
),
126+
);
127+
128+
chunkInitialsSet.add(upperContext.chunk);
135129
const dependOn = entryOptions?.dependOn || [];
136-
const dependOnIDs = this.getChunkByName(compilation, dependOn);
137-
const chunksToRef = [...dependOnIDs, ...initialChunks];
130+
this.getChunkByName(compilation, dependOn, chunkInitialsSet);
131+
132+
const initialChunks = [];
133+
134+
let hasRemoteModules = false;
135+
let consumeShares = false;
136+
137+
for (const chunk of chunkInitialsSet) {
138+
initialChunks.push(chunk.id);
139+
if (!hasRemoteModules) {
140+
hasRemoteModules = Boolean(
141+
compilation.chunkGraph.getChunkModulesIterableBySourceType(
142+
chunk,
143+
'remote',
144+
),
145+
);
146+
}
147+
if (!consumeShares) {
148+
consumeShares = Boolean(
149+
compilation.chunkGraph.getChunkModulesIterableBySourceType(
150+
chunk,
151+
'consume-shared',
152+
),
153+
);
154+
}
155+
if (hasRemoteModules && consumeShares) {
156+
break;
157+
}
158+
}
138159

139160
remotes = this._getRemotes(
140161
compiler.webpack.RuntimeGlobals,
141162
requirements,
142-
Boolean(hasRemoteModules),
143-
chunksToRef,
163+
hasRemoteModules,
164+
initialChunks,
144165
remotes,
145166
);
167+
146168
shared = this._getShared(
147169
compiler.webpack.RuntimeGlobals,
148170
requirements,
149-
Boolean(consumeShares),
150-
chunksToRef,
171+
consumeShares,
172+
initialChunks,
151173
shared,
152174
);
153175
}

0 commit comments

Comments
 (0)