Skip to content

fix: rewrite compiler-runtime chunk to use importShared in remote builds#744

Open
pedrotainha wants to merge 2 commits intooriginjs:mainfrom
pedrotainha:fix/compiler-runtime-importShared
Open

fix: rewrite compiler-runtime chunk to use importShared in remote builds#744
pedrotainha wants to merge 2 commits intooriginjs:mainfrom
pedrotainha:fix/compiler-runtime-importShared

Conversation

@pedrotainha
Copy link
Copy Markdown

Summary

  • Detect react/compiler-runtime chunks in generateBundle (remote builds only)
  • Rewrite them to obtain React via importShared("react") instead of a direct bundled import
  • Includes helpers: findImportSharedExportName (handles minified exports) and computeRelativePath

Problem

When a remote app uses React Compiler (babel-plugin-react-compiler), Rollup bundles react/compiler-runtime as a separate chunk that imports React directly:

import{r as R}from"./index-XYZ.js";  // direct React import, NOT importShared

The AST transform in remote-production.ts only matches exact shared module names (react), not sub-exports (react/compiler-runtime). The result: the compiler-runtime chunk gets its own React instance, separate from the host's shared one.

At render time, the shared React has its hooks dispatcher initialized (H), but the isolated copy has H = null → crash:

TypeError: Cannot read properties of null (reading 'useMemoCache')

Fix

In generateBundle, after all chunks are built:

  1. Find the __federation_fn_import chunk and discover the exported name for importShared (handles both minified and unminified forms)
  2. Find compiler-runtime chunks that access __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE
  3. Rewrite them to use importShared("react") via top-level await

Only applies to remote builds (builderInfo.isRemote) — the host owns the React instance directly and doesn't need the patch.

Test plan

  • Build a remote with babel-plugin-react-compiler enabled
  • Verify compiler-runtime-*.js chunk uses importShared instead of direct import
  • Load remote in host — verify useMemoCache works without crash
  • Verify host build is NOT affected (no patch applied)

Fixes #742

react/compiler-runtime is bundled as a separate chunk that imports React
directly instead of through importShared. In federated mode the host
provides React via the share scope — the local chunk is a different
instance where the hooks dispatcher is never initialised, causing
useMemoCache to crash.

The fix detects compiler-runtime chunks in generateBundle (remote builds
only) and rewrites them to obtain React via importShared("react").

Fixes originjs#742
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.

react/compiler-runtime chunk imports React directly instead of through importShared — broken hooks in remote builds

1 participant