Angular native federation.
I have an existing angular project "backoffice-scs" with a component "gp-admin".
gp-admin has component.ts component.html and is used in the backoffice-scs application.
I want to provide the gp-admin component to other applications via module federation (or angular implementation native-federation) whats the best way to achieve this?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The overall “best practice” is to encapsulate your remote component in its own dedicated Angular module that you then expose via Module Federation. That way, you can keep your component self‐contained and make it easier to share, lazy load, and update independently.
Wrap your gp-admin component inside its own Angular module (if it isn’t already). For example:
typescript
// src/app/gp-admin/gp-admin.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { GpAdminComponent } from './gp-admin.component';
@NgModule({
declarations: [GpAdminComponent],
imports: [CommonModule],
exports: [GpAdminComponent] // export if needed internally too
})
export class GpAdminModule { }
This serves as the “remote” module that you’ll expose via federation.
2. Configure Module Federation
If you’re using the Angular CLI with the module federation plugin (via the Angular Architects schematic), you can add or update your federation configuration. For example, create or update a file (often named module-federation.config.js):
javascript
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
module.exports = {
output: {
publicPath: "auto",
uniqueName: "backofficeScs"
},
optimization: {
// Important for de-duplication of shared modules
runtimeChunk: false
},
resolve: {
// alias for shared libraries if necessary
},
plugins: [
new ModuleFederationPlugin({
name: "backofficeScs",
filename: "remoteEntry.js",
exposes: {
// Expose the module containing your gp-admin component
'./GpAdmin': './src/app/gp-admin/gp-admin.module.ts',
},
shared: {
// Share Angular packages to avoid duplication
"@angular/core": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/common": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
"@angular/router": { singleton: true, strictVersion: true, requiredVersion: 'auto' },
// add any other shared packages as needed
}
})
]
};
This configuration tells webpack to expose your GpAdminModule under the key ./GpAdmin for other applications to load remotely.
Note: Depending on your Angular version and build system, you might need to integrate this configuration with your custom webpack configuration. The Angular Architects plugin provides schematics to set this up automatically.
3. Building and Serving
Make sure your project builds and serves the remote entry file (usually at /remoteEntry.js). When you serve your application (the “remote”), the remoteEntry file will be available and used by hosts to load the GpAdmin module.
4. Consuming the Remote Module in Another App
In the host (or shell) application, configure the remotes in its module-federation.config.js (or similar configuration file):
javascript
new ModuleFederationPlugin({
remotes: {
backofficeScs: "backofficeScs@http://localhost:4200/remoteEntry.js"
},
// ... other config
})
Then, in your routing (or anywhere you wish to load the remote component), you can lazy load the remote module using Angular’s dynamic import pattern. For example, in your routing module:
This dynamic import will fetch your GpAdminModule from the remote application at runtime.
Summary
By following these steps:
Encapsulate your component inside a dedicated module.
Expose the module in your webpack Module Federation configuration.
Configure your host to load the remote module dynamically.
You ensure that your gp-admin component is shared as a microfrontend, making it available to other Angular applications while taking advantage of the benefits (like shared dependencies and lazy loading) provided by Module Federation.
This method separates concerns, preserves encapsulation, and leverages the native federation support in Angular with minimal overhead. Enjoy your microfrontend architecture!