Introduction
With the release of .NET 7, Microsoft included a feature to render Blazor components in JavaScript applications (RegisterCustomElement<T>
). This helps those who want to slowly migrate JavaScript applications to Blazor, but unfortunately, won’t work for exposing Blazor components as microfrontends, as it works only for JavaScript applications deployed together with the Blazor application.
In this post, I’ll present a nuget package that I’ve created as a prototype to try to solve this problem, exposing Blazor components with module federation for other applications to consume.
What is Module Federation?
Module Federation is a webpack feature that enables us to expose JavaScript modules for other applications to consume. These federated modules can be deployed independently and are isolated from one another, allowing us to build an application using the Microfrontend architecture.
What is Microfrontend architecture?
Microfrontend architecture is similar to the microservices architecture, but applied to frontend applications. An application developed using microfrontend architecture is composed of one or more microfrontends, components that are self contained, individually deployed and that can be developed in different technologies from each other.
In the image below, we can see an application with four microfrontends. Each one is developed by a different team and in a different technology.
More on microfrontends in this Martin Fowler’s post.
Angular Module Federation wrapper for Blazor
Disclaimer: This package is a prototype and not ready for production.
After installing Angular Module Federation wrapper for Blazor nuget package, it will generate an Angular application at compile time, exposing the registered Blazor components through module federation.
The exposed components accept input parameters and subscription to events.
Blazor Configuration
First, install the Blazor.ModuleFederation.Angular
Nuget package:
Install-Package Blazor.ModuleFederation.Angular
The source code for the Angular application is generated by an MSBuild task. For this, we need to configure which component will be exposed.
PokemonCards.razor:
In the .razor
file of the component, include the attribute GenerateModuleFederationComponent
at the top.
|
|
Program.cs:
In the Program.cs
file, register the component with the RegisterForModuleFederation
method.
|
|
Project.csproj:
In the .csproj
file, configure the following parameters:
- ModuleFederationName: Name of the module that will be exposed;
- MicroFrontendBaseUrl: The URL where the Blazor application will be published to;
- BuildModuleFederationScript: Enable or disable the Angular module federation wrapper generation;
- IsProduction: If the Angular app will be compiled with production configuration;
|
|
Host Configuration
PokemonCardsLoaderComponent:
Create a loader component to load the remote Blazor component. Don’t forget to include it in the app module.
|
|
AppComponent:
Include the loader component in the HTML.
|
|
webpack.config.js
Import the Blazor exposed component in the ModuleFederationPlugin.remotes
.
|
|
Sample application
https://github.com/dgenezini/BlazorModuleFederationSample
Current issues and limitations
- Only works with Blazor WebAssembly;
- Only one Blazor app can be loaded by a host (the app may expose several components);
- Blazor App server needs to have CORS enabled.