Skip to content

fix: emit native injection code as CommonJS - #419

Closed
shin4141 wants to merge 1 commit into
nativewind:mainfrom
shin4141:codex/fix-native-injection-cjs
Closed

shin4141 wants to merge 1 commit into
nativewind:mainfrom
shin4141:codex/fix-native-injection-cjs

Conversation

@shin4141

Copy link
Copy Markdown

Summary

  • emit native injection code with CommonJS require() calls
  • preserve CSS side-effect loading and StyleCollection.inject behavior
  • add a regression that parses and executes the generated source in script mode

The synthetic module previously retained raw import / export syntax, which reaches Hermes unchanged when Expo's optimize-graph tree shaking is enabled.

Validation

  • focused regression: 1 test passed
  • full test suite: 56 suites passed (4 skipped), 1,052 tests passed (21 skipped)
  • yarn lint
  • yarn typecheck
  • yarn build
  • git diff --check

The exact Expo optimize-graph + Hermes export was not rerun locally; the regression directly covers the script-mode parse failure and the required module/injection behavior.

Closes #414.

AI assistance was used to inspect the issue, implement the focused change, and run validation. I reviewed the complete diff.

sammoore added a commit to sammoore/react-native-css that referenced this pull request Dec 4, 2026
…is on

With EXPO_UNSTABLE_METRO_OPTIMIZE_GRAPH and EXPO_UNSTABLE_TREE_SHAKING,
Expo's worker sees the injection module as `<file>.css.js`, so it leaves
it as unwrapped ESM for the serializer to finish. The serializer looks
at the graph path instead, which ends in `.css`, and skips it. The
module reaches the bundle as top-level `import`/`export`, and hermesc
refuses to compile it (nativewind#414).

Transform this one module with `optimize` off, so Expo converts and
wraps it in `__d` the way it handles its own CSS modules. Nothing in it
can be tree-shaken. Builds without the flags are byte-identical.

Emitting CommonJS instead (nativewind#419) only moves the failure to launch: the
unwrapped module calls a global `require` that a production bundle
lacks.

Closes nativewind#414
danstepanov pushed a commit that referenced this pull request Dec 4, 2026
…is on (#607)

With EXPO_UNSTABLE_METRO_OPTIMIZE_GRAPH and EXPO_UNSTABLE_TREE_SHAKING,
Expo's worker sees the injection module as `<file>.css.js`, so it leaves
it as unwrapped ESM for the serializer to finish. The serializer looks
at the graph path instead, which ends in `.css`, and skips it. The
module reaches the bundle as top-level `import`/`export`, and hermesc
refuses to compile it (#414).

Transform this one module with `optimize` off, so Expo converts and
wraps it in `__d` the way it handles its own CSS modules. Nothing in it
can be tree-shaken. Builds without the flags are byte-identical.

Emitting CommonJS instead (#419) only moves the failure to launch: the
unwrapped module calls a global `require` that a production bundle
lacks.

Closes #414
@danstepanov

danstepanov commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Thanks for digging into this one. #607 replaces it and is now merged as 5dac0e5.

Emitting CommonJS gets past hermesc, but the module still ends up unwrapped at the top level of the bundle, where Metro's production runtime has no global require in some configurations, so the app crashes on launch with ReferenceError: Property 'require' doesn't exist. #607 transforms that generated module with optimize off instead, so Expo wraps it during the transform. The PR description there has the full details.

Closing this in favor of #607.

@danstepanov danstepanov closed this Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tree shaking: getNativeInjectionCode emits raw ESM, hermesc fails with 'requires module mode'

2 participants