add react-native export condition resolving to browser build - #412
Closed
davor-bauk-sh wants to merge 1 commit into
Closed
davor-bauk-sh wants to merge 1 commit into
davor-bauk-sh wants to merge 1 commit into
Conversation
Metro (React Native's bundler) resolves package exports with the
conditions `import`/`require` and `react-native`, never `browser`
or `node`, so it falls through to the `default` entry. That build
does `import('node:diagnostics_channel')`, which cannot be provided
in a React Native runtime. Current Metro treats the `.catch()`-
guarded import as optional at build time, but on native the runtime
`require` of an unresolved optional module reports a fatal error via
`ErrorUtils` instead of throwing, so the `.catch()` never runs and
the app crashes on startup.
Expose the existing browser build under the `react-native` condition
as well. This is the conventional way for packages to provide a
Metro-specific entry, is ignored by every other resolver, and adds
no new build output.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.
Problem
Follow-up to #397 / #399 and react/metro#1681.
Metro resolves package
exportswith the conditionsimport/requireplusreact-native. It never setsbrowserornode, so forlru-cacheit falls through todefault, i.e.dist/esm/index.min.js, which doesimport('node:diagnostics_channel').Current Metro does recognise the
.catch()-guarded dynamic import as optional at build time (the fix from metro#1681), so bundling succeeds. On native, though, the runtimerequireof an unresolved optional module reports a fatal error throughErrorUtils.reportFatalErrorinstead of throwing, so the promise.catch()never sees it and the app crashes on startup. This is not thenodecondition being loaded by mistake; it is thedefaultentry.Change
Add a
react-nativecondition to the root export, in bothtshy.exportsand the generatedexports, pointing at the existing browser build (dist/{esm,commonjs}/browser/). No new build output, no source change.react-nativeis the conventional condition packages use to give Metro a dedicated entry, and every other resolver ignores it.Verified with a bare-specifier probe against this branch after
npm run prepare:tshypreserves the condition on rebuild (git statusclean apart from this diff). Browser builds contain nonode:references.Not touched: the
./rawexport, which is generated bytshyfrom source. React Native users importinglru-cache/rawwould still hit the default build; happy to hand-write that export too if you'd prefer.🤖 Generated with Claude Code