@lemon-fe/vite-plugin-micro-frontend 2.0.1 → 2.0.3
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 +19 -7
- 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 +25 -23
- 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
|
@@ -43,10 +43,8 @@ type DynamicBaseConfig = {
|
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
interface DevelopmentShellConfig {
|
|
46
|
-
/** 已部署支持 base/
|
|
46
|
+
/** 已部署支持 base/app 的基座地址。 */
|
|
47
47
|
baseOrigin: string;
|
|
48
|
-
/** 默认使用基座配置;需要覆盖时显式指定浏览器直连的网关。 */
|
|
49
|
-
gatewayOrigin?: string;
|
|
50
48
|
remoteName?: string;
|
|
51
49
|
}
|
|
52
50
|
|
|
@@ -86,11 +84,25 @@ interface FederationApplicationConfig {
|
|
|
86
84
|
/** 随能力文件发布的业务构建版本,可用于诊断和缓存追踪 */
|
|
87
85
|
buildVersion?: string;
|
|
88
86
|
}
|
|
89
|
-
|
|
87
|
+
/** 官方 afterGenerate 回调接收的类型产物信息。 */
|
|
88
|
+
interface FederationGeneratedTypes {
|
|
89
|
+
zipTypesPath: string;
|
|
90
|
+
apiTypesPath: string;
|
|
91
|
+
zipName: string;
|
|
92
|
+
apiFileName: string;
|
|
93
|
+
}
|
|
94
|
+
type OfficialDtsOptions = Exclude<ModuleFederationOptions['dts'], boolean | undefined>;
|
|
95
|
+
/** 补齐当前 Vite 配置类型尚未声明的官方 afterGenerate 钩子。 */
|
|
96
|
+
interface FederationDtsOptions extends Omit<OfficialDtsOptions, 'generateTypes'> {
|
|
97
|
+
generateTypes?: boolean | (Exclude<OfficialDtsOptions['generateTypes'], boolean | undefined> & {
|
|
98
|
+
afterGenerate?: (options: FederationGeneratedTypes) => Promise<void> | void;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
interface FederationConfig extends Omit<ModuleFederationOptions, 'name' | 'remotes' | 'exposes' | 'shared' | 'dts'> {
|
|
90
102
|
/** 默认使用组合插件的 appName(连字符转下划线);单独调用 federationPlugin 时必填。 */
|
|
91
103
|
name?: string;
|
|
92
104
|
/** 官方类型生成与消费配置,默认 true;可分别控制 generateTypes / consumeTypes。 */
|
|
93
|
-
dts?:
|
|
105
|
+
dts?: boolean | FederationDtsOptions;
|
|
94
106
|
/** 暴露的模块 */
|
|
95
107
|
shared?: ModuleFederationOptions['shared'];
|
|
96
108
|
exposes?: NonNullable<ModuleFederationOptions['exposes']>;
|
|
@@ -194,7 +206,7 @@ interface MfGeneratorOptions {
|
|
|
194
206
|
interface MicroFrontendPluginOptions {
|
|
195
207
|
/** 应用名称 */
|
|
196
208
|
appName: string;
|
|
197
|
-
/**
|
|
209
|
+
/** 本地入口启动已部署基座,仅 serve 生效。 */
|
|
198
210
|
developmentShell?: DevelopmentShellConfig;
|
|
199
211
|
/** @deprecated 未参与插件行为;开发模式由 Vite command 决定。 */
|
|
200
212
|
useDevMode?: boolean;
|
|
@@ -296,4 +308,4 @@ declare const MICRO_FRONTEND_OPTIONS_KEY = "__microFrontendOptions";
|
|
|
296
308
|
declare function microFrontendPlugins(options: MicroFrontendPluginOptions): PluginOption[];
|
|
297
309
|
|
|
298
310
|
export { MICRO_FRONTEND_OPTIONS_KEY, devHmrPlugin, federationPlugin, generateMf, generateRoutesToFile, getDynamicBasePlugins, getRoutes, mfGeneratorPlugin, microFrontendPlugins, pagesRoutesPlugin };
|
|
299
|
-
export type { ComponentMetadata, DevelopmentShellConfig, DynamicBaseConfig, FederationApplicationConfig, FederationConfig, IRoute, MfGeneratorOptions, MicroFrontendPluginOptions, RemoteConfig, RouteConfig, RoutesPluginOptions, RoutesTemplateOptions, VitePlugin };
|
|
311
|
+
export type { ComponentMetadata, DevelopmentShellConfig, DynamicBaseConfig, FederationApplicationConfig, FederationConfig, FederationDtsOptions, FederationGeneratedTypes, IRoute, MfGeneratorOptions, MicroFrontendPluginOptions, RemoteConfig, RouteConfig, RoutesPluginOptions, RoutesTemplateOptions, VitePlugin };
|