|
|
3 tuần trước cách đây | |
|---|---|---|
| .. | ||
| README_AUTO_EXPOSES.md | 2 tháng trước cách đây | |
| generateExposesPlugin.ts | 1 tháng trước cách đây | |
| generateRoutesInfoPlugin.ts | 3 tuần trước cách đây | |
| zipDistPlugin.ts | 1 tháng trước cách đây | |
这个插件会根据 Vue Router 的路由配置自动生成 Module Federation 的 exposes 配置。
/home → './home': './src/views/HomeView.vue'/home/dashboard → './home/dashboard': './src/views/DashboardView.vue'/,生成规范的模块路径// src/router/routes.ts
export const routes: RouteRecordRaw[] = [
{
path: '/home',
name: 'Home',
component: () => import('../views/HomeView.vue'),
meta: {
title: '首页视图',
description: '应用首页,展示主要内容和导航',
category: 'Page',
icon: 'home',
tags: ['首页', '主页面'],
},
children: [
{
path: 'dashboard',
name: 'Dashboard',
component: () => import('../views/DashboardView.vue'),
meta: {
title: '仪表盘',
description: '展示应用的主要数据和统计信息',
category: 'Page',
icon: 'dashboard',
tags: ['仪表盘', '数据展示'],
},
},
],
},
{
path: '/about',
name: 'About',
component: () => import('../views/AboutView.vue'),
meta: {
title: '顶部导航栏',
description: '全局头部导航组件,包含Logo和菜单',
category: 'Layout',
icon: 'header',
tags: ['导航', '布局'],
},
},
]
// rsbuild.config.ts
import { routes } from './src/router/routes';
import { generateExposesFromRoutes } from './plugins/generateExposesPlugin';
export default defineConfig({
plugins: [
pluginModuleFederation({
name: 'remote1',
// 🚀 根据路由自动生成 exposes 配置
exposes: generateExposesFromRoutes(routes).exposes,
// ... 其他配置
}),
],
});
exposes: {
'./home': './src/views/HomeView.vue',
'./home/dashboard': './src/views/DashboardView.vue',
'./about': './src/views/AboutView.vue',
}
运行测试脚本查看生成的配置:
npx tsx scripts/test-exposes.ts
输出示例:
📋 Detailed expose items:
┌─────────┬────────────────────┬─────────────────────────────────┬──────────────┐
│ (index) │ Expose Key │ Component Path │ Title │
├─────────┼────────────────────┼─────────────────────────────────┼──────────────┤
│ 0 │ './home' │ './src/views/HomeView.vue' │ '首页视图' │
│ 1 │ './home/dashboard' │ './src/views/DashboardView.vue' │ '仪表盘' │
│ 2 │ './about' │ './src/views/AboutView.vue' │ '顶部导航栏' │
└─────────┴────────────────────┴─────────────────────────────────┴──────────────┘
// host 应用 rsbuild.config.ts
export default defineConfig({
plugins: [
pluginModuleFederation({
name: 'host',
remotes: {
remote1: 'remote1@http://localhost:3000/mf-manifest.json',
},
}),
],
});
// 在 Vue 组件中使用
const HomeView = defineAsyncComponent(() =>
import('remote1/home')
)
const DashboardView = defineAsyncComponent(() =>
import('remote1/home/dashboard')
)
const { exposes, exposeItems } = generateExposesFromRoutes(routes);
// exposeItems 包含详细的 meta 信息
console.log(exposeItems);
/*
[
{
key: './home',
value: './src/views/HomeView.vue',
meta: {
title: '首页视图',
description: '应用首页,展示主要内容和导航',
category: 'Page',
icon: 'home',
tags: ['首页', '主页面'],
}
},
// ... more items
]
*/
利用 exposeItems 中的 meta 信息,你可以自动生成模块使用文档。
routes.ts 文件中,以便在构建环境中也能使用() => import(...)project/
├── src/
│ └── router/
│ ├── routes.ts # 路由配置(新增)
│ └── index.ts # Router 实例
├── plugins/
│ └── generateExposesPlugin.ts # 自动生成插件
├── scripts/
│ └── test-exposes.ts # 测试脚本
└── rsbuild.config.ts # Rsbuild 配置