fix: rewrite compiler-runtime chunk to use importShared in remote builds#744
Open
pedrotainha wants to merge 2 commits intooriginjs:mainfrom
Open
fix: rewrite compiler-runtime chunk to use importShared in remote builds#744pedrotainha wants to merge 2 commits intooriginjs:mainfrom
pedrotainha wants to merge 2 commits intooriginjs:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
react/compiler-runtimechunks ingenerateBundle(remote builds only)importShared("react")instead of a direct bundled importfindImportSharedExportName(handles minified exports) andcomputeRelativePathProblem
When a remote app uses React Compiler (
babel-plugin-react-compiler), Rollup bundlesreact/compiler-runtimeas a separate chunk that imports React directly:The AST transform in
remote-production.tsonly 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 hasH = null→ crash:Fix
In
generateBundle, after all chunks are built:__federation_fn_importchunk and discover the exported name forimportShared(handles both minified and unminified forms)compiler-runtimechunks that access__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADEimportShared("react")via top-level awaitOnly applies to remote builds (
builderInfo.isRemote) — the host owns the React instance directly and doesn't need the patch.Test plan
babel-plugin-react-compilerenabledcompiler-runtime-*.jschunk usesimportSharedinstead of direct importuseMemoCacheworks without crashFixes #742