@lemon-fe/vite-plugin-micro-frontend 2.0.2 → 2.0.4
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 +42 -26
- package/dist/index.d.ts +22 -15
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/runtime-federation.d.ts +6 -20
- package/dist/runtime-federation.js +30 -30
- package/dist/runtime-federation.js.map +1 -1
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -261,8 +261,9 @@ ReactDOM 不提供 Fast Refresh renderer,单独配置 WebSocket 不能让 Reac
|
|
|
261
261
|
|
|
262
262
|
### 微应用本地入口加载线上基座
|
|
263
263
|
|
|
264
|
-
|
|
265
|
-
|
|
264
|
+
配套基座须通过 `mf-manifest.json` 暴露 `./app`,并提供 `bootstrap({ localApplications })`。
|
|
265
|
+
微应用的本地 HTML 提供 `#root`,并在启动前按基座首页的顺序加载配置和所需的全局脚本。
|
|
266
|
+
基座负责渲染,微应用复用同一个 React 树。
|
|
266
267
|
`developmentShell` 仅在 Vite serve 时设置 `base: '/'` 并安装资源代理,生产 remote 构建保持原样。
|
|
267
268
|
|
|
268
269
|
```ts
|
|
@@ -270,7 +271,6 @@ microFrontendPlugins({
|
|
|
270
271
|
appName: 'your-app',
|
|
271
272
|
developmentShell: {
|
|
272
273
|
baseOrigin: 'https://your-base.example.com',
|
|
273
|
-
// gatewayOrigin: 'https://your-gateway.example.com',
|
|
274
274
|
},
|
|
275
275
|
federation: {
|
|
276
276
|
debugReactHost: true,
|
|
@@ -284,31 +284,27 @@ microFrontendPlugins({
|
|
|
284
284
|
});
|
|
285
285
|
```
|
|
286
286
|
|
|
287
|
-
HTML
|
|
288
|
-
|
|
287
|
+
HTML 可从 `/__earth_base__/config/index.js` 加载基座配置。开启 `debugReactHost` 后,
|
|
288
|
+
按顺序加载 `/@micro-frontend/host-react.js` 和 `/@micro-frontend/host-react-dom.js`,
|
|
289
|
+
再加载 antd 等依赖。最后用 `<script type="module" vite-ignore src="/src/dev.ts"></script>`
|
|
290
|
+
引用本地启动模块。启动模块使用插件的 Runtime 入口,微应用无需单独安装 `@module-federation/runtime`:
|
|
289
291
|
|
|
290
292
|
```ts
|
|
291
|
-
import {
|
|
293
|
+
import { startDevelopmentApp } from '@lemon-fe/vite-plugin-micro-frontend/runtime/development';
|
|
292
294
|
|
|
293
295
|
if (import.meta.env.DEV) {
|
|
294
|
-
|
|
295
|
-
const { BaseRoot, props, createRenderer, createElement } = await loadDevelopmentRoot({
|
|
296
|
-
appName: 'your-app',
|
|
297
|
-
});
|
|
298
|
-
createRenderer(container).render(createElement(BaseRoot, props));
|
|
296
|
+
void startDevelopmentApp({ appName: 'your-app' });
|
|
299
297
|
}
|
|
300
298
|
```
|
|
301
299
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
和 `./app` 模块名。基座需要显示该应用时,用现有加载器提供共享依赖并加载本地组件。
|
|
306
|
-
请先部署提供 `root/loadBaseRoot` 的配套基座。
|
|
300
|
+
`startDevelopmentApp` 通过代理的 `/__earth_base__/mf-manifest.json` 加载 `base/app`,再把本地 `remote.js` 地址
|
|
301
|
+
和 `./app` 模块名传给基座。若显式配置了不同的联邦名称,可传入 `federationName`。
|
|
302
|
+
基座需要显示该应用时,用现有加载器提供共享依赖并加载本地组件。
|
|
307
303
|
|
|
308
304
|
本地页面使用 `/<appName>/...` 业务路由,深层路径刷新由 Vite HTML fallback 处理。
|
|
309
305
|
`/__earth_base__/` 代理基座配置、remote 和 chunks,`/assets/` 代理基座绝对静态资源,
|
|
310
306
|
`/app-*/` 代理其他微应用;本地能力文件只在根路径和本应用的 Vite `base` 下提供。
|
|
311
|
-
|
|
307
|
+
登录和业务请求沿用基座配置的网关;插件只需配置 `baseOrigin`。
|
|
312
308
|
localhost 与线上存储隔离,在本地基座登录;线上调试 URL 开关已不再使用。
|
|
313
309
|
|
|
314
310
|
### 基座启动前加载 React 开发运行时
|
|
@@ -400,14 +396,22 @@ const buttonModule = await loadRemote('catalog/Button');
|
|
|
400
396
|
入口中的 eager glob 显式使用 `ComponentType<MicroFrontendProps>`,Vite 将其编译为静态导入;
|
|
401
397
|
TypeScript 只读取公开组件类型,生成 `./app` 的声明不会沿根组件遍历整个应用的路由和页面。
|
|
402
398
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
399
|
+
构建时,官方 DTS 先从 exposes 入口编译其导入的依赖,封装内部通过官方
|
|
400
|
+
`dts.generateTypes.afterGenerate` 钩子为每个 expose 合并公开声明,无需另配 Vite 后处理插件。
|
|
401
|
+
最终 ZIP 中每个 expose 对应一个 `.d.ts`,保留默认导出、命名导出、导出的类型和必要的
|
|
402
|
+
Props 类型,移除仅用于实现的页面、services、hooks 等中间声明。内部 `@/` 等别名按
|
|
403
|
+
`tsconfig` 的 `paths` 解析并合并,消费方无需配置生产项目的源码别名;React 等第三方
|
|
404
|
+
依赖仍使用包名引用。别名需同时配置到 TypeScript;仅配置 Vite 的 resolve.alias 不够。
|
|
405
|
+
合并时优先读取官方生成的 `.d.ts`,并补入公开类型引用的手写声明;不会沿类型文件重新编译
|
|
406
|
+
业务 TSX。声明中遗留的纯样式副作用导入在类型合并时移除,运行时样式仍由 Vite 正常构建。
|
|
407
|
+
用户显式提取的第三方和远程依赖仍随类型包保留。
|
|
408
|
+
|
|
409
|
+
业务项目仍只配置官方 `dts` 选项。已有 `afterGenerate` 回调会在公开声明合并完成后执行,
|
|
410
|
+
接收官方原始的 `zipTypesPath`、`apiTypesPath`、`zipName` 和 `apiFileName`。
|
|
411
|
+
`adm-zip` 用于读写官方已生成的 ZIP,官方 DTS 本身也使用该库;`rollup-plugin-dts`
|
|
412
|
+
负责合并公开声明与解析源码别名。这些依赖由封装插件提供,业务项目无需单独安装。
|
|
413
|
+
|
|
414
|
+
`compiled-types/` 仅用于中间编译,不随收敛后的类型包发布;最终发布的是 `dist/@mf-types.zip` 和
|
|
411
415
|
`dist/@mf-types.d.ts`。官方编译失败也可能留下中间目录,所以看到目录不等于类型包生成成功。
|
|
412
416
|
|
|
413
417
|
生产方需随 JS/CSS 一起发布 `@mf-types.zip` 和 `@mf-types.d.ts`;其他应用可以使用官方插件,
|
|
@@ -433,7 +437,19 @@ exposes 决定。只修改 tsconfig 的 include/exclude 不能排除被入口 im
|
|
|
433
437
|
插件不自动改写消费项目的 tsconfig。`@mf-types/`、`.mf/` 和 `src/.external/` 是生成内容,
|
|
434
438
|
应加入消费项目的 `.gitignore`。
|
|
435
439
|
|
|
436
|
-
|
|
440
|
+
默认 `dts: true` 即可生成收敛后的公开类型;需要指定声明编译配置时使用:
|
|
441
|
+
|
|
442
|
+
```ts
|
|
443
|
+
federation: {
|
|
444
|
+
dts: {
|
|
445
|
+
generateTypes: {
|
|
446
|
+
tsConfigPath: './tsconfig.dts.json',
|
|
447
|
+
},
|
|
448
|
+
},
|
|
449
|
+
}
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
仅消费远程模块时,可分别控制生成和下载:
|
|
437
453
|
|
|
438
454
|
```ts
|
|
439
455
|
federation: {
|
package/dist/index.d.ts
CHANGED
|
@@ -42,14 +42,6 @@ type DynamicBaseConfig = {
|
|
|
42
42
|
removeIndexCssPreload?: boolean;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
interface DevelopmentShellConfig {
|
|
46
|
-
/** 已部署支持 base/root 的基座地址。 */
|
|
47
|
-
baseOrigin: string;
|
|
48
|
-
/** 默认使用基座配置;需要覆盖时显式指定浏览器直连的网关。 */
|
|
49
|
-
gatewayOrigin?: string;
|
|
50
|
-
remoteName?: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
45
|
/**
|
|
54
46
|
* 远程模块配置
|
|
55
47
|
*/
|
|
@@ -86,13 +78,26 @@ interface FederationApplicationConfig {
|
|
|
86
78
|
/** 随能力文件发布的业务构建版本,可用于诊断和缓存追踪 */
|
|
87
79
|
buildVersion?: string;
|
|
88
80
|
}
|
|
89
|
-
|
|
81
|
+
/** 官方 afterGenerate 回调接收的类型产物信息。 */
|
|
82
|
+
interface FederationGeneratedTypes {
|
|
83
|
+
zipTypesPath: string;
|
|
84
|
+
apiTypesPath: string;
|
|
85
|
+
zipName: string;
|
|
86
|
+
apiFileName: string;
|
|
87
|
+
}
|
|
88
|
+
type OfficialDtsOptions = Exclude<ModuleFederationOptions['dts'], boolean | undefined>;
|
|
89
|
+
/** 补齐当前 Vite 配置类型尚未声明的官方 afterGenerate 钩子。 */
|
|
90
|
+
interface FederationDtsOptions extends Omit<OfficialDtsOptions, 'generateTypes'> {
|
|
91
|
+
generateTypes?: boolean | (Exclude<OfficialDtsOptions['generateTypes'], boolean | undefined> & {
|
|
92
|
+
afterGenerate?: (options: FederationGeneratedTypes) => Promise<void> | void;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
interface FederationConfig extends Omit<ModuleFederationOptions, 'name' | 'remotes' | 'exposes' | 'dts'> {
|
|
90
96
|
/** 默认使用组合插件的 appName(连字符转下划线);单独调用 federationPlugin 时必填。 */
|
|
91
97
|
name?: string;
|
|
92
98
|
/** 官方类型生成与消费配置,默认 true;可分别控制 generateTypes / consumeTypes。 */
|
|
93
|
-
dts?:
|
|
99
|
+
dts?: boolean | FederationDtsOptions;
|
|
94
100
|
/** 暴露的模块 */
|
|
95
|
-
shared?: ModuleFederationOptions['shared'];
|
|
96
101
|
exposes?: NonNullable<ModuleFederationOptions['exposes']>;
|
|
97
102
|
/**
|
|
98
103
|
* exposes 目录路径,默认为 src/exposes
|
|
@@ -100,6 +105,8 @@ interface FederationConfig extends Omit<ModuleFederationOptions, 'name' | 'remot
|
|
|
100
105
|
* 例如:src/exposes/Button -> { './Button': './src/exposes/Button/index.tsx' }
|
|
101
106
|
*/
|
|
102
107
|
exposesDir?: string;
|
|
108
|
+
/** 仅在 Vite 开发服务器中禁用 exposesDir 的自动暴露,默认 false。 */
|
|
109
|
+
disableAutoExposesInDev?: boolean;
|
|
103
110
|
/** 远程模块配置 */
|
|
104
111
|
remotes?: RemoteConfig[];
|
|
105
112
|
/** 生成的文件路径,默认为 src/utils/mf.tsx */
|
|
@@ -194,8 +201,8 @@ interface MfGeneratorOptions {
|
|
|
194
201
|
interface MicroFrontendPluginOptions {
|
|
195
202
|
/** 应用名称 */
|
|
196
203
|
appName: string;
|
|
197
|
-
/**
|
|
198
|
-
|
|
204
|
+
/** 已部署基座的 HTTP(S) origin,仅本地开发生效。省略时不启动基座。 */
|
|
205
|
+
baseOrigin?: string;
|
|
199
206
|
/** @deprecated 未参与插件行为;开发模式由 Vite command 决定。 */
|
|
200
207
|
useDevMode?: boolean;
|
|
201
208
|
/** 模块联邦配置 */
|
|
@@ -236,7 +243,7 @@ interface FederationPluginContext {
|
|
|
236
243
|
* 模块联邦插件
|
|
237
244
|
* 整合了 federation 配置、exposes 自动扫描、remotes URL 构建等功能
|
|
238
245
|
*/
|
|
239
|
-
declare function federationPlugin(
|
|
246
|
+
declare function federationPlugin({ disableAutoExposesInDev, ...options }: FederationConfig, context?: FederationPluginContext): PluginOption[];
|
|
240
247
|
|
|
241
248
|
/**
|
|
242
249
|
* 生产构建专用:动态 publicPath(vite-plugin-dynamic-base + 可选 HTML 兜底)。
|
|
@@ -296,4 +303,4 @@ declare const MICRO_FRONTEND_OPTIONS_KEY = "__microFrontendOptions";
|
|
|
296
303
|
declare function microFrontendPlugins(options: MicroFrontendPluginOptions): PluginOption[];
|
|
297
304
|
|
|
298
305
|
export { MICRO_FRONTEND_OPTIONS_KEY, devHmrPlugin, federationPlugin, generateMf, generateRoutesToFile, getDynamicBasePlugins, getRoutes, mfGeneratorPlugin, microFrontendPlugins, pagesRoutesPlugin };
|
|
299
|
-
export type { ComponentMetadata,
|
|
306
|
+
export type { ComponentMetadata, DynamicBaseConfig, FederationApplicationConfig, FederationConfig, FederationDtsOptions, FederationGeneratedTypes, IRoute, MfGeneratorOptions, MicroFrontendPluginOptions, RemoteConfig, RouteConfig, RoutesPluginOptions, RoutesTemplateOptions, VitePlugin };
|