Basic CRA with Rsbuild
Check out the example project list here: Rsbuild CRA
Setup Environment
Before getting started, you will need to install Node.js, and ensure that your Node.js version >= 16. We recommend using the LTS version of Node.js 20.
You can check the currently used Node.js version with the following command:
If you do not have Node.js installed in your current environment, or the installed version is too low, you can use nvm or fnm to install the required version.
Here is an example of how to install the Node.js 20 LTS version via nvm:
Step 1: Setup React Applications
Create React Project
You can use create-rsbuild to create a project with Rsbuild + React. Just execute the following command:
Create App 1
Create App 2
Install
Use React in an existing project
To compile React, you need to register the Rsbuild React Plugin. The plugin will automatically add the necessary configuration for React builds.
For example, register in rsbuild.config.ts:
For projects using Create React App, you can refer to the CRA Migration Guide.
Ensure that Webpack version 5 or above is installed by checking the installation report provided by Yarn.
Step 2: Installing Module Federation Build Plugin
Step 3: Update Entry Files
In both applications, rename the index.js file to bootstrap.js. This change allows bootstrap.js to load asynchronously, which is essential for Module Federation to function correctly between the two applications.
Update the contents of bootstrap.tsx to the following:
Now, create a new index.tsx file in both applications with the following content to import bootstrap.tsx:
Step 4: Create and Expose
Now, create a component to expose from MFE2
4.1 Create Button Component
In MFE2, create a new file named Button.tsx in the src directory with the following content:
4.2 Update App.tsx
Update App.tsx in MFE2 to import and render the Button component:
Step 5: Configure Rsbuild in MFE2
First create module-federation.config.ts in file in the root directory of MFE2 with the following configuration:
And then modify the rsbuild.config.ts file in the root directory of MFE2 with the following configuration:
Step 6: Consume Remote Module
Consume the exposed module from MFE2 in MFE1
6.1 Update App.tsx
Update App.tsx in MFE1 to import and render the MFE2 Button component:
Step 6.2: Configure Rsbuild in MFE1
First create module-federation.config.ts in file in the root directory of MFE1 with the following configuration:
And then modify the rsbuild.config.ts file in the root directory of MFE1 with the following configuration:
This setup initiates Module Federation within MFE1, and upon initiating the development server, it is accessible at http://localhost:3001.
Similarly, the configuration activates Module Federation for MFE2, thereby exposing the Button component at http://localhost:3002/remoteEntry.js. With the development server operational, it becomes accessible at http://localhost:3002.