Microfrontends Single-SPA Migration: Converting create-react-app to a Microfrontend app using Single-SPA Without Eject

Microfrontends Single-SPA Migration: Converting create-react-app to a Microfrontend app using Single-SPA Without Eject

The challenge of using Run-Time Microfrontends with create-react-app (CRA) has been escalating over the past year. There are multiple reasons that prohibit you from using CRA, here are the main two reasons:

  1. CRA is built to use index.html for each SPA, while Microfrontends uses remote js files to live in one html.
  2. CRA does not allow you to modify Webpack configurations without eject

You might ask the question: "Why depend on CRA?" Well, CRA saves a lot of time and Webpack configurations that most developers are trying to avoid. Facebook keeps releasing performance improvements and new features through react-scripts. If you eject from CRA, you will not receive any updates or bug fixes. You might have to own the configurations and learn Webpack to build sites from scratch. This is not an easy choice for most developers. There is a tool that was created to get around this issue is called CRACO (Create-React-App?Configuration?Override), which I will discuss in other articles

In this article, I will explain how to convert a project that was created with create-react-app to a Single-SPA Microfrontend application.

1. Setup your Mac with Node.js and NPM/Yarn

Please, follow the following article if you need to set up a new machine.


2. Create a react app project

please, follow the following article to create a new react app from scratch using create-react-app

3. Open the project in Visual Studio code.

4. Open package.json, and it should look as follows:

No alt text provided for this image

5. Delete the dependencies and the scripts highlighted above so that the file will be as follows:

No alt text provided for this image

Note, your file might look longer with more dependencies. Just search for the highlighted above and remove them and leave the rest.

Now, run npx create-single-spa to convert to a single-spa

npx create-single-spa        

Select the options the are shown in the image below. Of course, names will be different, and you may select npm if you want

No alt text provided for this image

After that, it will ask you to overwrite both package.json and .gitignore. Select y for both

No alt text provided for this image

Notice the changes single-spa added to package.json

No alt text provided for this image

When we were configuring Module Federation, we had to write those changes ourselves. The nice thing about single-spa, it will make the needed changes for you.

6. Delete the public folder

Since we will be using Webpack directly, let's delete the public folder. To be able to merge multiple Microfrontends together, we have to include them in a single index.html. Therefore we need to delete the one created by CRA.

No alt text provided for this image

Your directory will look close to the following image:

No alt text provided for this image

All the highlighted green files were created by create-single-spa

When you open the src folder, you will find three new files as shown, below

No alt text provided for this image

rany-react-app-1.js (organization-projectname.js): this is a replacement from index.js. It is named according to the options you provided to the create-single-spa command.

Let's delete index.js since we will be using rany-react-app-1.js

The file root.component.js is the replacement for App.js. However, we will keep using App.js because this is our main app. Let's delete root.component.js and replace the import on rany-react-app-1.js

No alt text provided for this image

To be as follows:

No alt text provided for this image

Also, import index.css inside rany-react-app-1.js.

No alt text provided for this image


Jintu Babu

Frontend developer at JP Morgan chase & co

1 年

Hi, I tried the steps mentioned above to convert my existing react app into a micro frontend. But the remote entry file is not loading under localhost. what to do?

回复
Padmanathan Jeyamani

Lead Software Engineer at Encora Inc.

2 年

Hi, I followed your article and its good. But I am facing one issue. After completing the process I am getting the "Content, not from webpack is served from 'D:\Projects\micro-frontend\own\new-react-app\public'" message. it is not taking my webpack config. it is taking default CRA webpack configuration. can you please help me to solve this

回复

要查看或添加评论,请登录

Rany ElHousieny, PhD???的更多文章

社区洞察

其他会员也浏览了