For Angular 11 I strongly recommend you to take a look at implementation with Webpack 5 Module Federation
🎉 https://github.com/alexzuza/angular-plugin-architecture-with-module-federation
🛠️ Github demo angular-plugin-architecture
Maybe Ivy can change something but for the time being I use the solution that uses Angular CLI Custom Builder and meets the following requirements:
The usage is simple as:
ng build --project plugins --prod --modulePath=./plugin1/plugin1.module#Plugin1Module
--pluginName=plugin1 --sharedLibs=shared --outputPath=./src/assets/plugins
More on this in my article:
I created a repository on github with a solution which might help. It uses Angular 6 libraries and 1 base applications which load up the UMD bundled libraries lazily; https://github.com/lmeijdam/angular-umd-dynamic-example
If you have any suggestions, please feel free to add!
I have just published a new chapter for my book "Developing with Angular" that addresses the topic of plugins in Angular 2+ and should be of a great interest to people that are trying to build external plugins.
Key points:
The book is free to get, and has "pay what you want" model. Feel free to grab a copy and hope that helps.
Example application with a working plugin system (thanks to Gijs for founding the github repo!) https://github.com/PacktPublishing/Mastering-Angular-2-Components/tree/master/angular-2-components-chapter-10 based on the eBook Mastering Angular 2 Components
Cheers, Niklas
What you're looking for is lazy module loading. Here is an example of it: http://plnkr.co/edit/FDaiDvklexT68BTaNqvE?p=preview
import {Component} from '@angular/core';
import {Router} from '@angular/router';
@Component({
selector: 'my-app',
template: `
<a [routerLink]="['/']">Home</a> |
<a [routerLink]="['/app/home']">App Home</a> |
<a [routerLink]="['/app/lazy']">App Lazy</a>
<hr>
<button (click)="addRoutes()">Add Routes</button>
<hr>
<router-outlet></router-outlet>
`
})
export class App {
loaded: boolean = false;
constructor(private router: Router) {}
addRoutes() {
let routerConfig = this.router.config;
if (!this.loaded) {
routerConfig[1].children.push({
path: `lazy`,
loadChildren: 'app/lazy.module#LazyModule'
});
this.router.resetConfig(routerConfig);
this.loaded = true;
}
}
}
Best...Tom
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With