Single-spa is a JavaScript framework for creating micro-frontends. It allows you to build SPAs (single-page applications) using multiple frameworks and libraries, such as Angular, React, and Vue. With the introduction of webpack module federation, it’s now easier than ever to create a Single-spa application using Angular.
Webpack module federation is a new feature of webpack 5 that enables you to share code between different JavaScript bundles. It allows you to create a federated module, which can be consumed by other applications. This is incredibly helpful when building micro-frontends, as it allows you to share common code, such as UI components and utilities, between different applications.
To create a Single-spa application using Angular and webpack module federation, you first need to set up a new Angular project. You can do this by running the following command in your terminal:
“`html
ng new my-angular-app
“`
Once you have set up your Angular project, you can then install the necessary dependencies for Single-spa and webpack module federation. You can do this by running the following commands:
“`html
npm install single-spa single-spa-angular webpack webpack-cli @angular-builders/custom-webpack --save
“`
Next, you need to configure your Angular app to work with Single-spa and webpack module federation. You can do this by creating a new webpack configuration file and adding the necessary configuration for module federation. Here’s an example of what your webpack configuration file might look like:
“`html
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
const singleSpaAngularWebpack = require('single-spa-angular-webpack');
module.exports = (_, args) => {
const singleSpaWebpackConfig = singleSpaAngularWebpack({
projectRoot: __dirname,
angularJson: 'angular.json',
});
return {
...singleSpaWebpackConfig,
plugins: [
new ModuleFederationPlugin({
name: 'my_angular_app',
library: { type: 'var', name: 'my_angular_app' },
filename: 'remoteEntry.js',
exposes: {
'./Module': './src/app/app.module.ts',
},
}),
],
};
};
“`
In this configuration file, we are using the ModuleFederationPlugin to create a federated module for our Angular app. We are exposing our app module, which can be consumed by other applications. We also specify the name of our app and the filename for our remote entry file.
Once you have set up your webpack configuration file, you can run the build command to build your Angular app with module federation support. You can do this by running the following command:
“`html
ng build --prod
“`
After building your Angular app, you should see a new remote entry file in your dist folder. This file contains the necessary code for consuming your Angular app as a federated module.
Finally, you can create a Single-spa application that consumes your Angular app as a federated module. You can do this by creating a new Single-spa root config and importing your remote entry file. Here’s an example of what your root config might look like:
“`html
import { registerApplication, start } from 'single-spa';
import { loadApp } from 'single-spa-angular';
import { APPS_CONFIG } from './constants';
Promise.all(
APPS_CONFIG.map(({ name, url, src }) => {
return loadApp({
name,
app: () => System.import(src),
activeWhen: (location) => location.pathname.startsWith(url),
});
})
).then(() => {
start();
});
“`
In this root config, we are using the loadApp function from single-spa-angular to load our Angular app as a federated module. We specify the name of our app, the location of our remote entry file, and the URL where our app should be active.
With these steps, you have successfully created a Single-spa application using Angular and webpack module federation. You can now start building micro-frontends with ease, using the power of Angular and module federation.
getting error ngmfe1 is not defined, what is the command to run
Hello, very good tutorial, but I have an error that I have not been able to resolve "NG0400: A platform with a different configuration has been created. Please destroy it" first when I share the Angular dependencies
If its possible to have the shell application as an another angular base application ? Use the parent angular application's routing to consume a module exposed by child angular application through module federation ?
how can we add a styleguide like material-ui or clarity into this, something that can be shared by each MFE? I tried adding it to the shared dependency in the webpack config for the index.ejs but that doesn't seem to work.
Awesome! Are we using the same approach for sharing the style guide and svg icons?
This will kill the nice local <-> remote replacement posibilities, since downloading is a script tag.
can I use this between an angular 15 app and an angularJs app? where the angularjs app would be the host app, thanks in advance
Nice explanation! Keep up the good work. ❤