Skip to content

fix(metro): wrap the native stylesheet module when Expo tree shaking is on - #607

Open
sammoore wants to merge 1 commit into
nativewind:mainfrom
sammoore:fix/tree-shaking-injection
Open

sammoore wants to merge 1 commit into
nativewind:mainfrom
sammoore:fix/tree-shaking-injection

Conversation

@sammoore

@sammoore sammoore commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #414. Replaces #419.

With Expo's experimental tree shaking on (EXPO_UNSTABLE_METRO_OPTIMIZE_GRAPH=1 EXPO_UNSTABLE_TREE_SHAKING=1), a release build fails in hermesc with 'import' statement requires module mode. The module that react-native-css generates for each stylesheet reaches the bundle as raw ESM.

Cause

Two checks in @expo/metro-config disagree about this module:

  • The transformer hands Expo's worker the generated code as <file>.css.js. The worker decides whether to tree-shake from that filename, so it leaves the module as unwrapped ESM for the serializer to finish.
  • The serializer finishes modules by their graph path, which is still <file>.css, and skips every path ending in .css (reconcileTransformSerializerPlugin). The module is never converted or wrapped in __d(...).

Fix

Transform this one module with optimize off, so Expo converts and wraps it during the transform, the way it handles its own CSS modules. The generated module has nothing to tree-shake. Builds without the flags are byte-identical to main.

Why not CommonJS (#419)

Emitting require() instead of import gets past hermesc, but the module is still unwrapped at the top level of the bundle, where Metro's production runtime has no global require. The app crashes on launch with ReferenceError: Property 'require' doesn't exist, as reported on a device in #414. I reproduced the crash by running #419's bundle in Hermes.

Test

src/__tests__/metro/tree-shaking.test.ts runs our transformer through Expo's real worker with optimize: "true" and checks the output is a wrapped __d(...) module with no import/export. It fails on main with #414's unwrapped output, and it would fail on #419 too.

Verified

Example app, expo export --platform android with both flags:

hermesc Runtime (Hermes)
main fails: 'import' statement requires module mode —
#419 passes ReferenceError: Property 'require' doesn't exist
this PR passes the module is defined as a dependency of the app module and calls StyleCollection.inject

On devices, with both flags set for every build (Expo 57, RN 0.86.3, Hermes):

iOS 26.3 simulator (iPhone 17 Pro) Android emulator (Pixel 9)
Release, main build fails in hermesc: 'import' statement requires module mode build fails in hermesc: same error
Release, this PR builds, launches, styles render builds, launches, styles render
Debug (Metro), this PR launches, styles render launches, styles render

The main row shows the flags reached Xcode's and Gradle's bundling steps. In debug, optimize is never set, so this change does nothing there.

…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
@sammoore
sammoore force-pushed the fix/tree-shaking-injection branch from d9db185 to 85f8e3d Compare December 4, 2026 15:53

This branch has not been deployed

No deployments
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'

1 participant