Global variables
In Module Federation, the main global variables are __FEDERATION__, __SHARE__ and __INSTANCES__. They record the module federation info, shared module info, and instance info of the current application, and help us understand and debug the runtime behavior of module federation.
Global variables are read-only — do not modify them.
FEDERATION
After initialization, the __FEDERATION__ variable will be injected globally. This variable is an object that contains all the module federation information of the current application.
moduleInfo
It is recommended to use chrome devtools to view the processed moduleInfo.
Record all module information of the current application. Based on this information, you can get the module's dependencies.
The moduleInfo structure is an object whose key consists of two parts: {moduleName}(:{moduleVersion}).
-
moduleNameis the name of the module, andmoduleVersionis the version of the module or itsurl. -
moduleVersiononly exists for producer modules.
The following example shows how to use moduleInfo to get dependencies, assuming the following moduleInfo:
moduleInfo
In the above example, moduleInfo contains information about 5 modules, among which manifest_host does not have a version number after it, indicating that it is a consumer module.
The remotesInfo field in manifest_host records the information of all producer modules it depends on, and the corresponding matchedVersion field records the version or url it matches.
The corresponding producer module information can be obtained through the key + matchedVersion in the consumer remotesInfo field, such as webpack_provider:http://localhost:3009/mf-manifest.json.
version
Only exists in producers, the value of consumers is an empty string
The version or url of the current module.
buildVersion
The build version of the current module, usually taken from the version field in package.json.
globalName
Only exists in the producer
The global variable name of the current producer module. You can get the producer's container through globalThis[globalName], which is usually taken from the name field in package.json.
publicPath
The publicPath of the current module.
getPublicPath
This field and
publicPathare mutually exclusive
The getPublicPath of the current module, get the final publicPath through new Function(getPublicPath)() call.
ssrPublicPath
The publicPath of the server file of the current module.
remoteEntry
Only exists in the producer
The name of the entry file of the current producer module.
remoteEntryType
Only exists in the producer
The entry file type of the current producer module.
remoteTypesZip
Only exists in the producer
The name of the compressed package of the type declaration file of the current producer module.
remoteTypesAPI
Only exists in the producer
The name of the type API declaration file of the current producer module.
shared
All shared module information that the current module depends on. The type definition is as follows:
modules
Only exists in the producer
All module information exported by the current module. The type definition is as follows:
SHARE
All shared module information currently registered (loaded), the type definition is as follows:
Type declaration
We can use __SHARE__ to query the loading status of a specific shared. For example, if the following __SHARE__ exists:
__SHARE__
In the data above, you can see the shared module information for the two modules manifest_host and app1. For example, to inspect how the react module was loaded, follow these steps:
reacthas only one version,18.3.1.- Check
__SHARE__["app1:0.1.94"].default.react["18.3.1"]—loadedistrue, indicating the module has been loaded. - The
fromfield iswebpack_provider, meaning thereactmodule was provided by that module. - Finally, look at the
useInfield: it containswebpack_providerandapp1, meaning this version ofreactis consumed by both modules.
INSTANCES
The currently loaded MF instance. It stores a list of all loaded MF modules. Every list entry has the following structure:
bridgeHook
Contains metadata about the MF bridge which is used to enable cross-application use-cases (React, Vue) to load properly.
hooks
Contains metadata about the MF hooks which are used to tap into the MF lifecycle.
loaderHook
Contains metadata about the MF loaderHook.
moduleCache
Contains a Map storing all MF modules with their metadata available in the moduleCache.
name
The name of the MF module.
options
Useful for debugging your modules.
Vital data about the MF module, such as loaded plugins, connected remotes, shareStrategy and the shared dependency object.
remoteHandler
Contains metadata about the MF remoteHandler including the idToRemoteMap which stores all bindings to connected remote components exposed by the connected MF remotes.
shareScopeMap
Contains metadata about the MF shared scope including data about every connected shared dependency.
sharedHandler
Contains metadata about the MF sharedHandler.
snapshotHandler
Useful to check the mf-manifest.json structure and status.
Contains metadata about the MF snapshotHandler including the manifestCache object and the manifestLoading object.
version
The version of the Mf module.
PRELOADED_MAP
The key-value-map of all preloaded modules (build-time). key represents the module name, and value represents the boolean value of the loaded status.
MANIFEST_LOADING
Contains metadata about the manifestLoading object. This is the same as the snapshotHandler.manifestLoading in __INSTANCES__.