@fulgurjs/federation 5.1.3 → 5.2.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/CHANGELOG.md +17 -0
- package/README.en.md +7 -5
- package/README.md +14 -12
- package/dist/context.cjs +2 -2
- package/dist/context.js +2 -2
- package/dist/index.cjs +141 -48
- package/dist/index.js +141 -48
- package/dist/react-adapter.cjs +32 -1
- package/dist/react-adapter.d.cts +1 -1
- package/dist/react-adapter.d.ts +1 -1
- package/dist/react-adapter.js +32 -1
- package/dist/react.d.ts +2 -2
- package/dist/runtime-entry.d.ts +2 -2
- package/dist/runtime.js +7 -7
- package/dist/vue-adapter.cjs +99 -15
- package/dist/vue-adapter.d.cts +1 -1
- package/dist/vue-adapter.d.ts +1 -1
- package/dist/vue-adapter.js +100 -16
- package/examples/README.en.md +47 -0
- package/examples/README.md +40 -18
- package/examples/react/README.md +83 -0
- package/examples/react/host/README.md +49 -0
- package/examples/react/host/fulgurjs.config.ts +31 -0
- package/examples/{react-remote → react/host}/index.html +2 -1
- package/examples/react/host/package-lock.json +2381 -0
- package/examples/react/host/package.json +24 -0
- package/examples/react/host/src/App.tsx +19 -0
- package/examples/react/host/src/main.tsx +33 -0
- package/examples/react/host/src/pages/UtilsDemo.tsx +32 -0
- package/examples/react/host/src/remotePages.tsx +16 -0
- package/examples/{react-remote → react/host}/tsconfig.json +2 -3
- package/examples/{react-remote → react/host}/vite.config.ts +2 -2
- package/examples/react/remote/README.md +47 -0
- package/examples/react/remote/fulgurjs.config.ts +20 -0
- package/examples/{react-host → react/remote}/index.html +2 -1
- package/examples/react/remote/package-lock.json +2323 -0
- package/examples/react/remote/package.json +23 -0
- package/examples/react/remote/src/App.tsx +16 -0
- package/examples/react/remote/src/exposes/ClickButton.tsx +15 -0
- package/examples/react/remote/src/exposes/pages/DetailPage.tsx +10 -0
- package/examples/react/remote/src/exposes/pages/HomePage.tsx +12 -0
- package/examples/react/remote/src/exposes/utils.ts +10 -0
- package/examples/react/remote/src/main.tsx +11 -0
- package/examples/{react-host → react/remote}/tsconfig.json +3 -4
- package/examples/{react-host → react/remote}/vite.config.ts +2 -2
- package/examples/vue/README.md +85 -0
- package/examples/vue/host/README.md +50 -0
- package/examples/vue/host/fulgurjs.config.ts +30 -0
- package/examples/vue/host/index.html +12 -0
- package/examples/vue/host/package-lock.json +1855 -0
- package/examples/vue/host/package.json +21 -0
- package/examples/vue/host/src/App.vue +12 -0
- package/examples/vue/host/src/main.ts +44 -0
- package/examples/vue/host/src/pages/HomePage.vue +16 -0
- package/examples/vue/host/src/pages/UtilsDemo.vue +45 -0
- package/examples/vue/host/tsconfig.json +13 -0
- package/examples/vue/host/vite.config.ts +8 -0
- package/examples/vue/remote/README.md +47 -0
- package/examples/vue/remote/fulgurjs.config.ts +20 -0
- package/examples/vue/remote/index.html +12 -0
- package/examples/vue/remote/package-lock.json +1833 -0
- package/examples/vue/remote/package.json +20 -0
- package/examples/vue/remote/src/App.vue +16 -0
- package/examples/vue/remote/src/exposes/ClickButton.vue +16 -0
- package/examples/vue/remote/src/exposes/pages/DetailPage.vue +14 -0
- package/examples/vue/remote/src/exposes/pages/HomePage.vue +11 -0
- package/examples/vue/remote/src/exposes/utils.ts +10 -0
- package/examples/vue/remote/src/main.ts +6 -0
- package/examples/vue/remote/tsconfig.json +13 -0
- package/examples/vue/remote/vite.config.ts +8 -0
- package/package.json +2 -2
- package/examples/host/fulgurjs.config.ts +0 -26
- package/examples/react-host/README.md +0 -28
- package/examples/react-host/fulgurjs.config.ts +0 -19
- package/examples/react-host/package.json +0 -23
- package/examples/react-host/src/App.tsx +0 -66
- package/examples/react-host/src/federation/pages.data.ts +0 -21
- package/examples/react-host/src/main.tsx +0 -8
- package/examples/react-remote/fulgurjs.config.ts +0 -17
- package/examples/react-remote/package.json +0 -22
- package/examples/react-remote/src/Button.tsx +0 -15
- package/examples/react-remote/src/main.tsx +0 -6
- package/examples/react-remote/src/pages/Detail.tsx +0 -10
- package/examples/react-remote/src/pages/Home.tsx +0 -14
- package/examples/react-remote/src/utils.ts +0 -3
- package/examples/remote-a/fulgurjs.config.ts +0 -24
- package/examples/remote-a/src/views/Detail.vue +0 -3
- package/examples/remote-a/src/views/Home.vue +0 -3
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# React 联邦示例:宿主 + 远程(可直接复制运行)
|
|
2
|
+
|
|
3
|
+
本目录是一对完整、可独立安装运行的 React 19 模块联邦演示:
|
|
4
|
+
|
|
5
|
+
| 目录 | 角色 | 端口 | 说明 |
|
|
6
|
+
| --- | --- | --- | --- |
|
|
7
|
+
| [`remote/`](./remote/) | **React 远程**(被消费方) | 5203 | 暴露可点击按钮组件、普通 TS 工具模块、联邦首页与参数详情页 |
|
|
8
|
+
| [`host/`](./host/) | **React 宿主**(消费方) | 5204 | 消费 react-remote 的组件/模块/页面,提供导航与懒加载 |
|
|
9
|
+
|
|
10
|
+
- 包管理器统一为 **npm**;插件依赖为 **registry 精确正式包**(`@fulgurjs/federation`,见各 `package.json`),不依赖本仓库源码、monorepo workspace 或父目录 node_modules。
|
|
11
|
+
- 把 `host/` 与 `remote/` 复制到**两个互不共享父目录的目录**后,各自 `npm install` 仍可运行。
|
|
12
|
+
|
|
13
|
+
## 最快开始(从干净拷贝)
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# 终端 1 —— 先启动远程
|
|
17
|
+
cd react/remote
|
|
18
|
+
npm install
|
|
19
|
+
npm run dev # http://localhost:5203
|
|
20
|
+
|
|
21
|
+
# 终端 2 —— 再启动宿主
|
|
22
|
+
cd react/host
|
|
23
|
+
npm install
|
|
24
|
+
npm run dev # http://localhost:5204
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
打开 <http://localhost:5204/>:
|
|
28
|
+
|
|
29
|
+
- 「首页」里有来自 react-remote 的**可点击计数按钮**(进入页面才按需加载)。
|
|
30
|
+
- 「远程首页 / 远程详情」是 react-remote 暴露的**联邦页面**;详情页演示真实路由参数(`/remote/detail/7?tab=basic`)。
|
|
31
|
+
- 「TS 模块调用」演示 `useLoadRemote` 调用 react-remote 暴露的普通 TS 模块。
|
|
32
|
+
- 停掉远程 dev server 再刷新宿主页面 → 插件默认错误占位出现,提供**「重试加载」**(同页恢复)与**「刷新页面重试」**(整页恢复)两个操作;重启远程后点击即可恢复业务。
|
|
33
|
+
|
|
34
|
+
## 演示的功能点
|
|
35
|
+
|
|
36
|
+
| 功能 | 位置 |
|
|
37
|
+
| --- | --- |
|
|
38
|
+
| 远程可点击组件(Hooks 计数) | `remote/src/exposes/ClickButton.tsx`,宿主首页消费 |
|
|
39
|
+
| 普通 TS 模块调用(useLoadRemote) | `remote/src/exposes/utils.ts`,`host/src/pages/UtilsDemo.tsx` |
|
|
40
|
+
| 联邦页面(远程首页 / 带参数详情页) | `remote/src/exposes/pages/`,页面表见 `host/fulgurjs.config.ts` |
|
|
41
|
+
| 宿主导航 + 懒加载(不批量预取) | `host/src/main.tsx`(路由级按需加载) |
|
|
42
|
+
| 默认加载/错误/恢复占位 | 插件内置(`remoteComponent` / `createReactHostPages`),示例不自建错误组件 |
|
|
43
|
+
|
|
44
|
+
## 生产构建与最小 Nginx 部署
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
cd react/remote && npm run build -- --base=/react-remote/ # 产物 remote/dist/(子路径 base 与部署位置对应)
|
|
48
|
+
cd react/host && npm run build # 产物 host/dist/
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
部署形态:宿主部署在站点根 `/`,远程部署在子路径 `/react-remote/`(与 `host/fulgurjs.config.ts` 的 `prod: '/react-remote'` 对应)。最小 Nginx 配置:
|
|
52
|
+
|
|
53
|
+
```nginx
|
|
54
|
+
server {
|
|
55
|
+
listen 8081;
|
|
56
|
+
|
|
57
|
+
root /srv/react-demo/host;
|
|
58
|
+
index index.html;
|
|
59
|
+
|
|
60
|
+
location /react-remote/ {
|
|
61
|
+
alias /srv/react-demo/remote/;
|
|
62
|
+
try_files $uri $uri/ =404;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# 宿主 SPA fallback:深链刷新回退到 index.html
|
|
66
|
+
location / {
|
|
67
|
+
try_files $uri /index.html;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
部署后访问 `http://<host>:8081/`,深链 `http://<host>:8081/remote/detail/7?tab=basic` 直接刷新也能到达同一页面。
|
|
73
|
+
|
|
74
|
+
## HMR 边界(dev)
|
|
75
|
+
|
|
76
|
+
- 远程组件修改(文本/样式/Hooks 结构不变的改动)自动热更新到**正在显示的宿主页面**,组件本地状态保留,无整页刷新(5.2.0 起跨源 Fast Refresh 由插件保证单一 react-refresh 实例)。
|
|
77
|
+
- 远程普通 TS 模块修改同样自动传播到引用它的组件边界。
|
|
78
|
+
- React Refresh 不兼容的导出/Hooks 结构变化、Vite 要求 full-reload 的改动会按框架标准重新挂载或整页刷新——不承诺任意改动保活。
|
|
79
|
+
|
|
80
|
+
## examples 与 fixtures 的区别
|
|
81
|
+
|
|
82
|
+
`examples/` 面向**使用者**:拷贝、安装 registry 正式包、独立运行。
|
|
83
|
+
仓库根的 `fixtures/` 是**插件内部回归夹具**(源码 `link:` 到插件工作区),不要当作使用模板。
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# react-host —— React 宿主(fulgurjs 联邦演示)
|
|
2
|
+
|
|
3
|
+
> **角色:React 宿主(消费方)。** 消费 [`../remote/`](../remote/)(react-remote,端口 5203)暴露的组件、TS 模块与联邦页面;本应用自身不暴露任何模块。
|
|
4
|
+
|
|
5
|
+
## 从干净拷贝运行
|
|
6
|
+
|
|
7
|
+
前置:Node ≥ 20.19 与 npm。
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# 先启动远程(另一终端):见 ../remote/README.md
|
|
11
|
+
cd react-host # 把本目录复制到任意位置后进入
|
|
12
|
+
npm install
|
|
13
|
+
npm run dev # http://localhost:5204
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
成功画面:导航条含「首页 / 远程首页 / 远程详情 / TS 模块调用」;「远程首页」出现 react-remote 的标题与可点击计数按钮。
|
|
17
|
+
|
|
18
|
+
## 目录与关键配置
|
|
19
|
+
|
|
20
|
+
| 文件 | 职责 |
|
|
21
|
+
| --- | --- |
|
|
22
|
+
| `fulgurjs.config.ts` | **联邦唯一声明**:remotes 地址(dev/prod)、页面表 `pages`、前缀归属 `remotePrefixes` |
|
|
23
|
+
| `vite.config.ts` | 标准 Vite 配置,`federation(fulgurjsConfig)` 挂载插件 |
|
|
24
|
+
| `src/remotePages.tsx` | `createReactHostPages({ pages, remotePrefixes })`(模块顶层一次);`component(spec)` 取远程页面组件 |
|
|
25
|
+
| `src/App.tsx` | `remoteComponent('react-remote/ClickButton')` 消费单个远程组件 |
|
|
26
|
+
| `src/pages/UtilsDemo.tsx` | `useLoadRemote('react-remote/utils')` 调用普通 TS 模块 |
|
|
27
|
+
| `src/main.tsx` | react-router 路由;参数页把 `params`/`query` 透传给远程组件 |
|
|
28
|
+
|
|
29
|
+
## API 职责速查
|
|
30
|
+
|
|
31
|
+
- `remoteComponent(spec, opts?)`:单个远程组件;失败时插件内置中文错误占位(错误码 + 修法 + **重试加载** + **刷新页面重试**)。
|
|
32
|
+
- `useLoadRemote(spec)`:hook 形态的模块加载(`data/error/loading/reload`),适合普通 TS 模块。
|
|
33
|
+
- `createReactHostPages(...)` + `component(spec)`:页面表驱动的联邦页面。
|
|
34
|
+
|
|
35
|
+
## 生产构建与部署
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm run build # dist/
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
部署在站点根 `/`(Nginx SPA fallback 见[组合说明](../README.md#生产构建与最小-nginx-部署));深链刷新由 fallback + 插件路由解析恢复。
|
|
42
|
+
|
|
43
|
+
## 恢复操作(故障演练)
|
|
44
|
+
|
|
45
|
+
停掉 react-remote → 刷新宿主进入远程页面 → 默认错误占位出现 → 重启 react-remote → 点「重试加载」同页恢复;静态子依赖曾失败等场景点「刷新页面重试」整页恢复。
|
|
46
|
+
|
|
47
|
+
## HMR(5.2.0 起)
|
|
48
|
+
|
|
49
|
+
react-remote 的组件修改(文本/样式/Hooks 结构不变)自动热更新到本宿主正在显示的页面并**保留组件本地状态**(插件保证全页单一 react-refresh 实例);普通 TS 模块修改自动传播到引用它的组件边界;不兼容改动按框架标准重新挂载/整页刷新。
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { FederationOptions, PageRouteLike } from '@fulgurjs/federation'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 宿主端联邦配置(唯一需要维护的联邦声明文件)。
|
|
5
|
+
* - remotes:改远程地址只动这里;dev 与生产可以分别指定。
|
|
6
|
+
* - hostPages:页面表 + 路由前缀归属,浏览器侧 createReactHostPages 与 CLI check-pages 共用。
|
|
7
|
+
*/
|
|
8
|
+
export default {
|
|
9
|
+
name: 'react-host',
|
|
10
|
+
remotes: {
|
|
11
|
+
'react-remote': {
|
|
12
|
+
dev: 'http://localhost:5203',
|
|
13
|
+
prod: '/react-remote',
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
shared: {
|
|
17
|
+
react: { singleton: true },
|
|
18
|
+
'react-dom': { singleton: true },
|
|
19
|
+
},
|
|
20
|
+
} satisfies FederationOptions
|
|
21
|
+
|
|
22
|
+
/** 页面表:URL 路由 → 远程 expose 的映射(spec = 远程名 + exposes 键) */
|
|
23
|
+
export const pages: PageRouteLike[] = [
|
|
24
|
+
{ route: '/remote/home', spec: 'pages/HomePage', name: 'RemoteHome', title: '远程首页' },
|
|
25
|
+
{ route: '/remote/detail/:id', spec: 'pages/DetailPage', name: 'RemoteDetail', title: '远程详情' },
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
/** 路由前缀 → 远程归属(最长前缀匹配) */
|
|
29
|
+
export const remotePrefixes: Record<string, string> = {
|
|
30
|
+
'/remote': 'react-remote',
|
|
31
|
+
}
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
<html lang="zh-CN">
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
|
-
<
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>react-host(fulgurjs 演示宿主)</title>
|
|
6
7
|
</head>
|
|
7
8
|
<body>
|
|
8
9
|
<div id="root"></div>
|