Key Concepts
- Module Federation
- Micro Frontends
- React Native
- RS Pack
- Webpack
- Shared Libraries
- Runtime Updates
- Zephyr Cloud
- Remote Modules
- Host Application
- Bundle Sharing
- Asynchronous Loading
- React Lazy
- Suspense
- Version Control
- Git Integration
Cart Application Setup
- Goal: Extract cart functionality from Tennis World into a separate Cart application for sharing.
- Technology: Callstack's Repack is used to initialize a new React Native application named "cart".
yarn repack init module cart --template react-native-template-typescript - Bundler: RS Pack is chosen for its speed, although Webpack is also compatible with Module Federation.
- Code Transfer: The
add cart,cart store, andcart listcomponents are copied from the Tennis World application into the new Cart application. - State Management: Tand is used for shared state management.
yarn add @tandemly/state
Module Federation Configuration (Cart Application)
- Objective: Configure the Cart application to expose its components as a shared bundle.
- RS Pack Configuration: The
moduleFederationplugin is configured withinrs pack.config.js. - Module Definition:
name: "cart" (the name of the module)filename: "cart.bundle.js" (the output file name)exposes: Defines which components to share:./add-to-cart-button:add to cart buttoncomponent./cart-list:cart listcomponent
- Shared Dependencies:
- Dependencies like
react,react-native, andtandare shared to avoid version conflicts and reduce bundle size. - The
sharedproperty is configured to iterate through thepackage.jsondependencies. Singleton: trueensures only one version of each library is used.Eager: trueloads the shared dependencies immediately.requiredVersionspecifies the exact version frompackage.json.
- Dependencies like
- Error Handling:
- The
module Federation enhancedmodule is added as a dev dependency to resolve an initial error.yarn add -D @callstack/repack-module-federation - The application is configured to run on a specific port (9000) to avoid address conflicts.
- The
- Bundle Generation: Running the bundler creates a module export containing the necessary information for other applications to consume the Cart module.
Hiking World Integration
- Objective: Integrate the Cart module into the Hiking World application.
- RS Pack Configuration (Hiking World):
- The
moduleFederationplugin is used again, but this time to consume a remote module. name: "hiking_world"remotes: Defines the remote module to consume:cart: Points to the URL of the Cart module bundle (initiallyhttp://localhost:9000/cart.bundle).
- The
- Shared Dependencies: The same shared dependencies configuration is used as in the Cart application.
- Component Import:
- The
add to cart buttonis imported into theproductspage. React.lazyis used for asynchronous loading of the remote component.Suspenseis used to handle the loading state of the asynchronous component.
- The
- UI Integration: The
add to cart buttonis added to the product cards. - Cart Display:
- The
cart listcomponent is also lazily loaded and wrapped inSuspense. useStateis used to manage the visibility of the cart.- A button is added to toggle the cart's visibility.
- The
Tennis World Integration
- Process: The same integration steps used for Hiking World are applied to Tennis World.
- RS Pack Configuration (Tennis World): The
moduleFederationplugin is configured to consume the remote Cart module. - Code Reuse: The product display and cart integration code from Hiking World are copied and adapted for Tennis World.
- Verification: The
add to cart buttoncolor is changed in the Cart application to demonstrate live updates across both Hiking World and Tennis World.
Zephyr Cloud Integration
- Objective: Host the Cart module on Zephyr Cloud for production deployment.
- Zephyr Plugin: The Zephyr plugin is installed as a development dependency.
yarn add -D @zephyrcloud/plugin-module-federation - Bundler Configuration: The
withZephyrfunction wraps the RS Pack configuration to connect to Zephyr Cloud. - Git Integration: Zephyr Cloud requires a Git repository for identification and version control.
- Repository Setup: A new private GitHub repository is created for the Cart application.
- Code Upload: The Cart application code is pushed to the GitHub repository.
- Zephyr Cloud Connection: The bundler is restarted, which opens a browser window for Zephyr Cloud authentication.
- Module Deployment: Zephyr Cloud automatically detects and deploys the Cart module.
- Remote URL Update: The remote URL in Hiking World and Tennis World is updated to point to the Zephyr Cloud URL of the Cart module.
- Version Rollback: Zephyr Cloud's version control features are demonstrated by rolling back to a previous version of the Cart module.
Conclusion
The video demonstrates how to use Module Federation with React Native to share code between multiple applications at runtime. It covers setting up a shared Cart module, integrating it into Hiking World and Tennis World, and deploying it to Zephyr Cloud for production. The key takeaways are the ability to update features independently, reduce bundle sizes, and improve development velocity. Module Federation is presented as a powerful tool for building micro frontend architectures in React Native, with Zephyr Cloud providing a managed hosting solution.
AI summaries can miss context or contain errors. Check important details against the original video.





