{{announcement.body}}
{{announcement.title}}

Building Micro-Frontends With Single-spa, React, and Vue

DZone 's Guide to

Building Micro-Frontends With Single-spa, React, and Vue

In this article, we discuss how to build micro-frontends with Single-spa React and Vue in order to better manage the components of our application.

· Web Dev Zone ·
Free Resource

When I see something new and controversial like this, I always want to try it out myself to see what all of the hype is about and also so I can form my own opinions about the subject.

This led me down the path to creating a micro-frontend application that rendered two separate React applications, along with a single Vue application.

In this tutorial, I'll share what I've learned and show you how to build a micro-frontend app consisting of a React and a Vue application. 

To view the final code for this application, click here.

Single SPA

The tool we will be using to create our project is Single SPA — a JavaScript framework for frontend microservices.

Single SPA enables you to use multiple frameworks in a single-page application, allowing you to split code by functionality and have Angular, React, Vue.js, etc. apps all living together.

You may be used to the days of the Create React APP CLI and the Vue CLI. With these tools, you can quickly spin up an entire project, complete with Webpack configurations, dependencies, and boilerplate ready to go for you.

If you're used to this ease of setup, then this first part may be somewhat jarring. That is because we will be creating everything from scratch, including installing all of the dependencies we need as well as creating the Webpack and Babel configuration from scratch.

Getting Started

The first thing you'll need to do is create a new folder to hold the application and change into the directory:

Shell


Next, we'll initialize a new package.json file: 

Shell


Now, this is the fun part. We will install all of the dependencies that we will need for this project. I will split these up into separate steps.

 Installing Regular Dependencies 

Shell


Installing Babel Dependencies

Shell


Installing Webpack Dependencies

Shell


Now, all of the dependencies have been installed, and we can create our folder structure.

The main code of our app will live in an src directory. This src directory will hold subfolders for each of our applications. Let's go ahead and create the React and Vue application folders within the src folder:

Shell


Now, we can create the configuration for both Webpack and Babel.

Creating Webpack Configuration

In the root of the main application, create a webpack.config.js file with the following code:

JavaScript


Creating Babel Configuration

In the root of the main application, create a .babelrc file with the following code:

JSON

Initializing Single-spa

Registering applications is how we tell single-spa when and how to bootstrap, mount, and unmount an application.

In the webpack.config.js file, we set the entry point to be single-spa.config.js.

Let's go ahead and create that file in the root of the project and configure it.

Single-spa.config.js

JavaScript


This file is where you register all of the applications that will be part of the main Single Page App. Each call to registerApplication registers a new application and takes three arguments:

  1. App name.
  2. Loading function (what entry point to load).
  3. Activity function (logic to tell whether to load the app).

Next, we need to create the code for each of our apps.

React App

In src/react, create the following two files:

Shell


src/react/main.app.js

JavaScript

src/react/root.component.js

JavaScript


Vue app

In src/vue, create the following two files:

Shell


src/vue/vue.app.js

JavaScript


src/vue/main.vue

HTML


Next, create the index.html file in the root of the app:

Shell


Index.html

HTML


Updating Package.json With Scripts

To run the app, let's add the start script as well as a build script in package.json:

JSON


Running the app

To run the app, run the start script:

Shell


Now, you can visit the following URLs:

Plain Text


Conclusion

Overall, setting up this project was fairly painless with the exception of all of the initial boilerplate setup.

I think in the future, it would be nice to have some sort of CLI that handles much of the boilerplate and initial project setup.

If you have the need for this type of architecture, Single-spa definitely seems like the most mature way to do it as of today and was really nice to work with.

Topics:
javascript application, microfrontends, microservice architecture, react, spa, tutorial, vue app

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}