Posted onEdited onInangular Symbols count in article: 747Reading time ≈3 mins.
Introduction
In this article, we will explore the Angular Module Federation feature in depth. Please read the first article in this series to get a basic understanding of Module Federation.
remoteEntry.js
Each time you call loadRemoteModule, Angular will fetch the remoteEntry.js file first from the remote server. What is this file? Let’s take a look at the remoteEntry.js file in the remote project.
You can find the remoteEntry.js file in the dist folder of the remote project. The file is generated by the ModuleFederationPlugin in the webpack configuration. But the file under the dist folder is minimized and hard to read. You can read it on Chrome network tab when you call loadRemoteModule.
This file is very large and contains lots of code, you can scroll down to the bottom or search container entry to find key part.
remoteEntry.js mainly contains the following information:
1. The list of modules that are exposed by the remote project.
In the following example, the remote project exposes a module named ./Component from its ./projects/mfe1/src/app/product/product.component.ts file.
remoteEntry.js is a file that contains the list of modules that are exposed by the remote project.
Angular fetches the remoteEntry.js file first before loading the remote module.
The remoteEntry.js file is generated by the ModuleFederationPlugin in the webpack configuration.
shell/host project does not have a remoteEntry.js file.
loadRemoteModule
How loadRemoteModule works in Angular Module Federation? Let’s take a look at the source code of the loadRemoteModule function in the @angular-architects/module-federation package.
The entry point is from the router file, we call loadRemoteModule function to load the remote module. The loadRemoteModule function is defined in the load-remote-module.ts file in the @angular-architects/module-federation package.
Here is the source code of the loadRemoteModule function from webpack:///node_modules/@angular-architects/module-federation-runtime/fesm2022/angular-architects-module-federation-runtime.mjs
We can see that it uses the import function to load the remoteEntry.js file. The import function is a dynamic import function that fetches the remoteEntry.js file from the remote server. After loading the remoteEntry.js file, it calls the initRemote function to initialize the remote container. This container is used to get the remote module later.
After loading the remoteEntry.js file, then it calls lookupExposedModule function to get the module from the remote project.