Skip to content

feat: experimental tree shaking support#1664

Open
bacarybruno wants to merge 3 commits into
facebook:mainfrom
bacarybruno:tree-shaking
Open

feat: experimental tree shaking support#1664
bacarybruno wants to merge 3 commits into
facebook:mainfrom
bacarybruno:tree-shaking

Conversation

@bacarybruno
Copy link
Copy Markdown

@bacarybruno bacarybruno commented Mar 5, 2026

Summary

Note

Sharing this draft early to get initial feedback and iterate.

Early benchmarking is promising:

  • Baseline: 12,983,961 bytes 115s
  • Tree-shake: 11,939,372 bytes, 107s
  • Delta: -1,044,589 bytes (-8.05%), time -8s
    Validation so far is based on repeated bundle runs (tree-shake off vs on).

Minimal metro config:

const useTreeShake = process.env.METRO_TREE_SHAKE === '1'

const config = {
  ...(useTreeShake
    ? {transformerPath: require.resolve('metro-transform-worker')}
    : {}),
  transformer: {
    unstable_treeShake: useTreeShake,
  },
}

Todo:

  • Add unstable_treeShake flag
  • sideEffects support
  • Bench before / after
  • Manual tests
  • Run broader before/after benchmarks (multiple apps/configs)
  • Ensure no regression when unstable_treeShake is disabled / not set
  • Documentation

Test plan

Related issues:

The implementation plan can change depending on react-native-community/discussions-and-proposals#949

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 5, 2026
@bacarybruno bacarybruno marked this pull request as ready for review March 8, 2026 10:15
@bacarybruno
Copy link
Copy Markdown
Author

Open question: where should tree shaking live?

Expo has its own experimental tree shaking as a serializer plugin. It also has constraints that make adopting Metro-core tree shaking non-trivial: custom transformerPath, bypasses Server.build() entirely via _bundleDirectAsync, and relies on its own serializer pipeline.

Should tree shaking live in Metro core as the source of truth with Expo eventually delegating to it, or should Metro close the door and fully delegate to Expo's implem?

@kitten
Copy link
Copy Markdown
Contributor

kitten commented Apr 8, 2026

@bacarybruno: re

Should tree shaking live in Metro core as the source of truth with Expo eventually delegating to it, or should Metro close the door and fully delegate to Expo's implem?

We don't currently intend to designate the current, experimental tree-shaking implementation in Expo's core to be stable. It's a proof of concept, and transferring the AST from the transformer to the serialiser, while being the path of least resistance, isn't what we intend to do.

If Metro ships an implementation upstream, we're happy to adopt it and/or tweak as needed to fit it to the existing expectations we need it to comply to.

The main assumption we had was that it'd be nice if a single transformer worker pass could instead operate on source ranges. However, this is potentially a larger change in how our serializers work, but it's one idea we've seen that looks promising. The long and short of it is that we assume we want to keep the serializer lean even with tree-shaking, prevent uncacheable work, and stop transferring ASTs for tree-shaking to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants