Skip to content

Commit 451b4f5

Browse files
authored
fix(manifest): correct shared assets and filter expose assets (#3679)
1 parent c27f91b commit 451b4f5

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

.changeset/green-countries-beg.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/manifest': patch
3+
---
4+
5+
fix(manifest): correct shared assets and filter expose assets

packages/manifest/src/StatsManager.ts

+45-27
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,11 @@ class StatsManager {
187187

188188
private _getModuleAssets(
189189
compilation: Compilation,
190+
entryPointNames: string[],
190191
): Record<string, StatsAssets> {
191192
const { chunks } = compilation;
192193
const { exposeFileNameImportMap } = this._containerManager;
193194
const assets: Record<string, StatsAssets> = {};
194-
const entryPointNames = [...compilation.entrypoints.values()]
195-
.map((e) => e.name)
196-
.filter((v) => !!v) as Array<string>;
197195

198196
chunks.forEach((chunk) => {
199197
if (
@@ -214,6 +212,7 @@ class StatsManager {
214212
private _getProvideSharedAssets(
215213
compilation: Compilation,
216214
stats: StatsCompilation,
215+
entryPointNames: string[],
217216
): StatsAssets {
218217
const sharedModules = stats.modules!.filter((module) => {
219218
if (!module || !module.name) {
@@ -241,23 +240,14 @@ class StatsManager {
241240
const chunk = findChunk(chunkID, compilation.chunks);
242241

243242
manifestOverrideChunkIDMap[sharedModuleName].sync.add(chunkID);
244-
Array.from(chunk!.getAllInitialChunks() as Iterable<Chunk>).forEach(
245-
(syncChunk: Chunk) => {
246-
syncChunk.id &&
247-
manifestOverrideChunkIDMap[sharedModuleName].sync.add(
248-
syncChunk.id,
249-
);
250-
},
251-
);
252-
253-
Array.from(chunk!.getAllAsyncChunks() as Iterable<Chunk>).forEach(
254-
(asyncChunk: Chunk) => {
255-
asyncChunk.id &&
256-
manifestOverrideChunkIDMap[sharedModuleName].async.add(
257-
asyncChunk.id,
258-
);
259-
},
260-
);
243+
if (!chunk) {
244+
return;
245+
}
246+
[...chunk.groupsIterable].forEach((group) => {
247+
if (group.name && !entryPointNames.includes(group.name)) {
248+
manifestOverrideChunkIDMap[sharedModuleName].sync.add(group.id);
249+
}
250+
});
261251
});
262252
});
263253

@@ -360,12 +350,16 @@ class StatsManager {
360350
bundler: this._bundler,
361351
});
362352
const { remotes, exposesMap, sharedMap } = moduleHandler.collect();
353+
const entryPointNames = [...compilation.entrypoints.values()]
354+
.map((e) => e.name)
355+
.filter((v) => !!v) as Array<string>;
363356

364357
await Promise.all([
365358
new Promise<void>((resolve) => {
366359
const sharedAssets = this._getProvideSharedAssets(
367360
compilation,
368361
webpackStats,
362+
entryPointNames,
369363
);
370364

371365
Object.keys(sharedMap).forEach((sharedKey) => {
@@ -377,7 +371,10 @@ class StatsManager {
377371
resolve();
378372
}),
379373
new Promise<void>((resolve) => {
380-
const moduleAssets = this._getModuleAssets(compilation);
374+
const moduleAssets = this._getModuleAssets(
375+
compilation,
376+
entryPointNames,
377+
);
381378

382379
Object.keys(exposesMap).forEach((exposeKey) => {
383380
const assets = moduleAssets[exposeKey];
@@ -418,14 +415,35 @@ class StatsManager {
418415
}));
419416
resolve();
420417
}),
421-
new Promise<void>((resolve) => {
422-
stats.exposes = Object.values(exposesMap).map((expose) => ({
423-
...expose,
424-
}));
425-
resolve();
426-
}),
427418
]);
428419

420+
await new Promise<void>((resolve) => {
421+
const sharedAssets = stats.shared.reduce((sum, shared) => {
422+
const { js, css } = shared.assets;
423+
[...js.sync, ...js.async, ...css.async, css.sync].forEach((asset) => {
424+
sum.add(asset);
425+
});
426+
return sum;
427+
}, new Set());
428+
stats.exposes = Object.values(exposesMap).map((expose) => {
429+
const { js, css } = expose.assets;
430+
return {
431+
...expose,
432+
assets: {
433+
js: {
434+
sync: js.sync.filter((asset) => !sharedAssets.has(asset)),
435+
async: js.async.filter((asset) => !sharedAssets.has(asset)),
436+
},
437+
css: {
438+
sync: css.sync.filter((asset) => !sharedAssets.has(asset)),
439+
async: css.async.filter((asset) => !sharedAssets.has(asset)),
440+
},
441+
},
442+
};
443+
});
444+
resolve();
445+
});
446+
429447
return stats;
430448
} catch (err) {
431449
throw err;

0 commit comments

Comments
 (0)