Let Metro handle import/export instead of Babel #1772
Merged
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.
We currently rely on ESM -> CJS being compiled by Babel. This comes with some downsides.
I've tried using Metro's experimental built-in
import
/export
setup and it seems to work.Here's what this gives us.
Namespace imports are now lazy (as they should be)
In #1756, we enabled inline requires. However, initialization logs show all of these modules being prematurely initialized even though they're not being used. This is because Babel compiles
import *
to this:So the
_interopRequireWildcard
Babel helper was causing the inline requires to evaluate early.After the change, we can see these imports being inlined at their usage site, as you would expect:
React/RN built-in imports are now eager (as they should be)
In #1756, we regressed by having common APIs like
View
/Text
and React's JSX runtime itself (jsx
calls) go through new indirections at runtime, for example:After this change, both the
jsx
helpers and RN's built-ins are resolved eagerly:As you can see, our own modules like
@atproto/api
are still initialized lazily.Test plan
Walked around the app. Both iOS and Android on the simulator. Seemed fine.
This doesn't affect web.
Perf impact
This gets us down from 1449 to 1397 modules being eagerly initialized during startup. As a result, I'm observing a ~200ms reduction in PROD Android startup time. Nothing major but should scale up better as we add more code.
Follow-ups
I've noticed duplicated
_objectSpread
helpers everywhere in our bundle. I think we might need to externalize the Babel runtime. Also, isn't Hermes modern enough to support spread by default? I'll look at that later to keep this PR scoped.