Explaining Angular with Micro-Frontends
The user wants to build an Angular project using micro-frontends with Module Federation. It seems they’re looking for guidance on setting this up. I need to provide a concise explanation with moderate detail about architecture, steps to create host and remote applications using Angular CLI and Webpack 5 Module Federation plugin. I should include project structure, configuration, sample code, and how to run it. It’s important to ensure my information is current, reflecting knowledge through 2024.Setting Up Angular with Module Federation
For Angular 16 and above, we can use Standalone components with Module Federation. A simple way to set this up is through the @angular-architects/module-federation plugin. We might want to create a host shell and two remote applications.
First, we can choose Angular 17, which should be stable and current as of late 2024. Our steps will include creating the workspace, adding Module Federation, configuring the necessary files, and displaying components. I’ll aim for a focused and step-by-step outline without being overly lengthy.Drafting Angular Micro-Frontend Setup
I’m outlining the answer in structured sections. First, I’ll start with an overview of the architecture and tools.
Then, I’ll guide the user step-by-step: we’ll scaffold the Angular workspace, create the shell (host) app and micro-frontends (remotes), install and configure the Module Federation plugin, implement the remote micro-frontend with exposed modules/components, and configure the host to load these remotes dynamically. Finally, I’ll mention running the apps along with version compatibility and potential pitfalls. I'll make sure to include commands and code in a clear format.Recalling Module Federation API
I need to look at the API from the @angular-architects/module-federation documentation.
For the shell, I can set it up using the command: ng add @angular-architects/module-federation --project shell --type dynamic-host --port 4200. For a remote application, the command would be: ng add @angular-architects/module-federation --project mfe1 --port 4201 --type remote. This generates a module-federation.config.ts file, which I can configure to expose modules.
Next, I'll configure the shell to load remote modules dynamically, using provided code snippets for clarity. This should keep it informative yet straightforward.Adapting to Angular 17's Features
I need to adapt to Angular 17's standalone features, potentially using loadComponent, but I'll keep it module-based for simplicity, as the user might prefer that. When generating projects, I can mention using --standalone=false for clarity.
I’ll propose a structure with a shell and two remotes. I should also guide on running these concurrently and mention automatic updates to angular.json by the plugin. It's worth noting that Angular 12+ is needed for Webpack 5 Module Federation. Finally, I'll prepare a brief overview and steps for setup, including a possible minimal package.json. Below is a concrete, end‑to‑end recipe to build an Angular micro‑frontend setup using Webpack Module Federation.
I’ll assume:
- Angular CLI ≥ 16 (Webpack 5 enabled by default).
- You’re OK using NgModules for clarity (works fine even in Angular 17+; just generate apps with
--standalone=false).
Architecture:
shell – host application
mfe1 – remote micro‑frontend (can copy the pattern for more MFEs)
1. Create the Angular workspace and apps
# 1. Create a new workspace (no initial app is fine)
ng new microfrontends-workspace --no-create-application
cd microfrontends-workspace
# 2. Create host shell app (module-based)
ng g application shell --routing --no-standalone --style=scss
# 3. Create a remote app
ng g application mfe1 --routing --no-standalone --style=scss
You now have:
projects/shell
projects/mfe1
2. Install the Module Federation helper
The Angular Architects plugin makes Module Federation integration straightforward:
npm install @angular-architects/module-federation --save-dev
3. Configure the host (shell) as a Module Federation host
Run the schematic for the shell:
ng add @angular-architects/module-federation \
--project shell \
--type dynamic-host \
--port 4200
What this does (automatically):
- Adds a
webpack.config.ts / webpack.prod.config.ts (or module-federation.config.ts depending on version).
- Switches the builder in
angular.json to use a custom webpack config.
- Sets up shared dependencies (Angular, RxJS, etc).
You’ll see a file like projects/shell/webpack.config.ts (name may be webpack.config.js/ts or module-federation.config.ts, depending on version). It will roughly look like:
// projects/shell/webpack.config.ts
import { shareAll, withModuleFederationPlugin } from '@angular-architects/module-federation/webpack';
export default withModuleFederationPlugin({
remotes: {
// Will fill this in after we configure mfe1
},
shared: shareAll({
singleton: true,
strictVersion: true,
requiredVersion: 'auto',
}),
});
We’ll come back to the remotes section after configuring mfe1.
4. Configure the remote micro‑frontend (mfe1)
Run the schematic for mfe1:
ng add @angular-architects/module-federation \
--project mfe1 \
--type remote \
--port 4201 \
--name mfe1
This:
- Adds webpack/module-federation config for
mfe1.
- Exposes a default module or component.
- Changes
angular.json to use the custom webpack builder.
Open the generated federation config, something like:
// projects/mfe1/webpack.config.ts
import { shareAll, withModuleFederationPlugin } from '@angular-architects/module-federation/webpack';
export default withModuleFederationPlugin({
name: 'mfe1',
exposes: {
// key: what the host will refer to
// value: path to the module in this project
'./Module': './projects/mfe1/src/app/mfe1/mfe1.module.ts',
},
shared: shareAll({
singleton: true,
strictVersion: true,
requiredVersion: 'auto',
}),
});
If this file doesn’t yet point to a concrete module you want to expose, create one.
4.1. Create a feature module in mfe1 to expose
cd projects/mfe1
ng g module mfe1 --routing
ng g component mfe1/components/home
cd ../../..
Update mfe1-routing.module.ts:
// projects/mfe1/src/app/mfe1/mfe1-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './components/home/home.component';
const routes: Routes = [
{ path: '', component: HomeComponent }, // default route for this MFE
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class Mfe1RoutingModule {}
Update mfe1.module.ts:
// projects/mfe1/src/app/mfe1/mfe1.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Mfe1RoutingModule } from './mfe1-routing.module';
import { HomeComponent } from './components/home/home.component';
@NgModule({
declarations: [HomeComponent],
imports: [CommonModule, Mfe1RoutingModule],
})
export class Mfe1Module {}
Now adjust the federation config to expose this module (if it isn’t already):
// projects/mfe1/webpack.config.ts
export default withModuleFederationPlugin({
name: 'mfe1',
exposes: {
'./Module': './projects/mfe1/src/app/mfe1/mfe1.module.ts',
},
// ...
});
The remoteEntry.js for this remote will be served at:
http://localhost:4201/remoteEntry.js
5. Wire the remote into the shell
Update the shell’s webpack.config.ts to know about mfe1:
// projects/shell/webpack.config.ts
import { shareAll, withModuleFederationPlugin } from '@angular-architects/module-federation/webpack';
export default withModuleFederationPlugin({
remotes: {
mfe1: 'http://localhost:4201/remoteEntry.js',
},
shared: shareAll({
singleton: true,
strictVersion: true,
requiredVersion: 'auto',
}),
});
5.1. Add a route in the shell to load the remote
Install (if not already) the helper inside the shell app:
npm install @angular-architects/module-federation
Update shell’s routing to lazy‑load the remote module.
// projects/shell/src/app/app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { loadRemoteModule } from '@angular-architects/module-federation';
const routes: Routes = [
{
path: 'mfe1',
loadChildren: () =>
loadRemoteModule({
type: 'module',
remoteEntry: 'http://localhost:4201/remoteEntry.js',
exposedModule: './Module',
}).then((m) => m.Mfe1Module),
},
{
path: '',
pathMatch: 'full',
redirectTo: 'mfe1',
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
Now navigating to /mfe1 in the shell should pull in the remote module from mfe1.
6. Run the system
In two terminals:
# Terminal 1 – remote
ng serve mfe1 --port 4201
# Terminal 2 – host shell
ng serve shell --port 4200
Open: http://localhost:4200/mfe1
You should see the HomeComponent from mfe1 rendered inside the shell.
7. Scaling up: multiple micro‑frontends
To add another MFE (e.g., mfe2):
-
Generate an app:
ng g application mfe2 --routing --no-standalone
-
Run ng add @angular-architects/module-federation --project mfe2 --type remote --port 4202 --name mfe2
-
Expose a module in mfe2’s webpack.config.ts.
-
Add mfe2 to shell’s webpack.config.ts remotes:
remotes: {
mfe1: 'http://localhost:4201/remoteEntry.js',
mfe2: 'http://localhost:4202/remoteEntry.js',
}
-
Add another route using loadRemoteModule for mfe2.
8. Key benefits & considerations (brief)
-
Benefits
- Independent deployment/versioning of
mfe1, mfe2, etc.
- Teams can own their MFEs end‑to‑end.
- Shared Angular/runtime via Module Federation reduces bundle size.
-
Considerations
- Shared dependency versioning must be kept compatible.
- Cross‑MFE communication (events, state, routing) needs architecture decisions (e.g., shared services, message bus).
If you tell me your Angular version and whether you prefer standalone components, I can adjust the example to use standalone micro‑frontends (exposing components instead of NgModules) and update the routing accordingly.