Help React Native import @apollo/client/main.cjs
and other CommonJS bundles
#9716
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.
We use the
.cjs
file extension for our CommonJS bundle entry points (such as@apollo/client/core/core.cjs
) to indicate to Node.js that"type": "module"
in@apollo/client/package.json
does not apply to those files.Without additional configuration of its Metro bundler, React Native refuses to resolve
.cjs
files by default, but it will attempt to append various file extensions, including the (relatively) React Native-specific ending.native.js
. By providing adist/**/*.cjs.native.js
file for every existingdist/**/*.cjs
file we ship, React Native should always have a safe fallback to find the right code.If this works, it will completely supersede/obviate the manual advice involving
metro.config.js
we added toCHANGELOG.md
in #9084.There is some risk of the dual package hazard (importing the same module multiple times unexpectedly), but as long as Metro consistently resolves the
.native.js
version of the file, all should be well. Apollo Client should completely ignore the.native.js
version of the file, so React Native is the only reason those alternate modules should ever be used.This PR incorporates the following PRs for dependency packages that also used
.cjs
bundle files:index.cjs.native.js
to help React Native resolveindex.cjs
zen-observable-ts#273lib/invariant.cjs.native.js
andprocess/main.cjs.native.js
for React Native invariant-packages#293Issues potentially fixed: