Runtime Access
Before reading this chapter, it is assumed that you already understand:
- The features and capabilities of Module Federation
- The glossary of Module Federation
- How to consume and export modules
Currently, Module Federation provides two ways to register and load modules:
-
One is to declare it in the build plugin (usually in the
module-federation.config.tsfile) -
The other way is to directly register and load modules through the
runtimeAPI.
The two modes are not conflicting and can be used together. You can flexibly choose the module registration method and timing according to your actual scenario.
The differences between registering modules at runtime and registering modules in the build configuration are as follows:
If the build plugin is used, an MF instance will be automatically created and stored in memory when the project starts. You can directly call methods of the MF instance via the .
If the build plugin is not used, you need to manually create an MF instance before calling the corresponding API.
- What is a
ModuleFederationinstance?
A ModuleFederation instance is an instance of the ModuleFederation class, which contains all the functionality of the ModuleFederation runtime.
You can enter
__FEDERATION__.__INSTANCES__in the console to view the created instances.
Installation
-
In the following
Federation Runtimeexamples, we all show cases that are detached from specific frameworks such as Modern.js, so the API will be exported from the initial@module-federation/enhanced/runtimepackage. -
If your project is a Modern.js project and uses
@module-federation/modern-js, the runtime should export the runtime API from@module-federation/modern-js/runtime. This ensures that the plugin and the runtime use the same runtime instance, ensuring that modules are loaded normally. -
If your project is a Modern.js project but does not use
@module-federation/modern-js, you should export the runtime API from@module-federation/enhanced/runtime. However, we recommend that you use@module-federation/modern-jsfor module registration and loading, which will allow you to enjoy more capabilities combined with the framework.