fix(constants): inline LIB_VERSION via codegen to fix compiled output#1139
Open
marnelram wants to merge 1 commit intosoftware-mansion:mainfrom
Open
fix(constants): inline LIB_VERSION via codegen to fix compiled output#1139marnelram wants to merge 1 commit intosoftware-mansion:mainfrom
LIB_VERSION via codegen to fix compiled output#1139marnelram wants to merge 1 commit intosoftware-mansion:mainfrom
Conversation
Source `src/constants/resourceFetcher.ts` reads the version with
`require('../../package.json')`. That path is correct from
`src/constants/` (two-up = package root), but `react-native-builder-bob`'s
`module` target emits the file at `lib/module/constants/` — one level
deeper — and the babel transform leaves the relative `require` literal
unchanged. From the compiled location, `../../package.json` resolves to
`lib/package.json`, which does not exist. Any bundler that resolves
through `"main"` / `"module"` (web bundlers, Node) crashes with
`Unable to resolve module ../../package.json from
node_modules/react-native-executorch/lib/module/constants/resourceFetcher.js`.
Only Metro for iOS/Android survives, because it honors the
`"react-native": "src/index"` field and reads the source file directly.
Fix by generating the version into a committed source file before
build, so the compiled output never needs a runtime JSON require:
- New `scripts/sync-version.cjs` reads `package.json#version` and writes
`src/constants/libVersion.ts`.
- `src/constants/resourceFetcher.ts` re-exports `LIB_VERSION` from the
generated file.
- `scripts.prepare` runs `sync-version.cjs` before `bob build`.
- `scripts.version` runs it on `npm version <bump>` so the committed
`libVersion.ts` stays in sync with `package.json#version`.
Verified by running `yarn workspace react-native-executorch run prepare`
and confirming `lib/module/constants/resourceFetcher.js` now has
`export { LIB_VERSION } from './libVersion'` and the new
`lib/module/constants/libVersion.js` ships the inlined constant.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Collaborator
|
cc @mkopcins, i believe we can also use that new constant for the model urls to avoid duplicating |
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 #1138.
Problem
src/constants/resourceFetcher.tsreads the version withrequire('../../package.json').version. Fromsrc/constants/, that path resolves to the package root — correct. Butreact-native-builder-bob'smoduletarget emits the file atlib/module/constants/, one level deeper, and the babel transform leaves the relativerequireliteral unchanged. From the compiled location,../../package.jsonislib/package.json— a file that doesn't exist. Any bundler that resolves through"main"/"module"(web bundlers, Node) blows up.iOS/Android Metro is unaffected because the package's
"react-native": "src/index"field sends Metro to the source directly.Fix
Generate the version into a committed source file instead of requiring
package.jsonat runtime.scripts/sync-version.cjs: readspackage.json#version, writessrc/constants/libVersion.ts.src/constants/libVersion.ts: holdsexport const LIB_VERSION: string = '<version>'. Header marks it auto-generated.src/constants/resourceFetcher.ts: re-exportsLIB_VERSIONfrom./libVersion.package.json:scripts.prepare:node scripts/sync-version.cjs && bob build— regenerate before every build.scripts.version:node scripts/sync-version.cjs && git add src/constants/libVersion.ts— keeps the committed file in sync withnpm version <bump>.Why this shape
require, so no relative-path fragility across output layouts.package.json#version; the committedlibVersion.tsis regenerated before publish and before any version bump.src/directly (via thereact-nativeentry) get the committed file — still correct, since it's refreshed at version-bump time.Verification
After build,
lib/module/constants/resourceFetcher.js:And the new
lib/module/constants/libVersion.js:yarn workspace react-native-executorch run typecheck,prettier --check, andeslinton the touched files all pass.