I am currently working with two separate Angular applications:
- app1 = parentApp (the main shell application), running on port 4200
- app2 = childApp (the micro front-end I want to integrate into parentApp), running on port 4201
I aim to utilize a component from childApp within parentApp using Module Federation. Despite numerous failed attempts following tutorials, it seems like the right approach. Prior to incorporating the component, I am focusing on routing to it.
childApp consists of an additional module containing a single component, here is some code:
childApp - app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {AbcComponent} from "./test/abc/abc.component";
const routes: Routes = [
{path: "abc", component: AbcComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
childApp - test.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AbcComponent } from './abc/abc.component';
@NgModule({
declarations: [
AbcComponent
],
imports: [
CommonModule
]
})
export class TestModule { }
childApp - abc.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-abc',
templateUrl: './abc.component.html',
styleUrls: ['./abc.component.scss']
})
export class AbcComponent {
}
childApp - abc.component.html
<p>abc works!</p>
childApp - app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
{TestModule} from "./test/test.module"";
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
TestModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
and the most crucial childApp - webpack.config.ts
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const mf = require("@angular-architects/module-federation/webpack");
const path = require("path");
const share = mf.share;
const sharedMappings = new mf.SharedMappings();
sharedMappings.register(
path.join(__dirname, 'tsconfig.json'),
[/* mapped paths to share */]);
module.exports = {
output: {
uniqueName: "childApp",
publicPath: "auto"
},
optimization: {
runtimeChunk: false
},
resolve: {
alias: {
...sharedMappings.getAliases(),
}
},
experiments: {
outputModule: true
},
plugins: [
new ModuleFederationPlugin({
library: { type: "module" },
//For remotes (please adjust)
name: "childApp",
filename: "remoteEntry.js",
exposes: {
'./TestModule': './src/app/test/test.module.ts',
},
shared: share({
"@angular/core": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/common": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/common/http": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/router": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
...sharedMappings.getDescriptors()
})
}),
sharedMappings.getPlugin()
],
};
parentApp is a basic application without any complexities or components. It simply contains one module and a welcome page. Let's take a look at the code.
parentApp - app.component.html
<h1>this is the parent app</h1>
<router-outlet></router-outlet>
parentApp - app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
parentApp - app.routing.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {loadRemoteModule} from "@angular-architects/module-federation";
const routes: Routes = [
{
path: 'childApp',
loadChildren: () => loadRemoteModule({
remoteEntry: 'http://localhost:4201/remoteEntry.js',
remoteName: 'childApp',
exposedModule: './TestModule',
}).then(m => m.TestModule)
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
parentApp - webpack.config.ts
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const mf = require("@angular-architects/module-federation/webpack");
const path = require("path");
const share = mf.share;
const sharedMappings = new mf.SharedMappings();
sharedMappings.register(
path.join(__dirname, 'tsconfig.json'),
[/* mapped paths to share */]);
module.exports = {
output: {
uniqueName: "parentApp",
publicPath: "auto"
},
optimization: {
runtimeChunk: false
},
resolve: {
alias: {
...sharedMappings.getAliases(),
}
},
experiments: {
outputModule: true
},
plugins: [
new ModuleFederationPlugin({
library: { type: "module" },
remotes: {
"childApp": "http://localhost:4201/remoteEntry.js",
},
shared: share({
"@angular/core": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/common": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/common/http": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/router": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
...sharedMappings.getDescriptors()
})
}),
sharedMappings.getPlugin()
],
};
Despite being able to access http://localhost:4201/remonteEntry.js, attempting to access http://localhost:4200/childApp results in a typescript error: container is undefined.
I suspect that I might be implementing lazy loading incorrectly... Additionally, upon inspecting network requests in my browser, I noticed requests going from parentApp to childApp for all shared modules except for the specific module I intend to share, which raises some concerns.
Where could I potentially be making mistakes? Are there any typos in my code? And if you believe I am overlooking critical information here, is there any documentation or tutorial that you can recommend to assist me with this integration? Thank you.