WhoBuilt

Code Splitting in React Native

Oct 13, 2021· 22 min· 4.6K views·Watch on YouTube

Speaker

Paweł Trysła, software developer at Callstack, presents at React Native EU 2021 on code splitting in React Native.

What code splitting changes

Code splitting divides an application into multiple bundles that can load on demand or in parallel. A main bundle contains the entry point and the minimum code needed to show the app; additional chunks hold features or logic for later use. Loading less code at startup can improve time to render and time to interactive.

Where it can help

Super apps such as WeChat and Alipay can defer downloading mini apps until users need them; Trysła cites one million mini apps in WeChat. Modular apps can also load features according to user role, subscription, or preferences. Examples include separate student and teacher features in e-learning apps and vehicle-specific features in automotive companions.

Platform options and Re.Pack

Trysła describes Android Play Feature Delivery as a possible fit for brownfield apps, though sharing one JavaScript runtime is difficult. On iOS, On-Demand Resources can deliver assets, but not native code, and he says dynamic native code could run into App Store review issues. WebViews and Metro-based approaches have tradeoffs, so Callstack built Re.Pack to let webpack produce bundles that run in React Native.

Three webpack approaches

Trysła outlines async chunks, dynamic scripts, and Module Federation. Async chunks use dynamic imports from one codebase and suit modular apps; dynamic scripts can load arbitrary code but require developers to manage loading and dependency sharing. Module Federation supports separately built containers and may suit super apps, though it is harder to manage and was not yet supported in Re.Pack at the time of the talk.

Re.Pack status and next steps

At the time, Re.Pack could download and execute remote or local chunks, use basic caching and invalidation, and resolve chunk URLs with custom logic. Planned work included chunk signing and verification, more caching and fetching options, local asset support, and more documentation. Trysła notes that the APIs were still being developed and could change.

Takeaways

  • Start with the smallest entry bundle needed to make the app usable, then load features when users need them.
  • Choose code splitting around actual product boundaries, such as roles, subscriptions, or mini apps.
  • For React Native, weigh native delivery, WebViews, Metro, and webpack against runtime sharing, user experience, and team expertise.
  • Async chunks are a simpler fit for one codebase; dynamic scripts and Module Federation bring more flexibility and more integration work.
  • Treat Re.Pack’s 2021 capabilities and roadmap as time-specific; verify current support before choosing an implementation.

Mentioned in this talk

Search inside this talk

What was claimed 12

  • feature
    Callstack Team: React and React Native development experts
    “Callstack, React and React Native development experts.”
    YouTube
    Unverified1 source· Talk
  • role at
    Paweł Trysła is software developer at Callstack Team
    “I'm a software developer at Callstack.”
    YouTube
    Unverified1 source· Talk
  • feature
    code splitting: Splits product code into multiple bundles that can be loaded on demand or in parallel
    “a technique to split the code of your product into multiple bundles”
    YouTube
    Unverified1 source· Talk
  • feature
    code splitting: Can improve time to render and time to interactive by deferring code loading
    “This can improve time to render and time to interactive”
    YouTube
    Unverified1 source· Talk
  • feature
    Play Feature Delivery: Splits native Android code into multiple bundles and can embed React Native in a bundle
    “It allows us to split native code into multiple bundles.”
    YouTube
    Unverified1 source· Talk
  • uses
    “we could bridge this gap and make webpack compatible with React Native. And that’s exactly what we did.”
    YouTube
    Verified2 sources· Talk
  • feature
    Re.Pack: Makes webpack compatible with React Native
    “Re.Pack is a toolkit that makes webpack compatible with React Native.”
    YouTube
    Unverified1 source· Talk
  • feature
    Re.Pack: Provides a development server, React Refresh support, and Flipper compatibility
    “It gives you a fully featured development server, support for React Refresh, and compatibility with Flipper.”
    YouTube
    Unverified1 source· Talk
Show all 12 facts

Related talks