Skip to content

fix(webpack-bundler-runtime): preserve share scopes on repeated init - #4698

Open
2heal1 wants to merge 2 commits into
module-federation:mainfrom
2heal1:fix/issue-4691
Open

fix(webpack-bundler-runtime): preserve share scopes on repeated init#4698
2heal1 wants to merge 2 commits into
module-federation:mainfrom
2heal1:fix/issue-4691

Conversation

@2heal1

@2heal1 2heal1 commented Apr 29, 2026

Copy link
Copy Markdown
Member

Reuse an existing non-default share scope when repeated container init receives an incomplete host shareScopeMap instead of overwriting it with an empty scope or the wrong fallback.

Add a regression test covering repeated init with empty and missing custom share-scope entries, and keep the related initContainerEntry array/non-array test suites green.

Description

Related Issue

#4691

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

Reuse an existing non-default share scope when repeated container init receives an incomplete host shareScopeMap instead of overwriting it with an empty scope or the wrong fallback.

Add a regression test covering repeated init with empty and missing custom share-scope entries, and keep the related initContainerEntry array/non-array test suites green.
@changeset-bot

changeset-bot Bot commented Apr 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0760c76

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 45 packages
Name Type
@module-federation/webpack-bundler-runtime Patch
@module-federation/enhanced Patch
@module-federation/esbuild Patch
@module-federation/nextjs-mf Patch
@module-federation/runtime-tools Patch
@module-federation/modern-js-v3 Patch
@module-federation/modern-js Patch
@module-federation/node Patch
@module-federation/rsbuild-plugin Patch
@module-federation/rspress-plugin Patch
@module-federation/storybook-addon Patch
shared-tree-shaking-no-server-host Patch
shared-tree-shaking-no-server-provider Patch
@module-federation/rspack Patch
@module-federation/inject-external-runtime-core-plugin Patch
shared-tree-shaking-with-server-host Patch
shared-tree-shaking-with-server-provider Patch
node-dynamic-remote-new-version Patch
node-dynamic-remote Patch
remote5 Patch
remote6 Patch
website-new Patch
@module-federation/runtime Patch
@module-federation/sdk Patch
@module-federation/managers Patch
@module-federation/manifest Patch
@module-federation/dts-plugin Patch
@module-federation/third-party-dts-extractor Patch
@module-federation/devtools Patch
@module-federation/bridge-react Patch
@module-federation/bridge-vue3 Patch
@module-federation/bridge-shared Patch
@module-federation/bridge-react-webpack-plugin Patch
@module-federation/retry-plugin Patch
@module-federation/data-prefetch Patch
@module-federation/error-codes Patch
@module-federation/runtime-core Patch
create-module-federation Patch
@module-federation/cli Patch
@module-federation/treeshake-server Patch
@module-federation/treeshake-frontend Patch
@module-federation/metro Patch
@module-federation/metro-plugin-rnef Patch
@module-federation/metro-plugin-rnc-cli Patch
@module-federation/utilities Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify

netlify Bot commented Apr 29, 2026

Copy link
Copy Markdown

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 0760c76
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/6a1dba9ae32aa9000894170a
😎 Deploy Preview https://deploy-preview-4698--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new

pkg-pr-new Bot commented Apr 29, 2026

Copy link
Copy Markdown

Open in StackBlitz

@module-federation/devtools

pnpm add https://pkg.pr.new/@module-federation/devtools@0760c76

@module-federation/cli

pnpm add https://pkg.pr.new/@module-federation/cli@0760c76

create-module-federation

pnpm add https://pkg.pr.new/create-module-federation@0760c76

@module-federation/dts-plugin

pnpm add https://pkg.pr.new/@module-federation/dts-plugin@0760c76

@module-federation/enhanced

pnpm add https://pkg.pr.new/@module-federation/enhanced@0760c76

@module-federation/error-codes

pnpm add https://pkg.pr.new/@module-federation/error-codes@0760c76

@module-federation/esbuild

pnpm add https://pkg.pr.new/@module-federation/esbuild@0760c76

@module-federation/managers

pnpm add https://pkg.pr.new/@module-federation/managers@0760c76

@module-federation/manifest

pnpm add https://pkg.pr.new/@module-federation/manifest@0760c76

@module-federation/metro

pnpm add https://pkg.pr.new/@module-federation/metro@0760c76

@module-federation/metro-plugin-rnc-cli

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rnc-cli@0760c76

@module-federation/metro-plugin-rnef

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rnef@0760c76

@module-federation/metro-plugin-rock

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rock@0760c76

@module-federation/modern-js

pnpm add https://pkg.pr.new/@module-federation/modern-js@0760c76

