Module Federation in React-Native

Aexomir
4 min readNov 2, 2024

--

As a developer, I know how important it is to keep apps fast, flexible, and easy to manage. That’s why I want to introduce you to Module federation — a tool developed by CallStack! With RePack, you can break down your app using micro-frontend capabilities, making updates smoother, speeding up load times, and making scaling a breeze!

In this article, I’ll walk you through why RePack is a smart choice, what it brings to the table, how to get it up and running in an existing app, and a few drawbacks to keep in mind.

from: https://tenor.com/bK1oF.gif

Table Of Content

  • What is Repack? Why Repack?
  • The Repack Advantage — Code splitting, Module federation.
  • Considerations Before Repacking

What is RePack? Why RePack?

RePack is a tool make Module Federation in React Native applications easier. It works by breaking down a big application into smaller, independently loadable pieces, known as micro-apps. By organizing features into separate “packs,” RePack can load only the parts users need in real-time. This modular approach also supports feature updates without impacting the whole application — a major benefit when building super applications!

image from: https://cdn.prod.website-files.com/64d4ce10600bd67e51c42838/6538e3b17731a6370e0ff179_60ffc9df910b1345faca33e7_modular%2520aps.svg

The Repack Advantages

1. Code Splitting: Your App on a Diet

Think of your React Native app like a suitcase. Traditionally, you’d pack everything in at once leading to a bulky experience. Repack allows you to split your app! Imagine breaking your code into smaller chunks, like packing specific outfits for different activities. This way, users only download the code they need for the features they’re currently using.

from: https://tenor.com/bzqCF.gif

2. Module Federation: Real Micro-Frontend

In addition to code splitting, RePack supports module federation, an approach that allows different chunks to be developed and deployed independently. This setup is ideal for collaborative environments. Consider a large e-commerce app. You could break it down into modules like:

  • Product Catalog: Handles product listings and search functionality.
  • Checkout: Manages the checkout process and payment integration.
  • User Profile: Handles user accounts, orders, and preferences.

Each module can be developed and deployed independently, and they can share common components and services. For example, the product catalog and checkout modules could share a common Cart component.

image from: https://cdn.prod.website-files.com/64d4ce10600bd67e51c42838/664e10bbc64ee21b836ad1b9_Implementing%20Code%20Splitting%20in%20React%20Native%20with%20Re.Pack-3.png

Integrating Code-Splitting & Module Federation into an existing react-native application

TBH, I was going to add a step by step guide about this; but then I thought there’s no better guide than the maintainers guide itself! :) So, check out this link but please don’t hesitate to reach out to me if you face any challenges while integration!

from: https://tenor.com/byZHz.gif

Code-Splitting:

Module Federation:

Considerations Before Repacking

Before integrating RePack into your React Native app, there are few considerations to keep in mind. Some limitations could impact your workflow and app’s performance.

  1. Compatibility with Native Code

RePack operates within JavaScript bundles, which means it doesn’t extend directly to native modules or native code. If your app relies heavily on native functionality, RePack’s impact may be limited in those areas.

2. Debugging and Error Handling

When using RePack, debugging can sometimes become more complex. Dynamic imports can introduce unfamiliar error messages or loading issues, particularly if network conditions vary or dependencies aren’t properly shared across modules.

from: https://tenor.com/YAUe.gif

Bundle Size Optimization

While RePack is designed to improve performance, incorrect configuration can result in larger bundle sizes! For example, neglecting to split certain dependencies or failing to share modules correctly between apps can increase load times instead of reducing them.

In this article, I introduced Module Federation in React-Native. I believe most super-apps can take much benefit from this architecture and provide smoother experience. Hope you’ve enjoyed this and please feel free to reach out to me for further explanations or any issues you might face.

--

--

Aexomir
Aexomir

Written by Aexomir

Mobile Application Developer for Life / Bug Hunter for Fun

No responses yet