Module Federation
This chapter introduces how to build Module Federation output in Rslib.
Usage scenarios
Module federation has some typical usage scenarios, including:
- Allows independent applications (called "Micro-Frontend" in the Micro-Frontend architecture) to share modules without having to recompile the entire application.
- Different teams work on different parts of the same application without having to recompile the entire application.
- Dynamic code loading and sharing between applications at runtime.
Module Federation can help you:
- Reduce code duplication
- Improve code maintainability
- Reduce the overall size of the application
- Improve application performance
Quick start
First install the Module Federation Rsbuild Plugin.
Then register the plugin in the rslib.config.ts file:
In this way, we have completed the integration of Rslib Module as a producer. After the construction is completed, we can see that the mf directory has been added to the product, and consumers can directly consume this package.
In the above example we added a new format: 'mf' , which will help you add an additional Module Federation product, while also configuring the format of cjs and esm , which does not conflict.
However, if you want this Rslib Module to consume other producers at the same time, do not use the build configuration remote parameter, because in other formats, this may cause errors, please refer to the example below using the Module Federation runtime.
Develop MF remote module
Use host app
Rslib support developing Module Federation Rslib project with a host application.
1. Start rslib mf-dev command of library
Adding the dev command to the package.json file:
Then run the dev command can start the Module Federation development mode,
enabling consumption by your host app, along
with Hot Module Replacement (HMR).
2. Start host app
Set up the host app to consume the Rslib Module Federation library. Check out the @module-federation/rsbuild-plugin for more information.
Then start the host app with rsbuild dev.
Use Storybook
Rslib support developing Module Federation Rslib project with Storybook.
1. Start rslib mf-dev command of library
Adding the dev command to the package.json file:
Then run the dev command can start the Module Federation development mode, enabling consumption by Storybook, along with Hot Module Replacement (HMR).
2. Set up Storybook configuration
First, set up Storybook with the Rslib project. You can refer to the Storybook chapter to learn how to do this. In this chapter, we will use React as the framework for our example.
-
Install the following Storybook addons to let Storybook work with Rslib Module Federation:
- storybook-addon-rslib: Storybook addon that lets Storybook load the Rslib config.
- @module-federation/storybook-addon: Storybook addon that sets up the Module Federation config for Storybook.
-
Then set up the Storybook configuration file
.storybook/main.ts, specify the stories and addons, and set the framework with corresponding framework integration..storybook/main.ts
3. Writing stories with remote module
Import components from remote module.
4. Add Module Federation types and stories into tsconfig.json.
5. Start Storybook app
There you go, start Storybook with npx storybook dev.
Consume other Module Federation modules
Because there are multiple formats in Rslib, if you configure the remote parameter to consume other modules during construction, it may not work properly in all formats. It is recommended to access through the Module Federation Runtime
First install the runtime dependencies
Then consume other Module Federation modules at runtime, for example
This ensures that modules can be loaded as expected in multiple formats.
FAQs
How to control the loading strategy of shared dependencies when the producer and consumer build patterns are different
If the Rslib producer is built with build, this means that the process.env.NODE_ENV of the producer is production . If the consumer is started in dev mode at this time,
due to the shared loading strategy of Module Federation being version-first by default, there may be problems loading into different modes of react and react-dom (e.g. react in development mode, react-dom in production mode).
You can set up shareStrategy at the consumer to solve this problem, but make sure you fully understand this configuration
How to make module federated outputs generate export of ES modules
If you want Rslib producers' module federated outputs to generate the export of ES Modules, you can additionally configure as follows:
Examples
You can refer to the example projects in Rslib Module Federation Example.
mf-host: Rsbuild App Consumermf-react-component: Rslib Module, which is both a producer and a consumer, provides the module to themf-hostas a producer, and consumes themf-remotemf-remote: Rsbuild App Producer