@module-federation/modern-js-v3

pnpm add https://pkg.pr.new/@module-federation/modern-js-v3@0760c76

@module-federation/native-federation-tests

pnpm add https://pkg.pr.new/@module-federation/native-federation-tests@0760c76

@module-federation/native-federation-typescript

pnpm add https://pkg.pr.new/@module-federation/native-federation-typescript@0760c76

@module-federation/nextjs-mf

pnpm add https://pkg.pr.new/@module-federation/nextjs-mf@0760c76

@module-federation/node

pnpm add https://pkg.pr.new/@module-federation/node@0760c76

@module-federation/observability-plugin

pnpm add https://pkg.pr.new/@module-federation/observability-plugin@0760c76

@module-federation/retry-plugin

pnpm add https://pkg.pr.new/@module-federation/retry-plugin@0760c76

@module-federation/rsbuild-plugin

pnpm add https://pkg.pr.new/@module-federation/rsbuild-plugin@0760c76

@module-federation/rspack

pnpm add https://pkg.pr.new/@module-federation/rspack@0760c76

@module-federation/rspress-plugin

pnpm add https://pkg.pr.new/@module-federation/rspress-plugin@0760c76

@module-federation/runtime

pnpm add https://pkg.pr.new/@module-federation/runtime@0760c76

@module-federation/runtime-core

pnpm add https://pkg.pr.new/@module-federation/runtime-core@0760c76

@module-federation/runtime-tools

pnpm add https://pkg.pr.new/@module-federation/runtime-tools@0760c76

@module-federation/sdk

pnpm add https://pkg.pr.new/@module-federation/sdk@0760c76

@module-federation/storybook-addon

pnpm add https://pkg.pr.new/@module-federation/storybook-addon@0760c76

@module-federation/third-party-dts-extractor

pnpm add https://pkg.pr.new/@module-federation/third-party-dts-extractor@0760c76

@module-federation/treeshake-frontend

pnpm add https://pkg.pr.new/@module-federation/treeshake-frontend@0760c76

@module-federation/treeshake-server

pnpm add https://pkg.pr.new/@module-federation/treeshake-server@0760c76

@module-federation/typescript

pnpm add https://pkg.pr.new/@module-federation/typescript@0760c76

@module-federation/utilities

pnpm add https://pkg.pr.new/@module-federation/utilities@0760c76

@module-federation/webpack-bundler-runtime

pnpm add https://pkg.pr.new/@module-federation/webpack-bundler-runtime@0760c76

@module-federation/bridge-react

pnpm add https://pkg.pr.new/@module-federation/bridge-react@0760c76

@module-federation/bridge-react-webpack-plugin

pnpm add https://pkg.pr.new/@module-federation/bridge-react-webpack-plugin@0760c76

@module-federation/bridge-shared

pnpm add https://pkg.pr.new/@module-federation/bridge-shared@0760c76

@module-federation/bridge-vue3

pnpm add https://pkg.pr.new/@module-federation/bridge-vue3@0760c76

@module-federation/inject-external-runtime-core-plugin

pnpm add https://pkg.pr.new/@module-federation/inject-external-runtime-core-plugin@0760c76

commit: 0760c76

@github-actions

github-actions Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Report

30 package(s) changed, 11 unchanged.

Package dist + ESM entry

