To expose your gp-admin component via Angular Native Federation, here's the step-by-step approach:
1. Install Native Federation
In your backoffice-scs project:
ng add @angular-architects/native-federation
2. Create a Federated Module
Create a dedicated module for your component that will be exposed:
// src/app/federated/gp-admin-federated.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { GpAdminComponent } from '../gp-admin/gp-admin.component';
@NgModule({
declarations: [GpAdminComponent],
imports: [CommonModule],
exports: [GpAdminComponent]
})
export class GpAdminFederatedModule { }
3. Configure Federation
Update your federation.config.js (created by the ng add command):
const { withNativeFederation, shareAll } = require('@angular-architects/native-federation/config');
module.exports = withNativeFederation({
name: 'backoffice-scs',
exposes: {
'./GpAdminComponent': './src/app/federated/gp-admin-federated.module.ts',
},
shared: {
...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
},
skip: [
'rxjs/ajax',
'rxjs/fetch',
'rxjs/testing',
'rxjs/webSocket',
]
});
4. Create Bootstrap File
Ensure your src/main.ts is set up for federation:
// src/main.ts
import { initFederation } from '@angular-architects/native-federation';
initFederation()
.catch(err => console.error(err))
.then(_ => import('./bootstrap'))
.catch(err => console.error(err));
And create src/bootstrap.ts:
// src/bootstrap.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
5. Update Angular.json
Make sure your build configuration uses the federation builder:
{
"build": {
"builder": "@angular-architects/native-federation:build",
"options": {
// your existing options
}
},
"serve": {
"builder": "@angular-architects/native-federation:dev-server",
"options": {
// your existing options
}
}
}
6. Build and Serve
ng build
ng serve
This will create a remoteEntry.js file that other applications can consume.
7. Consuming in Other Applications
In the consuming application:
Install Native Federation:
ng add @angular-architects/native-federation
Configure federation.config.js:
const { withNativeFederation, shareAll } = require('@angular-architects/native-federation/config');
module.exports = withNativeFederation({
name: 'consumer-app',
remotes: {
'backoffice-scs': 'http://localhost:4200/remoteEntry.js',
},
shared: {
...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
},
});
Use the component:
// In your consuming component
import { loadRemoteModule } from '@angular-architects/native-federation';
async loadGpAdminComponent() {
const module = await loadRemoteModule({
remoteName: 'backoffice-scs',
exposedModule: './GpAdminComponent'
});
return module.GpAdminComponent;
}
Or use it declaratively in a route:
{
path: 'gp-admin',
loadChildren: () => loadRemoteModule({
remoteName: 'backoffice-scs',
exposedModule: './GpAdminComponent'
}).then(m => m.GpAdminFederatedModule)
}
Additional Considerations
- Shared Dependencies: Make sure both apps share the same major versions of Angular and other dependencies
- Styling: Consider how CSS/styling will be handled across federated modules
- Type Safety: You might want to create shared type definitions for better development experience
- Error Handling: Implement proper error handling for when remote modules fail to load
This setup allows your gp-admin component to be consumed by multiple applications while maintaining the ability to develop and deploy it independently.