Repository navigation
Conversation
…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
force-pushed
the
fix/tree-shaking-injection
branch
from
December 4, 2026 15:53
d9db185 to
85f8e3d
Compare
This branch has not been deployed
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.
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 inhermescwith'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-configdisagree about this module:<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.<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
optimizeoff, 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 tomain.Why not CommonJS (#419)
Emitting
require()instead ofimportgets pasthermesc, but the module is still unwrapped at the top level of the bundle, where Metro's production runtime has no globalrequire. The app crashes on launch withReferenceError: 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.tsruns our transformer through Expo's real worker withoptimize: "true"and checks the output is a wrapped__d(...)module with noimport/export. It fails onmainwith #414's unwrapped output, and it would fail on #419 too.Verified
Example app,
expo export --platform androidwith both flags:main'import' statement requires module modeReferenceError: Property 'require' doesn't existStyleCollection.injectOn devices, with both flags set for every build (Expo 57, RN 0.86.3, Hermes):
mainhermesc:'import' statement requires module modehermesc: same errorThe
mainrow shows the flags reached Xcode's and Gradle's bundling steps. In debug,optimizeis never set, so this change does nothing there.