mf-integrate by module-federation/core
npx skills add https://github.com/module-federation/core --skill mf-integrate调用 mf-context Skill(传入 $ARGUMENTS)以收集 MFContext。
如果未检测到打包器(未找到 rsbuild.config、rspack.config、webpack.config、modern.config、next.config、vite.config),这很可能是一个新项目。告知用户:
这看起来是一个新项目。运行以下命令来搭建一个完整的模块联邦项目:
npm create module-federation@latest
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
然后停止。
如果 MF 已配置(MFContext 显示现有的 remotes 或 exposes),告知用户已配置的内容,并询问他们是否要添加/修改配置或停止。
询问用户以下问题(合并到一个 AskUserQuestion 调用中):
* `consumer` — 从远程应用加载模块(默认)
* `provider` — 向其他应用暴露模块
* `both` — 既暴露模块也加载远程模块
2. 应用名称 — 此应用的 MF 名称应是什么?
* 建议使用 `package.json` 中的 `name` 字段(snake_case,无连字符)。MF 名称中不允许使用连字符。
3. 角色特定问题 :
* 如果选择 **consumer** 或 **both**:您希望连接到公共演示提供者以立即查看 MF 工作,还是配置自己的远程应用?
* `demo` — 使用公共演示提供者(消费者的默认选项)
* `custom` — 我将指定自己的远程 URL
* 如果选择 **provider** 或 **both**:您想要暴露哪些模块?提供 `key: path` 对,例如 `./Button: ./src/components/Button.tsx`。如果不确定,使用 `'.' : './src/index'` 作为默认值。
根据收集的参数构建 MF 配置:
演示提供者(当用户选择 demo 时使用):
remotes: {
'provider': 'rslib_provider@https://unpkg.com/module-federation-rslib-provider@latest/dist/mf/mf-manifest.json',
},
演示提供者在 'provider' 处暴露了一个 React 组件。用户可以在其应用中导入它:
import ProviderApp from 'provider';
自定义远程应用(当用户选择 custom 时使用):要求用户以 name: url 格式提供远程入口,然后按原样使用。
使用用户提供的条目。示例:
exposes: {
'./Button': './src/components/Button.tsx',
},
读取 package.json 以检查存在哪些框架。相应地设置单例:
react + react-dom:将两者添加为 { singleton: true }vue:添加为 { singleton: true }根据检测到的打包器应用正确的模式:
检测依据:项目根目录中的 rsbuild.config.ts / rsbuild.config.js。
module-federation.config.tsimport { createModuleFederationConfig } from '@module-federation/rsbuild-plugin';
export default createModuleFederationConfig({
name: '<app-name>',
// exposes: { ... }, // 仅 provider / both
// remotes: { ... }, // 仅 consumer / both
shareStrategy: 'loaded-first',
shared: {
// react + react-dom 或 vue — 来自步骤 3
},
});
rsbuild.config.ts将 pluginModuleFederation 添加到插件数组:
+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-plugin
检测依据:项目根目录中的 modern.config.ts / modern.config.js。
module-federation.config.tsimport { createModuleFederationConfig } from '@module-federation/modern-js-v3';
export default createModuleFederationConfig({
name: '<app-name>',
// exposes: { ... }, // 仅 provider / both
// remotes: { ... }, // 仅 consumer / both
shared: {
// react + react-dom 或 vue — 来自步骤 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-v3
检测依据:项目根目录中的 rspack.config.ts / rspack.config.js。
rspack.config.ts / rspack.config.js添加 ModuleFederationPlugin 和 experiments.asyncStartup:
+const { ModuleFederationPlugin } = require('@module-federation/enhanced/rspack');
module.exports = {
+ experiments: {
+ asyncStartup: true,
+ },
plugins: [
+ new ModuleFederationPlugin({
+ name: '<app-name>',
+ // exposes: { ... }, // 仅 provider / both
+ // remotes: { ... }, // 仅 consumer / both
+ shared: {
+ // 来自步骤 3
+ },
+ }),
],
};
注意:
experiments.asyncStartup需要 Rspack > 1.7.4。
pnpm add @module-federation/enhanced
检测依据:项目根目录中的 webpack.config.ts / webpack.config.js。
webpack.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
+ // remotes: { ... }, // 仅 consumer / both
+ shared: {
+ // 来自步骤 3
+ },
+ }),
],
};
pnpm add @module-federation/enhanced
检测依据:项目根目录中的 next.config.ts / next.config.mjs / next.config.js。
弃用警告:
@module-federation/nextjs-mf仅支持 Pages Router(不支持 App Router)且不再积极维护。对于新项目,请考虑使用 Rsbuild 或 Modern.js。
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
+ // remotes: { // 仅 consumer / both
+ // remote: `remote@http://localhost:3001/static/${options.isServer ? 'ssr' : 'chunks'}/remoteEntry.js`,
+ // },
+ shared: {},
+ extraOptions: {
+ exposePages: true,
+ enableImageLoaderFix: true,
+ enableUrlLoaderFix: true,
+ },
+ })
+ );
return config;
},
};
添加到 .env.local:
NEXT_PRIVATE_LOCAL_WEBPACK=true
pnpm add @module-federation/nextjs-mf webpack -D
检测依据:项目根目录中的 vite.config.ts / vite.config.js。
vite.config.ts+import { federation } from '@module-federation/vite';
export default defineConfig({
plugins: [
+ federation({
+ name: '<app-name>',
+ // exposes: { ... }, // 仅 provider / both
+ // remotes: { ... }, // 仅 consumer / both
+ shared: {
+ // 来自步骤 3
},
+ }),
],
});
pnpm add @module-federation/vite
对于仅 provider 角色,完全跳过此步骤。
询问用户:
您希望我自动将远程组件添加到您的应用入口,以便您能立即看到它工作吗?
如果用户说 不,只需将代码片段作为参考展示,然后继续步骤 6。
如果用户说 是:
按以下优先级顺序搜索入口组件文件:
| 打包器 | 候选文件(按顺序) |
|---|---|
| Rsbuild | src/App.tsx, src/App.jsx, src/App.js |
| Modern.js | src/routes/page.tsx, src/routes/page.jsx |
| Webpack / Rspack | src/App.tsx, src/App.jsx, src/App.js, src/index.tsx, src/index.jsx |
| Next.js | pages/index.tsx, pages/index.jsx, pages/index.js |
| Vite | src/App.tsx, src/App.jsx, src/App.js |
读取第一个存在的文件。如果未找到,告知用户需要手动修改哪个文件并展示代码片段 — 不要尝试盲目写入。
使用步骤 4 生成的配置中的远程名称:
provider,导入路径是 'provider'在文件顶部(现有导入之后)添加导入,并在现有的 JSX return 中渲染组件。
对于 React (Rsbuild / Rspack / Webpack / Vite)
在最后一个现有导入行后添加导入:
import ProviderApp from 'provider';
将 <ProviderApp /> 插入到现有的 JSX return 中。找到一个合适的位置 — 在 <div> 内部,现有内容之后。不要重构组件;只需追加元素。
对于 Modern.js (src/routes/page.tsx)
相同模式 — 添加导入并在返回的 JSX 中渲染 <ProviderApp />。
对于 Next.js (pages/index.tsx)
相同模式 — 添加导入并在返回的 JSX 中渲染 <ProviderApp />。
检查是否存在 tsconfig.json。如果存在,创建 src/remote.d.ts(或添加到现有的 src/declarations.d.ts / src/env.d.ts 如果存在):
declare module '<remote-name>' {
const Component: React.ComponentType;
export default Component;
}
将 <remote-name> 替换为实际的远程名称(例如,provider)。
告知用户,在运行开发服务器后,清单将在以下位置可用:
http://localhost:<port>/mf-manifest.jsonhttp://localhost:<port>/static/chunks/remoteEntry.js另一个应用可以通过以下方式将此应用作为远程应用引用:
remotes: {
'<app-name>': '<app-name>@http://localhost:<port>/mf-manifest.json',
},
输出简洁的总结:
package.json 中的现有脚本)每周安装量
100
代码仓库
GitHub 星标
2.5K
首次出现
2026年3月6日
安全审计
已安装于
cursor100
kimi-cli98
gemini-cli98
amp98
github-copilot98
opencode98
Call the mf-context Skill (pass $ARGUMENTS) to collect MFContext.
If no bundler can be detected (no rsbuild.config, rspack.config, webpack.config, modern.config, next.config, vite.config found), this is likely a new project. Tell the user:
This looks like a new project. Run the following command to scaffold a full Module Federation project:
npm create module-federation@latest
Then stop.
If MF is already configured (MFContext shows existing remotes or exposes), inform the user what is already configured and ask if they want to add/modify the configuration or stop.
Ask the user the following questions (combine into one AskUserQuestion call):
Role — What role should this app play?
consumer — loads modules from remote apps (default)provider — exposes modules to other appsboth — exposes modules and loads remote modulesApp name — What should the MF name be for this app?
name field from package.json (snake_case, no hyphens). Hyphens are not allowed in MF names.Role-specific :
demo — use the public demo provider (default for consumers)Construct the MF config based on the gathered parameters:
Demo provider (use when user chose demo):
remotes: {
'provider': 'rslib_provider@https://unpkg.com/module-federation-rslib-provider@latest/dist/mf/mf-manifest.json',
},
The demo provider exposes a React component at 'provider'. The user can import it in their app:
import ProviderApp from 'provider';
Custom remotes (use when user chose custom): Ask the user to provide remote entries in the format name: url, then use them as-is.
Use the entries provided by the user. Example:
exposes: {
'./Button': './src/components/Button.tsx',
},
Read package.json to check which frameworks are present. Set singletons accordingly:
react + react-dom present: add both as { singleton: true }vue present: add as { singleton: true }Apply the correct pattern for the detected bundler:
Detected by : rsbuild.config.ts / rsbuild.config.js in project root.
module-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.tsAdd pluginModuleFederation to the plugins array:
+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-plugin
Detected by : modern.config.ts / modern.config.js in project root.
module-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(),
],
});
Modify tsconfig.json to resolve remote types:
{
"compilerOptions": {
+ "paths": {
+ "*": ["./@mf-types/*"]
+ }
}
}
pnpm add @module-federation/modern-js-v3
Detected by : rspack.config.ts / rspack.config.js in project root.
rspack.config.ts / rspack.config.jsAdd ModuleFederationPlugin and experiments.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:
experiments.asyncStartuprequires Rspack > 1.7.4.
pnpm add @module-federation/enhanced
Detected by : webpack.config.ts / webpack.config.js in project root.
webpack.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/enhanced
Detected by : next.config.ts / next.config.mjs / next.config.js in project root.
Deprecation warning :
@module-federation/nextjs-mfonly supports Pages Router (not App Router) and is no longer actively maintained. For new projects, consider using Rsbuild or Modern.js instead.
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;
},
};
Add to .env.local:
NEXT_PRIVATE_LOCAL_WEBPACK=true
pnpm add @module-federation/nextjs-mf webpack -D
Detected by : vite.config.ts / vite.config.js in project root.
vite.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/vite
Skip this step entirely for provider-only role.
Ask the user:
Do you want me to automatically add the remote component to your app's entry so you can see it working right away?
If the user says no , just show the code snippet as a reference and move on to Step 6.
If the user says yes :
Search for the entry component file in this priority order:
| Bundler | Candidates (in order) |
|---|---|
| Rsbuild | src/App.tsx, src/App.jsx, src/App.js |
| Modern.js | src/routes/page.tsx, src/routes/page.jsx |
| Webpack / Rspack | src/App.tsx, src/App.jsx, src/App.js, , |
Read the first file that exists. If none found, tell the user which file to modify manually and show the snippet — do not attempt blind writes.
Use the remote name from the config generated in Step 4:
provider, import path is 'provider'Add the import at the top of the file (after existing imports) and render the component inside the existing JSX return.
For React (Rsbuild / Rspack / Webpack / Vite)
Add import after the last existing import line:
import ProviderApp from 'provider';
Insert <ProviderApp /> inside the existing JSX return. Find a natural place — inside a <div>, after existing content. Do not restructure the component; just append the element.
For Modern.js (src/routes/page.tsx)
Same pattern — add import and render <ProviderApp /> in the returned JSX.
For Next.js (pages/index.tsx)
Same pattern — add import and render <ProviderApp /> in the returned JSX.
Check if tsconfig.json exists. If it does, create src/remote.d.ts (or add to an existing src/declarations.d.ts / src/env.d.ts if present):
declare module '<remote-name>' {
const Component: React.ComponentType;
export default Component;
}
Replace <remote-name> with the actual remote name (e.g., provider).
Tell the user that after running the dev server, the manifest will be available at:
http://localhost:<port>/mf-manifest.jsonhttp://localhost:<port>/static/chunks/remoteEntry.jsAnother app can reference this app as a remote using:
remotes: {
'<app-name>': '<app-name>@http://localhost:<port>/mf-manifest.json',
},
Output a concise summary:
package.json)Weekly Installs
100
Repository
GitHub Stars
2.5K
First Seen
Mar 6, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
cursor100
kimi-cli98
gemini-cli98
amp98
github-copilot98
opencode98
Azure Pipelines 本地验证与构建管理指南 - VS Code 开发工作流优化
434 周安装
custom — I'll specify my own remote URLskey: path pairs, e.g. ./Button: ./src/components/Button.tsx. If unsure, use '.' : './src/index' as a default.src/index.tsxsrc/index.jsx| Next.js | pages/index.tsx, pages/index.jsx, pages/index.js |
| Vite | src/App.tsx, src/App.jsx, src/App.js |