Skip to content

fix(manifest): correct shared assets and filter expose assets #3679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-countries-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/manifest': patch
---

fix(manifest): correct shared assets and filter expose assets
72 changes: 45 additions & 27 deletions packages/manifest/src/StatsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,11 @@ class StatsManager {

private _getModuleAssets(
compilation: Compilation,
entryPointNames: string[],
): Record<string, StatsAssets> {
const { chunks } = compilation;
const { exposeFileNameImportMap } = this._containerManager;
const assets: Record<string, StatsAssets> = {};
const entryPointNames = [...compilation.entrypoints.values()]
.map((e) => e.name)
.filter((v) => !!v) as Array<string>;

chunks.forEach((chunk) => {
if (
Expand All @@ -214,6 +212,7 @@ class StatsManager {
private _getProvideSharedAssets(
compilation: Compilation,
stats: StatsCompilation,
entryPointNames: string[],
): StatsAssets {
const sharedModules = stats.modules!.filter((module) => {
if (!module || !module.name) {
Expand Down Expand Up @@ -241,23 +240,14 @@ class StatsManager {
const chunk = findChunk(chunkID, compilation.chunks);

manifestOverrideChunkIDMap[sharedModuleName].sync.add(chunkID);
Array.from(chunk!.getAllInitialChunks() as Iterable<Chunk>).forEach(
(syncChunk: Chunk) => {
syncChunk.id &&
manifestOverrideChunkIDMap[sharedModuleName].sync.add(
syncChunk.id,
);
},
);

Array.from(chunk!.getAllAsyncChunks() as Iterable<Chunk>).forEach(
(asyncChunk: Chunk) => {
asyncChunk.id &&
manifestOverrideChunkIDMap[sharedModuleName].async.add(
asyncChunk.id,
);
},
);
if (!chunk) {
return;
}
[...chunk.groupsIterable].forEach((group) => {
if (group.name && !entryPointNames.includes(group.name)) {
manifestOverrideChunkIDMap[sharedModuleName].sync.add(group.id);
}
});
});
});

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

await Promise.all([
new Promise<void>((resolve) => {
const sharedAssets = this._getProvideSharedAssets(
compilation,
webpackStats,
entryPointNames,
);

Object.keys(sharedMap).forEach((sharedKey) => {
Expand All @@ -377,7 +371,10 @@ class StatsManager {
resolve();
}),
new Promise<void>((resolve) => {
const moduleAssets = this._getModuleAssets(compilation);
const moduleAssets = this._getModuleAssets(
compilation,
entryPointNames,
);

Object.keys(exposesMap).forEach((exposeKey) => {
const assets = moduleAssets[exposeKey];
Expand Down Expand Up @@ -418,14 +415,35 @@ class StatsManager {
}));
resolve();
}),
new Promise<void>((resolve) => {
stats.exposes = Object.values(exposesMap).map((expose) => ({
...expose,
}));
resolve();
}),
]);

await new Promise<void>((resolve) => {
const sharedAssets = stats.shared.reduce((sum, shared) => {
const { js, css } = shared.assets;
[...js.sync, ...js.async, ...css.async, css.sync].forEach((asset) => {
sum.add(asset);
});
return sum;
}, new Set());
stats.exposes = Object.values(exposesMap).map((expose) => {
const { js, css } = expose.assets;
return {
...expose,
assets: {
js: {
sync: js.sync.filter((asset) => !sharedAssets.has(asset)),
async: js.async.filter((asset) => !sharedAssets.has(asset)),
},
css: {
sync: css.sync.filter((asset) => !sharedAssets.has(asset)),
async: css.async.filter((asset) => !sharedAssets.has(asset)),
},
},
};
});
resolve();
});

return stats;
} catch (err) {
throw err;
Expand Down