Package Total dist (raw) Delta ESM gzip Delta
@module-federation/bridge-react 376.4 kB +14.3 kB (+3.9%) 1.3 kB +3 B (+0.2%)
@module-federation/bridge-vue3 171.1 kB +24.5 kB (+16.7%) 24.8 kB +2.4 kB (+10.5%)
@module-federation/cli 26.3 kB no change 786 B no change
@module-federation/core 39.1 kB no change 173 B no change
@module-federation/devtools 664.0 kB +176.0 kB (+36.1%) 4.0 kB +120 B (+3.0%)
@module-federation/dts-plugin 312.1 kB +6.0 kB (+1.9%) 4.7 kB +55 B (+1.2%)
@module-federation/enhanced 808.0 kB +1.4 kB (+0.2%) 672 B no change
@module-federation/error-codes 27.0 kB +1.9 kB (+7.4%) 210 B +10 B (+5.0%)
@module-federation/inject-external-runtime-core-plugin 5.2 kB no change 519 B +1 B (+0.2%)
@module-federation/managers 69.8 kB no change 334 B no change
@module-federation/manifest 136.1 kB no change 182 B no change
@module-federation/metro 277.2 kB +33.4 kB (+13.7%) 147 B no change
@module-federation/metro-plugin-rnc-cli 0 B no change 314 B no change
@module-federation/metro-plugin-rock 8.5 kB +8.5 kB (+∞%) 111 B +111 B (+∞%)
@module-federation/modern-js 190.9 kB +2.2 kB (+1.2%) 1.2 kB no change
@module-federation/modern-js-v3 182.1 kB +2.2 kB (+1.2%) 875 B no change
@module-federation/node 193.4 kB no change 217 B no change
@module-federation/observability-plugin 281.6 kB +281.6 kB (+∞%) 150 B +150 B (+∞%)
@module-federation/retry-plugin 65.8 kB +4.1 kB (+6.7%) 2.8 kB no change
@module-federation/rspress-plugin 19.9 kB +8.7 kB (+76.9%) 4.6 kB +1.7 kB (+59.2%)
@module-federation/runtime 20.0 kB +460 B (+2.3%) 715 B +99 B (+16.1%)
@module-federation/runtime-core 287.4 kB +34.7 kB (+13.7%) 477 B no change
@module-federation/sdk 121.3 kB +2.2 kB (+1.9%) 785 B no change
@module-federation/storybook-addon 78.6 kB +1.1 kB (+1.4%) 100 B no change
@module-federation/third-party-dts-extractor 17.9 kB +20 B (+0.1%) 1.9 kB no change
@module-federation/treeshake-frontend 629.3 kB +4.5 kB (+0.7%) 0 B no change
@module-federation/treeshake-server 812.0 kB +4.5 kB (+0.6%) 8.0 kB no change
@module-federation/utilities 110.6 kB no change 328 B no change
@module-federation/webpack-bundler-runtime 99.8 kB +5.9 kB (+6.3%) 405 B no change
create-module-federation 12.2 kB no change 3.5 kB no change

Bundle targets

Package Web bundle (gzip) Delta Node bundle (gzip) Delta
@module-federation/bridge-react 17.9 kB +433 B (+2.4%) 18.3 kB +442 B (+2.4%)
@module-federation/bridge-vue3 18.8 kB +1.7 kB (+9.6%) 18.6 kB +1.7 kB (+9.9%)
@module-federation/cli 2.3 kB -34 B (-1.4%) 2.3 kB -34 B (-1.4%)
@module-federation/core 1.1 kB -34 B (-3.0%) 1.0 kB -32 B (-3.0%)
@module-federation/devtools 30.2 kB +8.9 kB (+41.6%) 30.2 kB +8.9 kB (+41.6%)
@module-federation/dts-plugin 14.4 kB +63 B (+0.4%) 14.4 kB +63 B (+0.4%)
@module-federation/enhanced 2.6 kB -46 B (-1.7%) 2.6 kB -46 B (-1.7%)
@module-federation/error-codes 1012 B +75 B (+8.0%) 1012 B +75 B (+8.0%)
@module-federation/inject-external-runtime-core-plugin 396 B +1 B (+0.3%) 396 B +1 B (+0.3%)
@module-federation/managers 2.4 kB -28 B (-1.1%) 2.4 kB -28 B (-1.1%)
@module-federation/manifest 6.1 kB -41 B (-0.7%) 6.1 kB -41 B (-0.7%)
@module-federation/metro 9.5 kB +1.6 kB (+20.0%) 9.4 kB +1.6 kB (+20.2%)
@module-federation/metro-plugin-rnc-cli 411 B -27 B (-6.2%) 411 B -27 B (-6.2%)
@module-federation/metro-plugin-rock 599 B +599 B (+∞%) 599 B +599 B (+∞%)
@module-federation/modern-js 4.7 kB no change 4.7 kB no change
@module-federation/modern-js-v3 4.5 kB no change 4.5 kB no change
@module-federation/node 9.2 kB -28 B (-0.3%) 9.2 kB -28 B (-0.3%)
@module-federation/observability-plugin 13.1 kB +13.1 kB (+∞%) 13.1 kB +13.1 kB (+∞%)
@module-federation/retry-plugin 1.8 kB no change 1.8 kB no change
@module-federation/rspress-plugin 3.2 kB +1.1 kB (+51.3%) 3.2 kB +1.1 kB (+51.3%)
@module-federation/runtime 687 B +62 B (+9.9%) 687 B +62 B (+9.9%)
@module-federation/runtime-core 15.2 kB +1.5 kB (+11.3%) 15.0 kB +1.6 kB (+11.9%)
@module-federation/sdk 4.5 kB +15 B (+0.3%) 5.4 kB +15 B (+0.3%)
@module-federation/storybook-addon 1.9 kB -26 B (-1.3%) 1.7 kB -26 B (-1.4%)
@module-federation/third-party-dts-extractor 1.2 kB no change 1.2 kB no change
@module-federation/treeshake-frontend n/a n/a n/a n/a
@module-federation/treeshake-server 5.7 kB no change 5.7 kB no change
@module-federation/utilities 2.6 kB -33 B (-1.2%) 2.6 kB -33 B (-1.2%)
@module-federation/webpack-bundler-runtime 4.1 kB +113 B (+2.7%) 4.1 kB +113 B (+2.7%)
create-module-federation 2.5 kB -1 B (-0.0%) 2.5 kB -1 B (-0.0%)

