This plugin implements Module Federation 1.5.
import { rspack } from '@rspack/core';
export default {
output: {
// set uniqueName explicitly to make HMR works
uniqueName: 'app',
},
plugins: [
new rspack.container.ModuleFederationPlugin({
// options
}),
],
};stringProvide a path as the implementation for Module Federation 1.5 runtime, which defaults to @module-federation/runtime-tools.
string[]Provide the plugin required to run Module Federation 1.5, which can extend the behavior and capabilities of Module Federation.
stringDefine the unique name exposed to other containers in the current build. This name will exist as a global variable for the remote container.
stringSpecify the filename of the remote container entry file. Other containers will load the exposed modules through this file.
string | falseDefine the runtime chunk for remote container entry.
LibraryOptionsDefine the output format of remote container entry. The default libraryType is "var".
stringDefine the namespace for shared dependencies in the current container. By configuring share scopes between different containers, the sharing behavior of modules can be controlled, including determining which modules are shared between different containers. The default share scope is "default".
'version-first' | 'loaded-first'Control the loading strategy of shared dependencies:
'version-first': Version takes precedence. After setting, all remotes entry files will be automatically loaded and register the corresponding shared dependencies to ensure that all shared dependency versions can be obtained. This strategy is recommended when there are strict version requirements.
'loaded-first': reuse first. After setting, the remotes entry file will not be automatically loaded (it will only be loaded when needed), and registered shared dependencies will be reused first. This strategy is recommended when there are no strict requirements on the version and performance is required.
ExternalsTypeDefines how to load remote containers, defaulting to "script", which loads via the <script /> tag.
type Remotes = (RemotesItem | RemotesObject)[] | RemotesObject;
type RemotesItem = string;
type RemotesItems = RemotesItem[];
interface RemotesObject {
[k: string]: RemotesConfig | RemotesItem | RemotesItems;
}
interface RemotesConfig {
external: RemotesItem | RemotesItems;
shareScope?: string;
}Definition of the modules and their addresses that will be loaded remotely. The key is the name of the remote container, the value is the global variable name exposed by the remote container and the URL of the remote container entry. You can also specify shareScope to control whether the remote container shares dependencies.
type Exposes = (ExposesItem | ExposesObject)[] | ExposesObject;
type ExposesItem = string;
type ExposesItems = ExposesItem[];
interface ExposesObject {
[k: string]: ExposesConfig | ExposesItem | ExposesItems;
}
interface ExposesConfig {
import: ExposesItem | ExposesItems;
name?: string;
}Define how local modules can be referenced by remote containers. The key is the name of the module when referenced as a remote module in the remote container, and the value is the module path relative to the current folder. You can provide a name to specify the name of the exposed local module.
type Shared = (SharedItem | SharedObject)[] | SharedObject;
type SharedItem = string;
interface SharedObject {
[k: string]: SharedConfig | SharedItem;
}
interface SharedConfig {
import?: false | SharedItem;
eager?: boolean;
packageName?: string;
requiredVersion?: false | string;
shareKey?: string;
shareScope?: string;
singleton?: boolean;
strictVersion?: boolean;
version?: false | string;
}Specify which dependencies should be shared dependencies. This allows multiple micro-frontends to share the same instance of a dependency library to avoid loading the same code repeatedly. It can be an object dictionary where the keys are the names of the shared modules and the values are configuration or version strings. It can also be an array where the array items are the shared package names or configurations.
The SharedConfig can include the following sub-options:
true, the shared module will be loaded in the initial chunk instead of being dynamically loaded when used. This means that the shared module will be loaded together with the main entry point regardless of whether it has been used. This can eliminate the delay caused by dynamic loading, but it will increase the size of the initial package. Also, please note that when this configuration is enabled, all provided modules and fallback modules will always be downloaded.package.json. Configuration is only necessary when the package name cannot be automatically determined based on the request."^1.2.3". Used to set the version range of shared modules. If the module version of the remote container does not meet this range, the module will not be loaded."default".requiredVersion. If set to true, the shared module must match the version specified in requiredVersion exactly, otherwise an error will be reported and the module will not be loaded. If set to false, it can tolerate imprecise matching.package.json will be used.type Manifest = boolean | ManifestConfig;
interface ManifestConfig {
filePath?: string;
disableAssetsAnalyze?: boolean;
fileName?: string;
}Used to control whether to generate a manifest and the corresponding generation configuration.
When enabled, the plugin emits both mf-manifest.json and mf-stats.json (you can customize the base name through fileName) on every build and writes them into the build output so other tooling can read them during the processAssets hook or from the final artifacts.
mf-stats.json: Contains full build statistics, including the asset lists for exposes/shared/remotes, metaData (plugin version, build info, remoteEntry, etc.), and extra asset analysis data, making it suitable for later aggregation or diagnostics.mf-manifest.json: A runtime manifest distilled from the stats file. The structure stays stable and can be consumed by Module Federation clients when loading remote modules. The exposes/shared/remotes sections describe what is publicly exposed.ManifestConfig provides the following options:
filePath: Target directory for the manifest files. Applies to both manifest and stats outputs.fileName: Manifest file name. When set, the stats file automatically appends a -stats suffix (for example, fileName: 'mf.json' produces mf.json and mf-stats.json). All files are emitted into the directory defined by filePath (if provided).disableAssetsAnalyze: Disables asset analysis. When true, the manifest omits the shared and exposes fields, and the remotes entries will not include asset information.Found non-downgraded syntax in the build output?
If you need to be compatible with legacy browsers, please add builtin:swc-loader for syntax downgrade, and make sure it matches packages under @module-federation scope. Here is an example:
export default {
module: {
rules: [
{
include: [/@module-federation[\\/]/],
use: {
loader: 'builtin:swc-loader',
options: {
jsc: {
target: 'es5',
},
},
},
},
],
},
};Multiple assets emit different content to the same filename mf-manifest.json
Upgrade the @module-federation scoped npm package to 0.21.0 or above.