diff --git a/.changeset/green-countries-beg.md b/.changeset/green-countries-beg.md new file mode 100644 index 00000000000..d55f7fb8542 --- /dev/null +++ b/.changeset/green-countries-beg.md @@ -0,0 +1,5 @@ +--- +'@module-federation/manifest': patch +--- + +fix(manifest): correct shared assets and filter expose assets diff --git a/packages/manifest/src/StatsManager.ts b/packages/manifest/src/StatsManager.ts index c37e079c51a..964eb869422 100644 --- a/packages/manifest/src/StatsManager.ts +++ b/packages/manifest/src/StatsManager.ts @@ -187,13 +187,11 @@ class StatsManager { private _getModuleAssets( compilation: Compilation, + entryPointNames: string[], ): Record { const { chunks } = compilation; const { exposeFileNameImportMap } = this._containerManager; const assets: Record = {}; - const entryPointNames = [...compilation.entrypoints.values()] - .map((e) => e.name) - .filter((v) => !!v) as Array; chunks.forEach((chunk) => { if ( @@ -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) { @@ -241,23 +240,14 @@ class StatsManager { const chunk = findChunk(chunkID, compilation.chunks); manifestOverrideChunkIDMap[sharedModuleName].sync.add(chunkID); - Array.from(chunk!.getAllInitialChunks() as Iterable).forEach( - (syncChunk: Chunk) => { - syncChunk.id && - manifestOverrideChunkIDMap[sharedModuleName].sync.add( - syncChunk.id, - ); - }, - ); - - Array.from(chunk!.getAllAsyncChunks() as Iterable).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); + } + }); }); }); @@ -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; await Promise.all([ new Promise((resolve) => { const sharedAssets = this._getProvideSharedAssets( compilation, webpackStats, + entryPointNames, ); Object.keys(sharedMap).forEach((sharedKey) => { @@ -377,7 +371,10 @@ class StatsManager { resolve(); }), new Promise((resolve) => { - const moduleAssets = this._getModuleAssets(compilation); + const moduleAssets = this._getModuleAssets( + compilation, + entryPointNames, + ); Object.keys(exposesMap).forEach((exposeKey) => { const assets = moduleAssets[exposeKey]; @@ -418,14 +415,35 @@ class StatsManager { })); resolve(); }), - new Promise((resolve) => { - stats.exposes = Object.values(exposesMap).map((expose) => ({ - ...expose, - })); - resolve(); - }), ]); + await new Promise((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;