fix: Add getHostComponent.web.ts so web bundles don't pull in React Native internals - #1477
Open
giaBaoJS wants to merge 1 commit into
Open
fix: Add getHostComponent.web.ts so web bundles don't pull in React Native internals#1477giaBaoJS wants to merge 1 commit into
getHostComponent.web.ts so web bundles don't pull in React Native internals#1477giaBaoJS wants to merge 1 commit into
Conversation
…onent.web.ts`) `getHostComponent.ts` deep-imports `react-native/Libraries/NativeComponent/NativeComponentRegistry`. `src/views/` had no web variant, so web bundles resolved the native file and dragged native-only React Native modules in. With Metro's experimental tree-shaking that fails the build with "Circular dependency detected while tree-shaking: .../PlatformColorValueTypes.js". Add a `.web.ts` variant that throws for `getHostComponent` (Nitro Views are native-only) and keeps `callback` as-is, mirroring the existing `NativeNitroModules.web.ts` pattern. iOS/Android resolution is unaffected. Fixes mrousavy#1216
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
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.
Fixes #1216
The bug
packages/react-native-nitro-modules/src/views/getHostComponent.ts:4deep-imports a React Native internal:src/views/has no web variant, so on web Metro resolves the native file and pulls native-only React Native modules into the bundle. With Metro's experimental tree-shaking that fails the build outright:The rest of the package already handles this correctly —
src/turbomodule/NativeNitroModules.web.tsexists for exactly the same reason.views/was just missing its counterpart.You replied on the issue with "Ah yes, will fix that soon" (comment), so this follows that.
The fix
One new file:
src/views/getHostComponent.web.ts.getHostComponentthrows — Nitro Views are backed by native iOS/Android views, so there is nothing to return on web.callbackis kept identical, sincesrc/index.tsdoesexport * from './views/getHostComponent'andcallbackis part of that public surface. It is 6 lines of platform-independent code, mirroring howNativeNitroModules.web.tsrestatesisRuntimeAlive.import type, so they are fully erased at build time and the web output has no reference back to the native file (verified in the emitted JS below).React Native's platform-extension resolution does the rest. No other file is touched.
Verification
1. Reproduced the reported failure, on a fresh Expo SDK 57 app (RN 0.86.2,
react-native-nitro-modules@0.36.5from npm), whoseApp.tsximportsgetHostComponentfromreact-native-nitro-modules:2. Same app, same command, with this branch (
npm packed from this checkout and installed into the repro app):Grepping that web bundle:
PlatformColorValueTypesNativeComponentRegistryNitro Views are not supported on ${"web"}The last line is the decisive one — the web variant is what actually got bundled, with
Platform.OSinlined to"web".3. Native resolution is unchanged. Same app,
npx expo export --platform ios --no-bytecode, with this branch installed:NativeComponentRegistryNativeComponentRegistry is not available(nativegetHostComponent.ts)Nitro Views are not supported(web variant)4. Build output.
bun run buildinpackages/react-native-nitro-modulesemits the new file into every target:This matters because
package.jsononly sets"react-native": "src/index"— Metro on web usesmodule/main, i.e.lib/. Theimport types are erased, solib/module/views/getHostComponent.web.jsimports onlyreact-native.5. Checks.
bun typecheck(all workspaces) andbun lint-ciinpackages/react-native-nitro-modulesare clean.On the test requirement
CONTRIBUTING asks every bug fix to ship with a test, and I could not find an honest place to put one:
packages/react-native-nitro-moduleshas a jest config, but its only test isit.todo('write a test')and no workflow runs it, so a jest assertion here would not be a CI safety net — and asserting "the.web.tsfile exports the same keys as the native one" would test the file rather than the bundling behaviour that actually broke.Rather than add a test CI never runs, I verified the user-visible path directly (steps 1–3 above). Happy to add either of these if you'd prefer:
src/__tests__pinning that the web and native variants export the same runtime keys, plus wiringbun testintolint-typescript.yml, orexpo export --platform webagainst a small fixture app.Just say which and I'll push it.
What I did not verify
getHostComponentreturn a no-op component so a web app can render past it, that's a one-line change — I went with throwing to matchNativeNitroModules.web.ts.