These instructions will guide you through setting up a Single-SPA demo project from scratch, including configuring Webpack, installing necessary dependencies, setting up micro front-ends, and running the application.
-
Create a New Project Directory:
Open your terminal and navigate to the location where you want to create your project. For example, in
C:\Projects:cd C:\Projects
Create a new directory for your SPA project and navigate into it:
mkdir spa cd spa -
Initialize a New Node.js Project:
Run the following command to initialize a new Node.js project. This will create a
package.jsonfile:npm init -y
-
Install Single-SPA, Webpack, and Webpack CLI:
Run the following command to install Single-SPA, Webpack, and Webpack CLI as dependencies:
npm install single-spa webpack webpack-cli --save
-
Install Webpack Dev Server and HTML Webpack Plugin:
Install
webpack-dev-serverandhtml-webpack-pluginto serve your application during development:npm install webpack-dev-server html-webpack-plugin --save-dev
-
Install Babel Loader and Related Packages:
We will use Babel to transpile JavaScript and JSX. Install
babel-loaderand necessary Babel presets:npm install babel-loader @babel/core @babel/preset-env @babel/preset-react --save-dev
-
Install React and React-DOM (for Micro Front-Ends):
If you are using React for your micro front-ends, install React and React-DOM:
npm install react react-dom --save
-
Create a Webpack Configuration File:
In your
spadirectory, create a file namedwebpack.config.js:touch webpack.config.js
-
Edit
webpack.config.jsto Configure Webpack and Dev Server:Open
webpack.config.jsin your text editor and add the following configuration:const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/root-config.js', // Entry point for Single-SPA root configuration output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), publicPath: '/', }, mode: 'development', devServer: { static: path.resolve(__dirname, 'dist'), compress: true, port: 9000, historyApiFallback: true, // Ensures all requests go to index.html for SPA routing }, plugins: [ new HtmlWebpackPlugin({ template: 'src/index.html', // Reference to your HTML file }), ], module: { rules: [ { test: /\.js$/, // Apply this rule to .js files exclude: /node_modules/, // Exclude node_modules from transpilation use: { loader: 'babel-loader', // Use babel-loader for transpiling options: { presets: ['@babel/preset-env', '@babel/preset-react'], // Babel presets }, }, }, ], }, };
-
Save Your Changes:
Save the changes to
webpack.config.js.
-
Create the Required Directories and Files:
Create the
srcdirectory:mkdir src cd srcInside
src, create the following files:touch root-config.js index.html
-
Edit
index.htmlto Add the HTML Structure:Open
src/index.htmlin your text editor and add the following content:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>SPA Demo</title> </head> <body> <div id="root"></div> <!-- Ensure SystemJS is loaded before the bundle --> <script src="https://cdn.jsdelivr.net/npm/systemjs@6.10.2/dist/system.min.js"></script> <script> System.config({ map: { '@spa/header': 'http://localhost:9001/header.js', '@spa/main': 'http://localhost:9002/main.js' } }); </script> <script src="bundle.js"></script> <!-- Ensure this script is correctly linked --> </body> </html>
-
Edit
root-config.jsto Register Applications with Single-SPA:Open
src/root-config.jsand add the following content to register your applications:import { registerApplication, start } from 'single-spa'; // Register Micro Front-End Applications registerApplication({ name: '@spa/header', app: () => window.System.import('@spa/header'), activeWhen: ['/'], }); registerApplication({ name: '@spa/main', app: () => window.System.import('@spa/main'), activeWhen: ['/'], }); // Initialize the DOM elements where the micro front-ends will be mounted document.getElementById('root').innerHTML = ` <div id="header"></div> <div id="main"></div> `; // Start the Single-SPA application orchestrator start();
-
Save Your Changes:
Save the changes to
index.htmlandroot-config.js.
-
Create Micro Front-End Directories:
In the
srcdirectory, create separate folders for each micro front-end (e.g.,headerandmain):mkdir header main
-
Create
index.jsfor Each Micro Front-End:Inside each micro front-end directory (
headerandmain), create anindex.jsfile:cd header touch index.js cd ../main touch index.js
-
Add Basic React Components to Each Micro Front-End:
- For
header/index.js:
import React from 'react'; import ReactDOM from 'react-dom'; function Header() { return <header><h1>Welcome to the SPA Header!</h1></header>; } // Mount function to start up the micro front-end export function mount(props) { console.log('Mounting @spa/header'); ReactDOM.render(<Header />, document.getElementById('header')); } // Unmount function to clean up the micro front-end export function unmount(props) { console.log('Unmounting @spa/header'); ReactDOM.unmountComponentAtNode(document.getElementById('header')); }
- For
main/index.js:
import React from 'react'; import ReactDOM from 'react-dom'; function Main() { return <main><p>This is the main content area of the SPA.</p></main>; } // Mount function to start up the micro front-end export function mount(props) { console.log('Mounting @spa/main'); ReactDOM.render(<Main />, document.getElementById('main')); } // Unmount function to clean up the micro front-end export function unmount(props) { console.log('Unmounting @spa/main'); ReactDOM.unmountComponentAtNode(document.getElementById('main')); }
- For
-
Save Your Changes:
Save all the changes to your micro front-end files and
root-config.js.
-
Add Build and Start Scripts to
package.json:Open
package.jsonand ensure the"scripts"section looks like this:"scripts": { "build": "webpack", "start": "webpack serve" }
-
Start the Development Server:
Make sure you are in the root of the
spadirectory, then run:npm start
-
Open the Application in a Browser:
Open your browser and navigate to
http://localhost:9000.
-
Check the UI:
Ensure that the UI elements from your micro front-end applications (like the header and main content) are visible.
-
Check the Console and Network Tab:
-
Console Tab: Look for any JavaScript errors or warnings.
-
Network Tab: Ensure all JavaScript files (
header.js,main.js, etc.) are loaded without 404 errors.
-
-
Inspect HTML Structure:
- Open the Elements tab in Developer Tools and verify that the
divelements (<div id="header"></div>,<div id="main"></div>) exist.
- Open the Elements tab in Developer Tools and verify that the
-
Run the Build Command:
Run the following command to build the application for production:
npm run build
-
Serve the Built Application:
- Serve the contents of the
distfolder using a static server:
npx serve -s dist
- Open your browser and navigate to the served address (usually
http://localhost:5000).
- Serve the contents of the
By following these instructions, you will set up a Single-SPA project with multiple micro front-end applications, configure Webpack and Babel.