fix: unwrap namespace.default in prebuild named-exports branch#870
Merged
gioboa merged 1 commit intoJul 2, 2026
Merged
Conversation
The namedExports branch of writePreBuildLibPath emitted `export default __mfPrebuildExports` (the raw namespace object) instead of unwrapping `namespace.default`. All three sibling branches in the same function (react/jsx-runtime, react/jsx-dev-runtime, and the no-namedExports fallback) already emit `namespace.default ?? namespace`, so this was inconsistent codegen. For any shared package that exports named symbols AND has a CJS default export via ESM interop (React, react-dom, styled-components, react-router when consumed through the shim), `import Pkg from 'pkg'` would return the namespace object instead of the real default. Symptoms include `Cannot read properties of undefined (reading 'createElement')` from prebundled shared React. Fix: emit `__mfPrebuildNamespace.default ?? __mfPrebuildNamespace`, matching the other three branches.
commit: |
gioboa
approved these changes
Jul 2, 2026
gioboa
left a comment
Collaborator
There was a problem hiding this comment.
Thanks @muiradams for your help 🎉
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
Fixes a CJS-interop default-export bug in the
writePreBuildLibPathcodegen for shared modules.The branch selected when a shared package has any named exports emits:
But the three sibling branches in the same function —
react/jsx-runtime,react/jsx-dev-runtime, and the fallback for packages with no named exports — all usenamespace.default ?? namespace. This was inconsistent codegen and caused a real interop regression for consumers.Impact
For any shared package that exports named symbols and has a CJS default via ESM interop (
react,react-dom,styled-components,react-router, etc.),import Pkg from 'pkg'through the prebuild shim returned the module namespace object instead of the real default export.Concretely, when a host prebundles a shared
reactthis way, downstream consumers seePkg.createElementasundefinedbecause the default they got was{ default: React, useState, useEffect, ... }rather thanReactitself. This tends to surface asCannot read properties of undefined (reading 'createElement')from prebundled shared React.Fix
Emit
__mfPrebuildNamespace.default ?? __mfPrebuildNamespacein the named-exports branch, matching the other three branches.Tests
Added a regression test in
src/virtualModules/__tests__/virtualShared_preBuild.test.tsthat verifies the emitted code for a package with named exports (a) containsexport default __mfPrebuildNamespace.default ?? __mfPrebuildNamespace;and (b) does not emit the oldexport default __mfPrebuildExports;line.pnpm test— 598/598 passing (was 597/597; +1 new test).pnpm fmt.check/pnpm typecheckclean.mainwithout the source fix and passes with it.