Building a Microfrontend Application with Angular and Module Federation
Module Federation allows you to dynamically load parts of a web application from independently deployable JavaScript packages at runtime. This challenge will guide you in setting up a basic Angular application that utilizes Module Federation to consume a simple "Remote Entry" component from a separate, simulated remote application. This is a crucial technique for building microfrontend architectures, enabling teams to work on isolated parts of a larger application and deploy them independently.
Problem Description
You need to create two Angular applications: a host application and a remote application. The host application will load and display a component from the remote application using Module Federation. The remote application will expose a simple component that displays a greeting message.
What needs to be achieved:
- Remote Application: Create a minimal Angular application that exposes a single component called
RemoteEntryComponent. This component should simply display the text "Hello from the Remote!". - Host Application: Create another Angular application that acts as the host. This application should be configured to consume the
RemoteEntryComponentfrom the remote application using Module Federation. The host application should display the remote component on its main page. - Module Federation Configuration: Properly configure both applications with the necessary Module Federation settings, including the remote entry point URL for the host and the exposed module for the remote.
Key Requirements:
- Both applications must be functional Angular projects.
- The host application must successfully load and render the remote component.
- The remote application must expose the component correctly.
- The applications should be configured to use a development server for easy testing.
- Use the latest Angular version (>= 16) and the
@angular-devkit/build-angulartoolchain.
Expected Behavior:
When you run the host application, it should display the text "Hello from the Remote!" rendered by the RemoteEntryComponent from the remote application. The remote application can be running independently or stopped; Module Federation will dynamically load it at runtime.
Edge Cases to Consider:
- Network connectivity issues between the host and remote applications. (While not required to handle explicitly, be aware of this potential issue in a real-world scenario).
- Incorrect Module Federation configuration leading to failed loading of the remote component.
- Version mismatches between Angular versions in the host and remote applications (ensure consistency).
Examples
Example 1:
Input: Host application configured to load RemoteEntryComponent from 'http://localhost:4201'
Output: Host application displays "Hello from the Remote!"
Explanation: The host application successfully fetches and renders the remote component from the specified URL.
Example 2:
Input: Remote application exposes RemoteEntryComponent with the correct module federation configuration.
Output: Host application can successfully load and render the RemoteEntryComponent.
Explanation: The remote application correctly publishes the component, allowing the host to consume it.
Constraints
- Both applications must be Angular projects using TypeScript.
- The remote entry point URL for the host application should be
http://localhost:4201. (This is a suggested default; you can adjust it, but ensure consistency). - The remote application should expose a single module containing the
RemoteEntryComponent. - The solution should be deployable and runnable using standard Angular development servers (
ng serve). - Focus on the core Module Federation setup; styling and complex component interactions are not required.
Notes
- You'll need to modify the
angular.jsonfiles of both projects to enable Module Federation. Pay close attention to thebuildOptionssection. - The
@angular-devkit/build-angulartoolchain handles the Module Federation configuration. - Start with a minimal Angular project for both the host and remote applications to simplify the setup.
- Debugging Module Federation can be tricky. Use your browser's developer tools to inspect network requests and JavaScript execution to identify any issues.
- Consider using a separate terminal for each application to run
ng serveconcurrently. - The remote application does not need to be built separately. Module Federation will dynamically load it.