Skip to content

fix(vite): preserve singleton:true packages in excludeSharedSubDependencies#681

Merged
gioboa merged 2 commits intomodule-federation:mainfrom
ThibautMarechal:fix/exclude-shared-sub-deps-singleton
May 3, 2026
Merged

fix(vite): preserve singleton:true packages in excludeSharedSubDependencies#681
gioboa merged 2 commits intomodule-federation:mainfrom
ThibautMarechal:fix/exclude-shared-sub-deps-singleton

Conversation

@ThibautMarechal
Copy link
Copy Markdown
Contributor

Summary

Closes #680

excludeSharedSubDependencies is a dev-mode heuristic that auto-removes shared packages that are sub-dependencies of other shared packages, to prevent initialization-order issues (e.g. lit depending on lit-html).

However, the function was also silently removing packages explicitly declared with singleton: true — such as react and react-dom — when a company SDK or wrapper library lists them in its dependencies. This created multiple React instances across host and remotes, breaking all hooks with Cannot read properties of null.

The function already skips packages with import: false. This PR adds the same guard for singleton: true.

Changes

  • src/plugins/pluginProxySharedModule_preBuild.ts — one-line fix: add singleton === true guard in excludeSharedSubDependencies
  • src/plugins/__tests__/pluginProxySharedModule_preBuild.test.ts:
    • Update existing 'excludes shared sub-dependencies in dev mode and warns' test: change lit sub-packages from singleton: truesingleton: false (they were using singleton: true but asserting removal, which would now fail correctly)
    • Add 'preserves singleton: true shared sub-dependencies in dev mode without warning' — asserts react/react-dom are NOT removed when a wrapper lib lists them as deps
    • Add 'still removes non-singleton sub-dependencies when sibling singletons are preserved' — asserts the original lit-style behavior is preserved for non-singleton sub-deps
  • .changeset/lucky-lions-rest.md — patch changeset

Test plan

  • All 302 existing tests pass (pnpm test)
  • New tests cover: singleton sub-dep preserved, non-singleton sub-dep still removed
  • TypeScript type-check passes (pnpm typecheck)
  • Formatting check passes (pnpm fmt.check)

…encies

The dev-mode heuristic that removes shared sub-dependencies to prevent
initialization-order issues (e.g. lit/lit-html) was also silently removing
packages explicitly declared with singleton:true such as react and react-dom
when a company SDK or wrapper library lists them in its dependencies. This
caused multiple React instances across host and remotes, breaking all hooks.

Fix: add a singleton:true guard alongside the existing import:false guard.

Closes module-federation#680
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 2, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@module-federation/vite@681

commit: ca93569

Copy link
Copy Markdown
Collaborator

@gioboa gioboa left a comment

Choose a reason for hiding this comment

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

Thanks for your help @ThibautMarechal
I will look at this soonish

@ThibautMarechal
Copy link
Copy Markdown
Contributor Author

It fix my issue, but I don't know the impacts of other use cases. If you have another workarround, I can take it :)

Copy link
Copy Markdown
Collaborator

@gioboa gioboa left a comment

Choose a reason for hiding this comment

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

Tests With Different Frameworks

Image

Copy link
Copy Markdown
Collaborator

@gioboa gioboa left a comment

Choose a reason for hiding this comment

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

Thanks for this fix @ThibautMarechal 👏

@gioboa gioboa merged commit 9f78bc3 into module-federation:main May 3, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: excludeSharedSubDependencies incorrectly removes singleton: true packages in dev mode

2 participants