Tree-shakable entrypoints

Package Export Entry gzip Delta Web bundle (gzip) Delta Node bundle (gzip) Delta Gap (node-web) Delta
@module-federation/runtime ./bundler 177 B no change 687 B +62 B (+9.9%) 687 B +62 B (+9.9%) 0 B 0 B
@module-federation/webpack-bundler-runtime ./bundler 151 B no change 4.1 kB +113 B (+2.7%) 4.1 kB +113 B (+2.7%) 0 B 0 B

Consumer scenarios

Scenario Web output (gzip) Delta Node output (gzip) Delta Gap (node-web) Delta
Enhanced remoteEntry 21.5 kB +1.7 kB (+8.7%) 22.6 kB +1.8 kB (+8.6%) +1.1 kB +70 B

Total dist (raw): 6.80 MB (+617.9 kB (+9.7%))
Total ESM gzip: 78.3 kB (+4.6 kB (+6.2%))
Total web bundle (gzip): 206.6 kB (+28.8 kB (+16.2%))
Total node bundle (gzip): 207.1 kB (+28.9 kB (+16.2%))
Tracked ./bundler entry gzip: 556 B (no change)
Tracked ./bundler web bundle (gzip): 4.9 kB (+175 B (+3.6%))
Tracked ./bundler node bundle (gzip): 4.9 kB (+175 B (+3.6%))

Bundle sizes are generated with rslib (Rspack). Package-root metrics preserve the historical report. Tracked subpath exports such as ./bundler are measured separately so ENV_TARGET-driven tree-shaking is visible. Bare imports are externalized to keep package-level sizes consistent, and assets are emitted as resources.

};

// host: 'default' remote: 'default' remote['default'] = hostShareScopeMap['default']
// host: ['default', 'scope1'] remote: 'default' remote['default'] = hostShareScopeMap['default']; remote['scope1'] = hostShareScopeMap['scop1']

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: hostShareScopeMap['scop1'] → scope1

expect(mockIFunction).toHaveBeenCalledTimes(3);
});

test('should preserve non-default share scopes across repeated init with incomplete host shareScopeMap', () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions cover persistence of custom, but the visible symptom in #4691 was a cross-scope leak — entries showing up under both default and custom. Could you add an isolation assertion here, e.g. expect(federationInstance.shareScopeMap.default).not.toHaveProperty('') (or assert default's keys are unchanged after this re-init)? That locks in the actual user-facing bug, not just that custom survives.

Comment on lines +37 to +66
const resolveShareScope = (
key: string,
fallbackShareScope: Record<string, any>,
options: { fallbackWhenEmpty?: boolean } = {},
) => {
const currentShareScope = hostShareScopeMap[key];

if (
hasOwnScope(hostShareScopeMap, key) &&
!isEmptyShareScope(currentShareScope)
) {
return currentShareScope;
}

if (
hasOwnScope(existingShareScopeMap, key) &&
(!hasOwnScope(hostShareScopeMap, key) ||
isEmptyShareScope(currentShareScope))
) {
return existingShareScopeMap[key];
}

if (hasOwnScope(hostShareScopeMap, key)) {
return options.fallbackWhenEmpty && isEmptyShareScope(currentShareScope)
? fallbackShareScope
: currentShareScope;
}

return fallbackShareScope;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a short JSDoc summarizing the precedence: non-empty host scope wins → fall back to existing populated scope → fallback. The logic is subtle enough that the next maintainer will appreciate it.

Comment on lines +76 to +82
const sc = resolveShareScope(hostKey, {});
if (
!hasOwnScope(hostShareScopeMap, hostKey) ||
(hasOwnScope(existingShareScopeMap, hostKey) &&
isEmptyShareScope(hostShareScopeMap[hostKey]))
) {
hostShareScopeMap[hostKey] = sc;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor/readability: the string-key branch writes the resolved scope back into hostShareScopeMap[hostKey], but the array-shareScopeKey branch doesn't. Not a correctness issue (the array path works without it), but the asymmetry is confusing — a one-line comment on why the write-back is only needed in one branch would help.

@BaurinVladislav

Copy link
Copy Markdown

Up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants