@lemon-fe/vite-plugin-micro-frontend 1.2.3 → 2.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +231 -72
- package/dist/cli.js +20 -14
- package/dist/index.d.ts +52 -17
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/runtime-bridge.d.ts +73 -0
- package/dist/runtime-bridge.js +184 -0
- package/dist/runtime-bridge.js.map +1 -0
- package/dist/runtime-dynamic.js +14 -194
- package/dist/runtime-dynamic.js.map +1 -1
- package/dist/runtime-federation.d.ts +19 -0
- package/dist/runtime-federation.js +36 -0
- package/dist/runtime-federation.js.map +1 -0
- package/dist/runtime-react-refresh-init.js +45 -42
- package/dist/runtime-react-refresh-init.js.map +1 -1
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.js +2 -2
- package/package.json +23 -22
- package/dist/runtime-qiankun.d.ts +0 -27
- package/dist/runtime-qiankun.js +0 -72
- package/dist/runtime-qiankun.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @lemon-fe/vite-plugin-micro-frontend
|
|
2
2
|
|
|
3
|
-
Vite
|
|
3
|
+
基于 Module Federation Runtime、Vite 插件和官方 React Bridge 的微前端工具集。
|
|
4
|
+
v2 生成的完整微应用不再依赖 qiankun。
|
|
4
5
|
|
|
5
6
|
## 安装
|
|
6
7
|
|
|
@@ -8,13 +9,16 @@ Vite 微前端插件集合,包含路由自动生成、模块联邦、qiankun
|
|
|
8
9
|
pnpm add @lemon-fe/vite-plugin-micro-frontend
|
|
9
10
|
```
|
|
10
11
|
|
|
12
|
+
要求 Node.js `>=20.19.0`、React 18、Vite 5 到 Vite 8。官方
|
|
13
|
+
`@module-federation/vite@1.21.4` 不再支持 Vite 4。
|
|
14
|
+
|
|
11
15
|
## 功能特性
|
|
12
16
|
|
|
13
|
-
- 🚀 **HTML 转换** - 移除开发环境下的 React Refresh 脚本,避免微前端冲突
|
|
14
17
|
- 📁 **路由自动生成** - 根据 `pages` 目录自动生成 UMI 风格的路由配置
|
|
15
|
-
- 🔗 **模块联邦** -
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
+
- 🔗 **模块联邦** - 使用 `@module-federation/vite` 和 Runtime 加载远程模块
|
|
19
|
+
- 🌉 **React Bridge** - 自动暴露完整应用的 `./export-app` provider
|
|
20
|
+
- 🧭 **能力发现** - 开发和生产环境自动提供 `micro-frontend.json`
|
|
21
|
+
- ♨️ **远程 HMR** - 默认启用官方 `dev.remoteHmr`,支持线上基座代理本地微应用
|
|
18
22
|
- 📦 **CLI 生成** - 支持通过命令单独生成 mf / routes 文件(类似 `umi generate tmp`)
|
|
19
23
|
|
|
20
24
|
## 使用方式
|
|
@@ -44,16 +48,16 @@ pnpm add @lemon-fe/vite-plugin-micro-frontend
|
|
|
44
48
|
// mf.config.js
|
|
45
49
|
module.exports = {
|
|
46
50
|
routes: {
|
|
47
|
-
pagesDir:
|
|
48
|
-
outputPath:
|
|
51
|
+
pagesDir: 'src/pages',
|
|
52
|
+
outputPath: 'src/routes.ts',
|
|
49
53
|
routeTemplate: {},
|
|
50
54
|
},
|
|
51
55
|
federation: {
|
|
52
56
|
remotes: [
|
|
53
|
-
{ remoteName:
|
|
54
|
-
{ remoteName:
|
|
57
|
+
{ remoteName: 'ama', entry: '/app-ama/remote.js' },
|
|
58
|
+
{ remoteName: 'whs', entry: '/app-whs/remote.js' },
|
|
55
59
|
],
|
|
56
|
-
outputPath:
|
|
60
|
+
outputPath: 'src/utils/mf.tsx',
|
|
57
61
|
},
|
|
58
62
|
};
|
|
59
63
|
```
|
|
@@ -86,75 +90,116 @@ npx mf generate
|
|
|
86
90
|
|
|
87
91
|
```typescript
|
|
88
92
|
// vite.config.ts
|
|
89
|
-
import { defineConfig } from
|
|
90
|
-
import {
|
|
91
|
-
|
|
92
|
-
createMfAlias,
|
|
93
|
-
defaultRemotes,
|
|
94
|
-
} from "@anthropic/vite-plugin-micro-frontend";
|
|
95
|
-
import * as path from "path";
|
|
93
|
+
import { defineConfig } from 'vite';
|
|
94
|
+
import { microFrontendPlugins } from '@lemon-fe/vite-plugin-micro-frontend';
|
|
95
|
+
import * as path from 'path';
|
|
96
96
|
|
|
97
97
|
export default defineConfig({
|
|
98
98
|
plugins: [
|
|
99
99
|
...microFrontendPlugins({
|
|
100
|
-
appName:
|
|
101
|
-
useDevMode: process.env.NODE_ENV === "development",
|
|
100
|
+
appName: 'wms',
|
|
102
101
|
federation: {
|
|
103
|
-
name:
|
|
104
|
-
|
|
102
|
+
name: 'wms',
|
|
103
|
+
application: {
|
|
104
|
+
rootComponent: 'src/app-root.tsx',
|
|
105
|
+
},
|
|
106
|
+
bridge: {
|
|
107
|
+
enableBridgeRouter: true,
|
|
108
|
+
},
|
|
105
109
|
exposes: {
|
|
106
|
-
|
|
107
|
-
|
|
110
|
+
'./ProcessingInboundDetail': './src/exposes/processing-inbound-detail/index.tsx',
|
|
111
|
+
},
|
|
112
|
+
remotes: [
|
|
113
|
+
{
|
|
114
|
+
remoteName: 'ama',
|
|
115
|
+
isESModule: true,
|
|
116
|
+
entry: '/app-ama/remote.js',
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
// 未使用 externals 提供 React 时,建议配置为 singleton。
|
|
120
|
+
shared: {
|
|
121
|
+
react: { singleton: true },
|
|
122
|
+
'react-dom': { singleton: true },
|
|
108
123
|
},
|
|
109
|
-
remotes: defaultRemotes, // 或自定义远程模块配置
|
|
110
124
|
},
|
|
111
125
|
routes: {
|
|
112
|
-
pagesDir:
|
|
113
|
-
outputPath:
|
|
114
|
-
},
|
|
115
|
-
qiankun: {
|
|
116
|
-
enabled: true,
|
|
117
|
-
name: "wms",
|
|
126
|
+
pagesDir: 'src/pages',
|
|
127
|
+
outputPath: 'src/routes.ts',
|
|
118
128
|
},
|
|
119
129
|
}),
|
|
120
130
|
],
|
|
121
131
|
resolve: {
|
|
122
132
|
alias: {
|
|
123
|
-
|
|
124
|
-
|
|
133
|
+
'@': path.resolve(__dirname, 'src'),
|
|
134
|
+
'@@/mf': path.resolve(__dirname, 'src/utils/mf.tsx'),
|
|
125
135
|
},
|
|
126
136
|
},
|
|
127
137
|
});
|
|
128
138
|
```
|
|
129
139
|
|
|
140
|
+
默认情况下,插件会为 `src/main.tsx` 生成官方 React Bridge provider 适配层,并自动增加
|
|
141
|
+
`"./export-app"`。适配层不会写入业务源码,微应用独立打开时仍会执行原有渲染逻辑;
|
|
142
|
+
现有入口可以继续调用
|
|
143
|
+
`renderWithQiankun({ renderContainer })`,该名称在 v2 中只是
|
|
144
|
+
`registerMicroFrontend()` 的弃用别名,不再引入或注册 qiankun 生命周期。
|
|
145
|
+
|
|
146
|
+
新应用推荐配置 `application.rootComponent`。插件会直接把该组件交给官方 React 18 Bridge,
|
|
147
|
+
并按照官网的 `bridge.enableBridgeRouter` 配置自动识别 React Router v5/v6/v7,再接入对应的
|
|
148
|
+
官方 Bridge Router adapter;不会导入独立启动入口或
|
|
149
|
+
再创建一层业务 React Root。React Router 版本由插件从消费项目中自动识别。
|
|
150
|
+
根组件应同时支持独立运行与 Bridge 传入的 `basename` 等属性:独立入口自行渲染
|
|
151
|
+
根组件,基座加载时则由 `./export-app` provider 渲染。
|
|
152
|
+
微应用正常使用 `react-router-dom` 的 `BrowserRouter`,无需定义 `BridgeRouteReporter`,
|
|
153
|
+
也无需从根组件向布局透传 `onNavigate`。插件通过官方 Bridge Router 处理 basename 和路由上下文,
|
|
154
|
+
并在其内部自动监听 `pathname`、`search`、`hash`、`state`。
|
|
155
|
+
如果基座需要同步页签或菜单,可通过 Bridge props 传入 `onNavigate`,插件会自动通知它;
|
|
156
|
+
基座可通知自身 Router 消费已经变化的浏览器地址,不要再次 push 同一地址。
|
|
157
|
+
这层通知是插件对基座状态同步的补充,并非官方 `bridge-react@2.9.0` 自带的双向回调。
|
|
158
|
+
它不会改写 history;独立运行或未传入回调时不会向基座发送通知。
|
|
159
|
+
|
|
160
|
+
生产构建会同时生成:
|
|
161
|
+
|
|
162
|
+
- `remote.js`:ES Module remote entry,保留给已有组件消费配置。
|
|
163
|
+
- `mf-manifest.json`:Module Federation Runtime 使用的正式清单。
|
|
164
|
+
- `micro-frontend.json`:基座区分旧构建与 React Bridge 新构建的能力文件。
|
|
165
|
+
|
|
166
|
+
只需要暴露联邦组件、不作为完整微应用加载时,设置 `federation.application = false`。
|
|
167
|
+
已有专用 Bridge provider 时,可以手动配置 `federation.exposes["./export-app"]`;插件将直接使用它,不再生成适配层。
|
|
168
|
+
|
|
130
169
|
### 按需使用单独插件
|
|
131
170
|
|
|
132
171
|
```typescript
|
|
133
172
|
import {
|
|
134
|
-
htmlRemoveFreshPlugin,
|
|
135
173
|
pagesRoutesPlugin,
|
|
174
|
+
federationPlugin,
|
|
136
175
|
mfGeneratorPlugin,
|
|
137
|
-
} from
|
|
176
|
+
} from '@lemon-fe/vite-plugin-micro-frontend';
|
|
138
177
|
|
|
139
178
|
export default defineConfig({
|
|
140
179
|
plugins: [
|
|
141
|
-
htmlRemoveFreshPlugin(),
|
|
142
180
|
pagesRoutesPlugin({
|
|
143
|
-
pagesDir:
|
|
181
|
+
pagesDir: 'src/pages',
|
|
144
182
|
routeTemplate: {
|
|
145
|
-
defaultLoadingComponentPath:
|
|
146
|
-
intlPath:
|
|
147
|
-
pageAliasPrefix:
|
|
183
|
+
defaultLoadingComponentPath: '@/components/loading',
|
|
184
|
+
intlPath: '@/utils/intl',
|
|
185
|
+
pageAliasPrefix: '@/pages',
|
|
186
|
+
},
|
|
187
|
+
}),
|
|
188
|
+
...federationPlugin({
|
|
189
|
+
name: 'wms',
|
|
190
|
+
application: false,
|
|
191
|
+
exposes: {
|
|
192
|
+
'./Button': './src/exposes/button/index.tsx',
|
|
148
193
|
},
|
|
149
194
|
}),
|
|
150
195
|
mfGeneratorPlugin({
|
|
151
|
-
remotes:
|
|
152
|
-
|
|
153
|
-
aliasName:
|
|
154
|
-
remoteName:
|
|
155
|
-
entry:
|
|
196
|
+
remotes: [
|
|
197
|
+
{
|
|
198
|
+
aliasName: 'ama',
|
|
199
|
+
remoteName: 'ama',
|
|
200
|
+
entry: '/app-ama/remote.js',
|
|
156
201
|
},
|
|
157
|
-
|
|
202
|
+
],
|
|
158
203
|
}),
|
|
159
204
|
],
|
|
160
205
|
});
|
|
@@ -164,24 +209,119 @@ export default defineConfig({
|
|
|
164
209
|
|
|
165
210
|
### MicroFrontendPluginOptions
|
|
166
211
|
|
|
167
|
-
| 选项 | 类型 | 默认值
|
|
168
|
-
| --------------- | ------------------------------ |
|
|
169
|
-
| `appName` | `string` | -
|
|
170
|
-
| `
|
|
171
|
-
| `
|
|
172
|
-
| `
|
|
173
|
-
| `mfGenerator` | `MfGeneratorOptions \| false` | - | mf 生成器选项 |
|
|
174
|
-
| `htmlTransform` | `boolean` | `true` | 是否启用 HTML 转换 |
|
|
175
|
-
| `qiankun` | `object` | - | qiankun 配置 |
|
|
212
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
213
|
+
| --------------- | ------------------------------ | ------ | ---------------------------------------------- |
|
|
214
|
+
| `appName` | `string` | - | 能力文件中的应用名(必填,应与基座注册名一致) |
|
|
215
|
+
| `federation` | `FederationConfig` | - | Module Federation 配置 |
|
|
216
|
+
| `routes` | `RoutesPluginOptions \| false` | `{}` | 路由插件选项,设为 `false` 禁用 |
|
|
217
|
+
| `htmlTransform` | `boolean` | - | v2 弃用,组合插件使用官方 remote HMR |
|
|
176
218
|
|
|
177
219
|
### FederationConfig
|
|
178
220
|
|
|
179
|
-
| 选项
|
|
180
|
-
|
|
|
181
|
-
| `name`
|
|
182
|
-
| `filename`
|
|
183
|
-
| `
|
|
184
|
-
| `
|
|
221
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
222
|
+
| --------------- | -------------------------------------- | ---------------- | -------------------------------- |
|
|
223
|
+
| `name` | `string` | - | MF 名称,不允许包含连字符 |
|
|
224
|
+
| `filename` | `string` | `'remote.js'` | 远程入口文件名 |
|
|
225
|
+
| `manifest` | `boolean \| object` | `true` | 完整应用不可关闭 |
|
|
226
|
+
| `exposes` | `Record<string, string \| object>` | `{}` | 暴露的模块,key 必须以 `./` 开头 |
|
|
227
|
+
| `remotes` | `RemoteConfig[]` | `[]` | 远程模块配置 |
|
|
228
|
+
| `shared` | `ModuleFederationOptions['shared']` | - | 共享依赖配置 |
|
|
229
|
+
| `application` | `false \| FederationApplicationConfig` | `{}` | React Bridge 完整应用配置 |
|
|
230
|
+
| `shareStrategy` | `'loaded-first' \| 'version-first'` | `'loaded-first'` | 共享依赖选择策略 |
|
|
231
|
+
|
|
232
|
+
`application` 支持 `appName`、`rootComponent`、`entry`、`mountElementId`、
|
|
233
|
+
`capabilityFileName` 和 `buildVersion`。`rootComponent` 用于新的组件式 Bridge 模式;`entry`
|
|
234
|
+
用于兼容旧 lifecycle 入口,两者不能同时配置。两者都未配置时默认入口为 `src/main.tsx`。
|
|
235
|
+
基座应用名和挂载节点 id 默认使用
|
|
236
|
+
`microFrontendPlugins.appName`;当 MF 名使用下划线且 Vite base 明确采用对应的连字符名时,
|
|
237
|
+
插件会自动推断外部应用名,例如 `enterprise_wechat` + `/app-enterprise-wechat/` 会发布为
|
|
238
|
+
`enterprise-wechat`。也可以通过 `application.appName` 显式指定。
|
|
239
|
+
|
|
240
|
+
`bridge.enableBridgeRouter` 默认启用。启用时不要把 `react-router-dom` 配置为 shared;设为
|
|
241
|
+
`false` 后插件不会安装 Bridge Router alias,业务需要自行处理 basename 和路由协同。
|
|
242
|
+
|
|
243
|
+
开发态 remote entry 会自动加载当前微应用的 Vite HMR client,因此通过线上基座域名代理
|
|
244
|
+
本地微应用时不需要修改业务入口。代理规则必须覆盖微应用完整 base(例如 `/app-wms/`),
|
|
245
|
+
不能只代理 `remote.js` 或 manifest。HMR WebSocket 优先使用显式 `server.hmr` 配置,否则从
|
|
246
|
+
`server.origin` 推断;通过 HTTPS 线上基座代理本地服务时,应把 `server.origin` 配成本地 Vite
|
|
247
|
+
源(例如 `http://localhost:5176`),让 HMR client 直连本地 `ws://localhost:5176/app-wms/`。
|
|
248
|
+
如显式设置 `federation.dev = false` 或 `federation.dev.remoteHmr = false`,插件不会注入 HMR client。
|
|
249
|
+
`server.hmr = false` 同样禁用客户端注入。插件只补全 WebSocket 地址,Vite 自己负责客户端、
|
|
250
|
+
token、模块更新和 `full-reload`,不再使用旧 qiankun 的 client 文本替换或 reload 事件。
|
|
251
|
+
刷新监听由开发态虚拟模块承载,安装为 npm 依赖后也不会因 node_modules 预构建丢失 HMR 上下文。
|
|
252
|
+
|
|
253
|
+
如果生产基座把 CDN production React/ReactDOM 注册为 Module Federation singleton,开发态 remote
|
|
254
|
+
仍会复用该 singleton,避免 React、ReactDOM 与 antd 等依赖分别绑定到不同 React 实例。production
|
|
255
|
+
ReactDOM 不提供 Fast Refresh renderer,因此收到本地 HMR 更新后会自动刷新当前页面;基座提供
|
|
256
|
+
development renderer 时仍使用 React Fast Refresh。纯 CSS/Less 更新不会因此触发整页刷新;
|
|
257
|
+
production renderer 下的脚本更新会重新加载整个基座页面,组件状态会重置。
|
|
258
|
+
如果需要保留组件状态,基座也必须使用支持 Fast Refresh 的 development ReactDOM。
|
|
259
|
+
不要为了本地热更新强行更换已加载的 React singleton,否则共享组件可能出现 Invalid hook call。
|
|
260
|
+
|
|
261
|
+
### 线上基座 + 本地微应用:保留状态的 Fast Refresh
|
|
262
|
+
|
|
263
|
+
对于通过 CDN UMD 脚本加载 React 18 的基座,可以只在调试浏览器中代理 React 资源,
|
|
264
|
+
让基座和微应用从启动起共享同一套 development React/ReactDOM,无需重新发布基座。
|
|
265
|
+
这使用官方 `dev.remoteHmr: true` 的 React adapter 和 `@vitejs/plugin-react` Fast Refresh;
|
|
266
|
+
WebSocket 负责通知更新,React Refresh 负责替换组件,不需要每次整页 reload。
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
federation: {
|
|
270
|
+
name: 'your_app',
|
|
271
|
+
debugReactHost: true, // 默认关闭,仅 vite serve 生效
|
|
272
|
+
dev: { remoteHmr: true },
|
|
273
|
+
application: { rootComponent: 'src/app-root.tsx' },
|
|
274
|
+
shared: {
|
|
275
|
+
react: { singleton: true, requiredVersion: '^18.3.1' },
|
|
276
|
+
'react-dom': { singleton: true, requiredVersion: '^18.3.1' },
|
|
277
|
+
},
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
开启后,开发服务器在当前 Vite base 下提供两个经典脚本端点:
|
|
282
|
+
|
|
283
|
+
| 宿主请求 | 代理目标(`<dev-origin><base>` 后追加) |
|
|
284
|
+
| -------------------------- | --------------------------------------- |
|
|
285
|
+
| React 18 production UMD | `@micro-frontend/host-react.js` |
|
|
286
|
+
| ReactDOM 18 production UMD | `@micro-frontend/host-react-dom.js` |
|
|
287
|
+
|
|
288
|
+
资源来自消费者项目安装的 React/ReactDOM,必须与宿主版本一致。ReactDOM 端点在执行开发版
|
|
289
|
+
renderer 前安装官方 Refresh hook,并保存 renderer,供后续 Vite Refresh runtime 接管。
|
|
290
|
+
仅替换 production 文件名为 development、却在 ReactDOM 执行后才安装 hook,并不足以保证热更新。
|
|
291
|
+
|
|
292
|
+
两个代理规则和微应用资源代理均应开启;设置后首次手动刷新一次,让基座从启动时使用开发版
|
|
293
|
+
单例。后续组件修改由 Fast Refresh 更新,符合刷新边界的函数组件可保留状态。修改 Hook 结构、
|
|
294
|
+
组件导出契约或无法接受 HMR 的依赖时,仍可能按 Vite/React 官方规则重挂载或整页刷新。
|
|
295
|
+
关闭代理后刷新即可恢复生产版运行时。不要在页面运行中替换 `window.React`/`window.ReactDOM`。
|
|
296
|
+
|
|
297
|
+
`debugReactHost` 当前面向 React 18 UMD 宿主,不适用于 React 19 或将 React 打包进宿主 JS 的项目;
|
|
298
|
+
这类宿主需要提供自己的开发调试启动入口,确保 Refresh hook、development ReactDOM 和共享依赖
|
|
299
|
+
在应用启动前完成初始化。
|
|
300
|
+
|
|
301
|
+
本地调试这个 workspace 插件时,修改插件源码后先在插件目录执行 `pnpm build`,再重启微应用
|
|
302
|
+
Vite 服务。Vite 配置已经执行过的插件对象不会因为 `dist/index.js` 重建而自动换成新实现。
|
|
303
|
+
|
|
304
|
+
同一个库不能同时配置在 `shared` 和 Vite/Rollup `externals` 中。React 18 微应用应移除
|
|
305
|
+
React/ReactDOM 的 externals,并将 `react`、`react-dom` 配置为 singleton;如果联邦组件依赖
|
|
306
|
+
宿主的 antd 主题和上下文,也应将同版本 `antd` 配置为 singleton,并由宿主 Runtime 注册对应
|
|
307
|
+
shared provider。antd 样式仍需由宿主全局样式或远程 CSS 注入链路提供。
|
|
308
|
+
|
|
309
|
+
当 `shared` 包含 `react` 且没有显式配置 `react/jsx-dev-runtime` 时,插件会自动为
|
|
310
|
+
`react/jsx-dev-runtime` 使用应用级独立 share scope。React 生产构建的该入口不提供可调用的
|
|
311
|
+
`jsxDEV`,因此本地开发 Remote 不能复用生产 Host 的同名 provider;这不会影响 `react`、
|
|
312
|
+
`react-dom`、`react/jsx-runtime` 和 `antd` 的 singleton 共享。显式配置该子路径可覆盖此默认行为。
|
|
313
|
+
|
|
314
|
+
### 远程组件动态加载
|
|
315
|
+
|
|
316
|
+
生成的 `mf.tsx` 使用 `runtime/federation` 的 `createRemoteLoader`,按 `federation.name`
|
|
317
|
+
绑定消费应用自己的 Runtime,并在首次动态加载时通过官方 `registerRemotes` 注册配置。
|
|
318
|
+
因此即使微应用以 remote 身份被基座加载、采用 `loaded-first`,且业务中没有静态 remote import,
|
|
319
|
+
`safeRemoteComponent` 也能定位配置中的远程组件。各应用的 remotes 注册互相独立,不借用基座实例。
|
|
320
|
+
相对 entry 默认相对于浏览器页面的 origin 解析,显式 `devHost`、alias、ESM/UMD 类型和
|
|
321
|
+
`shareScope` 会同步保留。已经在同一实例中注册的 remote 不会被强制覆盖。
|
|
322
|
+
|
|
323
|
+
直接调用 `generateMf` 时可传入 `hostName`、`devHost`、`shareScope`;省略 `hostName` 时兼容
|
|
324
|
+
官方默认实例。组合插件和 `mf generate` CLI 会自动从 Federation 配置传入这些字段。
|
|
185
325
|
|
|
186
326
|
### RoutesPluginOptions
|
|
187
327
|
|
|
@@ -201,22 +341,41 @@ export default defineConfig({
|
|
|
201
341
|
| `intlPath` | `string` | `'@/utils/intl'` | 国际化工具路径 |
|
|
202
342
|
| `pageAliasPrefix` | `string` | `'@/pages'` | 页面别名前缀 |
|
|
203
343
|
|
|
204
|
-
>
|
|
344
|
+
> 新代码请从 `runtime/bridge` 引入 `registerMicroFrontend`。旧的 `runtime` 和
|
|
345
|
+
> `runtime/qiankun` 路径仍可用于迁移,但二者都指向 Bridge 实现,不再依赖 qiankun。
|
|
205
346
|
|
|
206
347
|
### Runtime 子路径
|
|
207
348
|
|
|
208
|
-
| 子路径
|
|
209
|
-
|
|
|
210
|
-
| `runtime/dynamic`
|
|
211
|
-
| `runtime/
|
|
212
|
-
| `runtime` |
|
|
349
|
+
| 子路径 | 用途 | 依赖 |
|
|
350
|
+
| -------------------- | ----------------------------------------- | --------------------------------- |
|
|
351
|
+
| `runtime/dynamic` | 路由 / 业务组件 lazy load | `react` |
|
|
352
|
+
| `runtime/bridge` | 完整应用生命周期注册与 Bridge provider | `@module-federation/bridge-react` |
|
|
353
|
+
| `runtime/federation` | 实例绑定的远程加载器及兼容 Runtime 转发入口 | `@module-federation/runtime` |
|
|
354
|
+
| `runtime/qiankun` | 旧 import 路径兼容,指向 `runtime/bridge` | 无 qiankun 依赖 |
|
|
355
|
+
| `runtime` | 兼容聚合入口 | Bridge 与 dynamic re-export |
|
|
213
356
|
|
|
214
357
|
```typescript
|
|
215
358
|
// routes.ts / 业务组件
|
|
216
|
-
import { dynamic } from
|
|
359
|
+
import { dynamic } from '@lemon-fe/vite-plugin-micro-frontend/runtime/dynamic';
|
|
360
|
+
|
|
361
|
+
// 兼容旧 lifecycle 入口的 main.tsx
|
|
362
|
+
import ReactDOM from 'react-dom';
|
|
363
|
+
import { registerMicroFrontend } from '@lemon-fe/vite-plugin-micro-frontend/runtime/bridge';
|
|
217
364
|
|
|
218
|
-
|
|
219
|
-
|
|
365
|
+
registerMicroFrontend({
|
|
366
|
+
async renderContainer(props) {
|
|
367
|
+
const mountNode = props.container?.querySelector<HTMLElement>('#app');
|
|
368
|
+
if (!mountNode) return;
|
|
369
|
+
|
|
370
|
+
ReactDOM.render(<App />, mountNode);
|
|
371
|
+
},
|
|
372
|
+
async unmount(props) {
|
|
373
|
+
const mountNode = props.container?.querySelector<HTMLElement>('#app');
|
|
374
|
+
if (!mountNode) return;
|
|
375
|
+
|
|
376
|
+
ReactDOM.unmountComponentAtNode(mountNode);
|
|
377
|
+
},
|
|
378
|
+
});
|
|
220
379
|
```
|
|
221
380
|
|
|
222
381
|
## 路由约定
|
|
@@ -239,13 +398,13 @@ const MyPage = () => {
|
|
|
239
398
|
};
|
|
240
399
|
|
|
241
400
|
// 页面标题
|
|
242
|
-
MyPage.title =
|
|
401
|
+
MyPage.title = '我的页面';
|
|
243
402
|
|
|
244
403
|
// 是否开启 KeepAlive
|
|
245
404
|
MyPage.keepAlive = true;
|
|
246
405
|
|
|
247
406
|
// 权限配置
|
|
248
|
-
MyPage.authority = [
|
|
407
|
+
MyPage.authority = ['admin', 'user'];
|
|
249
408
|
|
|
250
409
|
export default MyPage;
|
|
251
410
|
```
|
package/dist/cli.js
CHANGED
|
@@ -3,34 +3,37 @@ import * as path from 'path';
|
|
|
3
3
|
import { MICRO_FRONTEND_OPTIONS_KEY, generateMf, generateRoutesToFile } from './index.js';
|
|
4
4
|
|
|
5
5
|
const DEFAULT_ROUTES = {
|
|
6
|
-
pagesDir: path.resolve(process.cwd(),
|
|
7
|
-
outputPath: path.resolve(process.cwd(),
|
|
6
|
+
pagesDir: path.resolve(process.cwd(), 'src/pages'),
|
|
7
|
+
outputPath: path.resolve(process.cwd(), 'src/routes.ts'),
|
|
8
8
|
};
|
|
9
|
-
const DEFAULT_MF_OUTPUT = path.resolve(process.cwd(),
|
|
9
|
+
const DEFAULT_MF_OUTPUT = path.resolve(process.cwd(), 'src/utils/mf.tsx');
|
|
10
10
|
/** 扁平化 Vite plugins 数组(可能嵌套) */
|
|
11
11
|
function flattenPlugins(plugins) {
|
|
12
12
|
if (!Array.isArray(plugins))
|
|
13
13
|
return [];
|
|
14
|
-
return plugins.flatMap(
|
|
14
|
+
return plugins.flatMap(p => (Array.isArray(p) ? flattenPlugins(p) : [p]));
|
|
15
15
|
}
|
|
16
16
|
/** 从 vite.config 中解析 microFrontendPlugins 的 options,供 CLI 复用 federation/routes */
|
|
17
17
|
async function loadConfigFromVite(cwd) {
|
|
18
18
|
try {
|
|
19
19
|
const vite = await import('vite');
|
|
20
|
-
const result = await vite.loadConfigFromFile({ command:
|
|
20
|
+
const result = await vite.loadConfigFromFile({ command: 'build', mode: 'production' }, undefined, cwd);
|
|
21
21
|
if (!result?.config?.plugins)
|
|
22
22
|
return null;
|
|
23
23
|
const flat = flattenPlugins(result.config.plugins);
|
|
24
24
|
for (const plugin of flat) {
|
|
25
25
|
const raw = plugin &&
|
|
26
|
-
typeof plugin ===
|
|
26
|
+
typeof plugin === 'object' &&
|
|
27
27
|
plugin[MICRO_FRONTEND_OPTIONS_KEY];
|
|
28
28
|
const opts = raw;
|
|
29
|
-
if (opts && typeof opts ===
|
|
29
|
+
if (opts && typeof opts === 'object') {
|
|
30
30
|
return {
|
|
31
31
|
routes: opts.routes,
|
|
32
32
|
federation: opts.federation
|
|
33
33
|
? {
|
|
34
|
+
name: opts.federation.name,
|
|
35
|
+
devHost: opts.federation.devHost,
|
|
36
|
+
shareScope: opts.federation.shareScope,
|
|
34
37
|
remotes: opts.federation.remotes,
|
|
35
38
|
outputPath: opts.federation.outputPath,
|
|
36
39
|
}
|
|
@@ -56,6 +59,9 @@ async function runGenerate(config) {
|
|
|
56
59
|
let done = 0;
|
|
57
60
|
if (config.federation !== undefined) {
|
|
58
61
|
generateMf({
|
|
62
|
+
hostName: config.federation.name,
|
|
63
|
+
devHost: config.federation.devHost,
|
|
64
|
+
shareScope: config.federation.shareScope,
|
|
59
65
|
remotes: config.federation.remotes ?? [],
|
|
60
66
|
outputPath: config.federation.outputPath ?? DEFAULT_MF_OUTPUT,
|
|
61
67
|
});
|
|
@@ -69,32 +75,32 @@ async function runGenerate(config) {
|
|
|
69
75
|
});
|
|
70
76
|
done++;
|
|
71
77
|
if (done === 0) {
|
|
72
|
-
console.log(
|
|
78
|
+
console.log('未执行任何生成(未配置 federation,routes 已按默认路径生成)');
|
|
73
79
|
}
|
|
74
80
|
}
|
|
75
81
|
async function main() {
|
|
76
82
|
const args = process.argv.slice(2);
|
|
77
83
|
const cwd = process.cwd();
|
|
78
|
-
if (args[0] ===
|
|
84
|
+
if (args[0] === 'generate') {
|
|
79
85
|
const config = await loadConfigFromVite(cwd);
|
|
80
86
|
if (config) {
|
|
81
87
|
await runGenerate(config);
|
|
82
88
|
return;
|
|
83
89
|
}
|
|
84
|
-
console.warn(
|
|
85
|
-
|
|
90
|
+
console.warn('未在 vite.config 中找到 microFrontendPlugins 配置,已使用默认路径生成 routes;' +
|
|
91
|
+
'若要生成 mf,请在 vite.config 中配置 microFrontendPlugins({ federation: { remotes } }).');
|
|
86
92
|
await runGenerate({ routes: DEFAULT_ROUTES });
|
|
87
93
|
return;
|
|
88
94
|
}
|
|
89
|
-
if (args[0] ===
|
|
95
|
+
if (args[0] === '--help' || args[0] === '-h' || !args.length) {
|
|
90
96
|
printUsage();
|
|
91
97
|
return;
|
|
92
98
|
}
|
|
93
|
-
console.error(
|
|
99
|
+
console.error('未知命令:', args[0]);
|
|
94
100
|
printUsage();
|
|
95
101
|
process.exit(1);
|
|
96
102
|
}
|
|
97
|
-
main().catch(
|
|
103
|
+
main().catch(e => {
|
|
98
104
|
console.error(e);
|
|
99
105
|
process.exit(1);
|
|
100
106
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Plugin, PluginOption } from 'vite';
|
|
2
2
|
export { default as cssInjectedByJsPlugin } from 'vite-plugin-css-injected-by-js';
|
|
3
|
-
import { ModuleFederationOptions } from '@module-federation/vite
|
|
3
|
+
import { ModuleFederationOptions } from '@module-federation/vite';
|
|
4
4
|
|
|
5
5
|
interface DevHmrPluginOptions {
|
|
6
6
|
/** 入口文件路径,相对于项目根目录,默认为 src/main.tsx */
|
|
@@ -8,9 +8,9 @@ interface DevHmrPluginOptions {
|
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* 微前端开发环境 HMR 插件(合并版)
|
|
11
|
-
* 1. HTML
|
|
12
|
-
* 2. 入口注入:在入口最顶部自动注入 react-refresh-init
|
|
13
|
-
*
|
|
11
|
+
* 1. HTML 转换:移除默认 refresh preamble,由远程应用自己的 origin 初始化
|
|
12
|
+
* 2. 入口注入:在入口最顶部自动注入 react-refresh-init,接管远程模块的 HMR 监听
|
|
13
|
+
* 仅开发环境生效。v2 组合插件默认使用官方 remoteHmr;如需旧协议,必须单独注册此插件。
|
|
14
14
|
*/
|
|
15
15
|
declare function devHmrPlugin(options?: DevHmrPluginOptions): Plugin;
|
|
16
16
|
|
|
@@ -33,11 +33,14 @@ declare function generateMf(options: MfGeneratorOptions): void;
|
|
|
33
33
|
*/
|
|
34
34
|
declare function mfGeneratorPlugin(options: MfGeneratorOptions): VitePlugin;
|
|
35
35
|
|
|
36
|
+
interface FederationPluginContext {
|
|
37
|
+
appName?: string;
|
|
38
|
+
}
|
|
36
39
|
/**
|
|
37
40
|
* 模块联邦插件
|
|
38
41
|
* 整合了 federation 配置、exposes 自动扫描、remotes URL 构建等功能
|
|
39
42
|
*/
|
|
40
|
-
declare function federationPlugin(federationOptions: FederationConfig): PluginOption[];
|
|
43
|
+
declare function federationPlugin(federationOptions: FederationConfig, context?: FederationPluginContext): PluginOption[];
|
|
41
44
|
|
|
42
45
|
/**
|
|
43
46
|
* 生产构建动态 publicPath(类似 webpack `output.publicPath`)。
|
|
@@ -96,10 +99,28 @@ interface RemoteConfig {
|
|
|
96
99
|
/**
|
|
97
100
|
* 模块联邦配置
|
|
98
101
|
*/
|
|
99
|
-
interface
|
|
102
|
+
interface FederationApplicationConfig {
|
|
103
|
+
/** 基座注册的应用名;默认从 microFrontendPlugins.appName 与 Vite base 推断 */
|
|
104
|
+
appName?: string;
|
|
105
|
+
/**
|
|
106
|
+
* React 根组件入口。配置后直接生成官方 Bridge provider,独立启动入口不应包含在该模块中。
|
|
107
|
+
*/
|
|
108
|
+
rootComponent?: string;
|
|
109
|
+
/**
|
|
110
|
+
* 兼容旧项目的生命周期入口,默认 src/main.tsx;不能与 rootComponent 同时配置。
|
|
111
|
+
*/
|
|
112
|
+
entry?: string;
|
|
113
|
+
/** 应用内部的挂载节点 id,默认使用 microFrontendPlugins 的 appName */
|
|
114
|
+
mountElementId?: string;
|
|
115
|
+
/** 能力文件名,默认为 micro-frontend.json */
|
|
116
|
+
capabilityFileName?: string;
|
|
117
|
+
/** 随能力文件发布的业务构建版本,可用于诊断和缓存追踪 */
|
|
118
|
+
buildVersion?: string;
|
|
119
|
+
}
|
|
120
|
+
interface FederationConfig extends Omit<ModuleFederationOptions, 'remotes' | 'exposes' | 'shared'> {
|
|
100
121
|
/** 暴露的模块 */
|
|
101
|
-
shared?:
|
|
102
|
-
exposes?:
|
|
122
|
+
shared?: ModuleFederationOptions['shared'];
|
|
123
|
+
exposes?: NonNullable<ModuleFederationOptions['exposes']>;
|
|
103
124
|
/**
|
|
104
125
|
* exposes 目录路径,默认为 src/exposes
|
|
105
126
|
* 插件会自动扫描该目录下的子目录作为暴露的模块
|
|
@@ -116,6 +137,21 @@ interface FederationConfig extends Omit<ModuleFederationOptions, "remotes"> {
|
|
|
116
137
|
* 如果不设置,将使用 remotes 中的 entry 作为完整 URL
|
|
117
138
|
*/
|
|
118
139
|
devHost?: string;
|
|
140
|
+
/**
|
|
141
|
+
* 开发态提供宿主 React 18 UMD 调试资源。通过浏览器代理将宿主 React/ReactDOM CDN
|
|
142
|
+
* 请求替换为本地 development 构建并提前安装 Refresh hook,保持共享单例支持 Fast Refresh。
|
|
143
|
+
* 默认 false;不改变生产构建,也不会自动替换已经加载的 React。
|
|
144
|
+
*/
|
|
145
|
+
debugReactHost?: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* 完整微应用的 React Bridge 配置。默认启用;设为 false 时只构建普通联邦模块,
|
|
148
|
+
* 不生成 ./export-app 和 micro-frontend.json。
|
|
149
|
+
*/
|
|
150
|
+
application?: false | FederationApplicationConfig;
|
|
151
|
+
/** 官方 React Bridge Router 配置;默认启用并自动识别 React Router v5/v6/v7 */
|
|
152
|
+
bridge?: {
|
|
153
|
+
enableBridgeRouter?: boolean;
|
|
154
|
+
};
|
|
119
155
|
}
|
|
120
156
|
/**
|
|
121
157
|
* 路由配置
|
|
@@ -172,6 +208,12 @@ interface RoutesTemplateOptions {
|
|
|
172
208
|
* mf 生成器选项
|
|
173
209
|
*/
|
|
174
210
|
interface MfGeneratorOptions {
|
|
211
|
+
/** 消费侧 Federation 实例名;组合插件自动传入 federation.name。省略时兼容默认实例。 */
|
|
212
|
+
hostName?: string;
|
|
213
|
+
/** 相对 remote entry 的源地址,与 federation.devHost 一致。 */
|
|
214
|
+
devHost?: string;
|
|
215
|
+
/** 与构建期 remote 注册保持相同的共享作用域。 */
|
|
216
|
+
shareScope?: string | string[];
|
|
175
217
|
/** 远程模块配置 */
|
|
176
218
|
remotes: RemoteConfig[];
|
|
177
219
|
/** 生成的文件路径,默认为 src/utils/mf.tsx */
|
|
@@ -189,15 +231,8 @@ interface MicroFrontendPluginOptions {
|
|
|
189
231
|
federation?: FederationConfig;
|
|
190
232
|
/** 路由插件选项 */
|
|
191
233
|
routes?: RoutesPluginOptions;
|
|
192
|
-
/**
|
|
234
|
+
/** @deprecated v2 默认使用 @module-federation/vite 的 remoteHmr,此选项不再参与组合插件。 */
|
|
193
235
|
htmlTransform?: boolean | DevHmrPluginOptions;
|
|
194
|
-
/** qiankun 配置 */
|
|
195
|
-
qiankun?: {
|
|
196
|
-
/** 是否启用 qiankun */
|
|
197
|
-
enabled?: boolean;
|
|
198
|
-
/** qiankun 应用名称 */
|
|
199
|
-
name?: string;
|
|
200
|
-
};
|
|
201
236
|
/** 生产构建启用动态资源路径;未配置则不注册 */
|
|
202
237
|
dynamicBase?: DynamicBaseConfig | boolean;
|
|
203
238
|
}
|
|
@@ -253,4 +288,4 @@ declare const MICRO_FRONTEND_OPTIONS_KEY = "__microFrontendOptions";
|
|
|
253
288
|
declare function microFrontendPlugins(options: MicroFrontendPluginOptions): PluginOption[];
|
|
254
289
|
|
|
255
290
|
export { MICRO_FRONTEND_OPTIONS_KEY, autoExternalAntd, devHmrPlugin, federationPlugin, generateMf, generateRoutesToFile, getDynamicBasePlugins, getRoutes, mfGeneratorPlugin, microFrontendPlugins, pagesRoutesPlugin };
|
|
256
|
-
export type { ComponentMetadata, DynamicBaseConfig, FederationConfig, IRoute, MfGeneratorOptions, MicroFrontendPluginOptions, RemoteConfig, RouteConfig, RoutesPluginOptions, RoutesTemplateOptions, VitePlugin };
|
|
291
|
+
export type { ComponentMetadata, DynamicBaseConfig, FederationApplicationConfig, FederationConfig, IRoute, MfGeneratorOptions, MicroFrontendPluginOptions, RemoteConfig, RouteConfig, RoutesPluginOptions, RoutesTemplateOptions, VitePlugin };
|