Loading...
Loading...
Integrate Module Federation into an existing project — add provider (exposes modules) or consumer (loads remote modules) configuration. Use when the user wants to add Module Federation to an existing Rsbuild / Rspack / Webpack / Modern.js / Next.js / Vite project, set up a remote, create a host app, or quickly consume the demo provider to see MF working. Default role is consumer.
npx skill4agent add module-federation/core mf-integratemf-context$ARGUMENTSrsbuild.configrspack.configwebpack.configmodern.confignext.configvite.configThis looks like a new project. Run the following command to scaffold a full Module Federation project:bashnpm create module-federation@latest
remotesexposesconsumerproviderbothnamepackage.jsondemocustomkey: path./Button: ./src/components/Button.tsx'.' : './src/index'demoremotes: {
'provider': 'rslib_provider@https://unpkg.com/module-federation-rslib-provider@latest/dist/mf/mf-manifest.json',
},'provider'import ProviderApp from 'provider';customname: urlexposes: {
'./Button': './src/components/Button.tsx',
},package.jsonreactreact-dom{ singleton: true }vue{ singleton: true }rsbuild.config.tsrsbuild.config.jsmodule-federation.config.tsimport { createModuleFederationConfig } from '@module-federation/rsbuild-plugin';
export default createModuleFederationConfig({
name: '<app-name>',
// exposes: { ... }, // provider / both only
// remotes: { ... }, // consumer / both only
shareStrategy: 'loaded-first',
shared: {
// react + react-dom or vue — from Step 3
},
});rsbuild.config.tspluginModuleFederation+import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
+import moduleFederationConfig from './module-federation.config';
export default defineConfig({
plugins: [
pluginReact(),
+ pluginModuleFederation(moduleFederationConfig),
],
});pnpm add @module-federation/rsbuild-pluginmodern.config.tsmodern.config.jsmodule-federation.config.tsimport { createModuleFederationConfig } from '@module-federation/modern-js-v3';
export default createModuleFederationConfig({
name: '<app-name>',
// exposes: { ... }, // provider / both only
// remotes: { ... }, // consumer / both only
shared: {
// react + react-dom or vue — from Step 3
},
});modern.config.ts+import { moduleFederationPlugin } from '@module-federation/modern-js-v3';
export default defineConfig({
plugins: [
appTools(),
+ moduleFederationPlugin(),
],
});tsconfig.json {
"compilerOptions": {
+ "paths": {
+ "*": ["./@mf-types/*"]
+ }
}
}pnpm add @module-federation/modern-js-v3rspack.config.tsrspack.config.jsrspack.config.tsrspack.config.jsModuleFederationPluginexperiments.asyncStartup+const { ModuleFederationPlugin } = require('@module-federation/enhanced/rspack');
module.exports = {
+ experiments: {
+ asyncStartup: true,
+ },
plugins: [
+ new ModuleFederationPlugin({
+ name: '<app-name>',
+ // exposes: { ... }, // provider / both only
+ // remotes: { ... }, // consumer / both only
+ shared: {
+ // from Step 3
+ },
+ }),
],
};Note:requires Rspack > 1.7.4.experiments.asyncStartup
pnpm add @module-federation/enhancedwebpack.config.tswebpack.config.jswebpack.config.js+const { ModuleFederationPlugin } = require('@module-federation/enhanced/webpack');
module.exports = {
+ experiments: {
+ asyncStartup: true,
+ },
plugins: [
+ new ModuleFederationPlugin({
+ name: '<app-name>',
+ filename: 'remoteEntry.js',
+ // exposes: { ... }, // provider / both only
+ // remotes: { ... }, // consumer / both only
+ shared: {
+ // from Step 3
+ },
+ }),
],
};pnpm add @module-federation/enhancednext.config.tsnext.config.mjsnext.config.jsDeprecation warning:only supports Pages Router (not App Router) and is no longer actively maintained. For new projects, consider using Rsbuild or Modern.js instead.@module-federation/nextjs-mf
next.config.mjs+import { NextFederationPlugin } from '@module-federation/nextjs-mf';
const nextConfig = {
webpack(config, options) {
+ config.plugins.push(
+ new NextFederationPlugin({
+ name: '<app-name>',
+ filename: 'static/chunks/remoteEntry.js',
+ // exposes: { ... }, // provider / both only
+ // remotes: { // consumer / both only
+ // remote: `remote@http://localhost:3001/static/${options.isServer ? 'ssr' : 'chunks'}/remoteEntry.js`,
+ // },
+ shared: {},
+ extraOptions: {
+ exposePages: true,
+ enableImageLoaderFix: true,
+ enableUrlLoaderFix: true,
+ },
+ })
+ );
return config;
},
};.env.localNEXT_PRIVATE_LOCAL_WEBPACK=truepnpm add @module-federation/nextjs-mf webpack -Dvite.config.tsvite.config.jsvite.config.ts+import { federation } from '@module-federation/vite';
export default defineConfig({
plugins: [
+ federation({
+ name: '<app-name>',
+ // exposes: { ... }, // provider / both only
+ // remotes: { ... }, // consumer / both only
+ shared: {
+ // from Step 3
+ },
+ }),
],
});pnpm add @module-federation/viteDo you want me to automatically add the remote component to your app's entry so you can see it working right away?
| Bundler | Candidates (in order) |
|---|---|
| Rsbuild | |
| Modern.js | |
| Webpack / Rspack | |
| Next.js | |
| Vite | |
provider'provider'import ProviderApp from 'provider';<ProviderApp /><div>src/routes/page.tsx<ProviderApp />pages/index.tsx<ProviderApp />tsconfig.jsonsrc/remote.d.tssrc/declarations.d.tssrc/env.d.tsdeclare module '<remote-name>' {
const Component: React.ComponentType;
export default Component;
}<remote-name>providerhttp://localhost:<port>/mf-manifest.jsonhttp://localhost:<port>/static/chunks/remoteEntry.jsremotes: {
'<app-name>': '<app-name>@http://localhost:<port>/mf-manifest.json',
},package.json