@fulgurjs/federation 5.1.4 → 5.2.1

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.en.md +5 -5
  3. package/README.md +12 -12
  4. package/dist/index.cjs +91 -3
  5. package/dist/index.js +91 -3
  6. package/dist/react-adapter.cjs +32 -1
  7. package/dist/react-adapter.d.cts +1 -1
  8. package/dist/react-adapter.d.ts +1 -1
  9. package/dist/react-adapter.js +32 -1
  10. package/dist/react.d.ts +1 -1
  11. package/dist/runtime-entry.d.ts +1 -1
  12. package/dist/runtime.js +1 -1
  13. package/dist/vue-adapter.cjs +99 -15
  14. package/dist/vue-adapter.d.cts +1 -1
  15. package/dist/vue-adapter.d.ts +1 -1
  16. package/dist/vue-adapter.js +100 -16
  17. package/examples/README.en.md +47 -0
  18. package/examples/README.md +40 -18
  19. package/examples/react/README.md +83 -0
  20. package/examples/react/host/README.md +49 -0
  21. package/examples/react/host/fulgurjs.config.ts +31 -0
  22. package/examples/{react-remote → react/host}/index.html +2 -1
  23. package/examples/react/host/package-lock.json +2383 -0
  24. package/examples/{react-host → react/host}/package.json +9 -8
  25. package/examples/react/host/src/App.tsx +19 -0
  26. package/examples/react/host/src/main.tsx +33 -0
  27. package/examples/react/host/src/pages/UtilsDemo.tsx +32 -0
  28. package/examples/react/host/src/remotePages.tsx +16 -0
  29. package/examples/{react-remote → react/host}/tsconfig.json +2 -3
  30. package/examples/{react-remote → react/host}/vite.config.ts +2 -2
  31. package/examples/react/remote/README.md +47 -0
  32. package/examples/react/remote/fulgurjs.config.ts +20 -0
  33. package/examples/{react-host → react/remote}/index.html +2 -1
  34. package/examples/react/remote/package-lock.json +2325 -0
  35. package/examples/{react-remote → react/remote}/package.json +8 -7
  36. package/examples/react/remote/src/App.tsx +16 -0
  37. package/examples/react/remote/src/exposes/ClickButton.tsx +15 -0
  38. package/examples/react/remote/src/exposes/pages/DetailPage.tsx +10 -0
  39. package/examples/react/remote/src/exposes/pages/HomePage.tsx +12 -0
  40. package/examples/react/remote/src/exposes/utils.ts +10 -0
  41. package/examples/react/remote/src/main.tsx +11 -0
  42. package/examples/{react-host → react/remote}/tsconfig.json +3 -4
  43. package/examples/{react-host → react/remote}/vite.config.ts +2 -2
  44. package/examples/vue/README.md +85 -0
  45. package/examples/vue/host/README.md +50 -0
  46. package/examples/vue/host/fulgurjs.config.ts +30 -0
  47. package/examples/vue/host/index.html +12 -0
  48. package/examples/vue/host/package-lock.json +1857 -0
  49. package/examples/vue/host/package.json +21 -0
  50. package/examples/vue/host/src/App.vue +12 -0
  51. package/examples/vue/host/src/main.ts +44 -0
  52. package/examples/vue/host/src/pages/HomePage.vue +16 -0
  53. package/examples/vue/host/src/pages/UtilsDemo.vue +45 -0
  54. package/examples/vue/host/tsconfig.json +13 -0
  55. package/examples/vue/host/vite.config.ts +8 -0
  56. package/examples/vue/remote/README.md +47 -0
  57. package/examples/vue/remote/fulgurjs.config.ts +20 -0
  58. package/examples/vue/remote/index.html +12 -0
  59. package/examples/vue/remote/package-lock.json +1835 -0
  60. package/examples/vue/remote/package.json +20 -0
  61. package/examples/vue/remote/src/App.vue +16 -0
  62. package/examples/vue/remote/src/exposes/ClickButton.vue +16 -0
  63. package/examples/vue/remote/src/exposes/pages/DetailPage.vue +14 -0
  64. package/examples/vue/remote/src/exposes/pages/HomePage.vue +11 -0
  65. package/examples/vue/remote/src/exposes/utils.ts +10 -0
  66. package/examples/vue/remote/src/main.ts +6 -0
  67. package/examples/vue/remote/tsconfig.json +13 -0
  68. package/examples/vue/remote/vite.config.ts +8 -0
  69. package/package.json +2 -2
  70. package/examples/host/fulgurjs.config.ts +0 -26
  71. package/examples/react-host/README.md +0 -28
  72. package/examples/react-host/fulgurjs.config.ts +0 -19
  73. package/examples/react-host/src/App.tsx +0 -66
  74. package/examples/react-host/src/federation/pages.data.ts +0 -21
  75. package/examples/react-host/src/main.tsx +0 -8
  76. package/examples/react-remote/fulgurjs.config.ts +0 -17
  77. package/examples/react-remote/src/Button.tsx +0 -15
  78. package/examples/react-remote/src/main.tsx +0 -6
  79. package/examples/react-remote/src/pages/Detail.tsx +0 -10
  80. package/examples/react-remote/src/pages/Home.tsx +0 -14
  81. package/examples/react-remote/src/utils.ts +0 -3
  82. package/examples/remote-a/fulgurjs.config.ts +0 -24
  83. package/examples/remote-a/src/views/Detail.vue +0 -3
  84. package/examples/remote-a/src/views/Home.vue +0 -3
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "fulgurjs-demo-vue-host",
3
+ "private": true,
4
+ "version": "0.0.0",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite --port 5214 --strictPort",
8
+ "build": "vite build",
9
+ "preview": "vite preview --port 5214 --strictPort"
10
+ },
11
+ "dependencies": {
12
+ "@fulgurjs/federation": "5.2.0",
13
+ "vue": "^3.5.13",
14
+ "vue-router": "^4.5.0"
15
+ },
16
+ "devDependencies": {
17
+ "@vitejs/plugin-vue": "^5.2.1",
18
+ "typescript": "^5.8.0",
19
+ "vite": "^6.3.5"
20
+ }
21
+ }
@@ -0,0 +1,12 @@
1
+ <template>
2
+ <div style="font-family: sans-serif; padding: 16px; max-width: 860px">
3
+ <h1>vue-host(fulgurjs 联邦宿主)</h1>
4
+ <nav style="display: flex; gap: 16px; margin-bottom: 16px">
5
+ <RouterLink to="/">首页</RouterLink>
6
+ <RouterLink to="/remote/home">远程首页</RouterLink>
7
+ <RouterLink to="/remote/detail/7?tab=basic">远程详情(id=7)</RouterLink>
8
+ <RouterLink to="/utils-demo">TS 模块调用</RouterLink>
9
+ </nav>
10
+ <RouterView />
11
+ </div>
12
+ </template>
@@ -0,0 +1,44 @@
1
+ import { createApp, h } from 'vue'
2
+ import { createRouter, createWebHistory, useRoute } from 'vue-router'
3
+ import { createHostPages } from '@fulgurjs/federation/runtime'
4
+ import App from './App.vue'
5
+ import HomePage from './pages/HomePage.vue'
6
+ import UtilsDemo from './pages/UtilsDemo.vue'
7
+ import { pages, remotePrefixes } from '../fulgurjs.config.ts'
8
+
9
+ // 页面适配器:URL 解析、最长前缀远程归属、异步组件缓存、
10
+ // 加载/错误占位(含「重试加载 / 刷新页面重试」)全部由插件完成。
11
+ const hostPages = createHostPages({
12
+ pages,
13
+ remotePrefixes,
14
+ })
15
+
16
+ const router = createRouter({
17
+ history: createWebHistory(),
18
+ routes: [
19
+ { path: '/', component: HomePage },
20
+ { path: '/utils-demo', component: UtilsDemo },
21
+ {
22
+ // 远程页面:spec 经 hostPages.component(spec) 取异步组件(spec = 远程名 + exposes 键);
23
+ // route.params + route.query 作为 props 透传给远程页面组件
24
+ path: '/remote/home',
25
+ component: { render: () => h(hostPages.component('vue-remote/pages/HomePage')) },
26
+ },
27
+ {
28
+ path: '/remote/detail/:id',
29
+ // params + query 全量透传给远程页面组件(响应式:路由变化即重渲染)
30
+ component: {
31
+ setup() {
32
+ const route = useRoute()
33
+ return () =>
34
+ h(hostPages.component('vue-remote/pages/DetailPage'), {
35
+ id: route.params.id as string,
36
+ tab: (route.query.tab as string | undefined) ?? undefined,
37
+ })
38
+ },
39
+ },
40
+ },
41
+ ],
42
+ })
43
+
44
+ createApp(App).use(router).mount('#app')
@@ -0,0 +1,16 @@
1
+ <template>
2
+ <div>
3
+ <h2>宿主首页</h2>
4
+ <p>下面的按钮来自 vue-remote(<code>./ClickButton</code> expose)。
5
+ 仅在进入本页时按需加载——首页不会批量预取未访问的远程页面。</p>
6
+ <RemoteButton />
7
+ </div>
8
+ </template>
9
+
10
+ <script setup lang="ts">
11
+ import { remoteComponent } from '@fulgurjs/federation/runtime'
12
+
13
+ // remoteComponent:首次渲染才真正加载远程模块;失败时插件呈现默认错误占位
14
+ // (含「重试加载 / 刷新页面重试」),无需自己实现错误组件。
15
+ const RemoteButton = remoteComponent('vue-remote/ClickButton')
16
+ </script>
@@ -0,0 +1,45 @@
1
+ <template>
2
+ <div>
3
+ <h2>普通 TS 模块调用</h2>
4
+ <p>调用 vue-remote 暴露的 <code>./utils</code> 模块(loadRemote 返回模块命名空间,显式调用其导出):</p>
5
+ <p v-if="error" data-testid="demo-utils-error" style="color: #c45656">调用失败:{{ error }}</p>
6
+ <ul v-else-if="result" data-testid="demo-utils-result">
7
+ <li>sumNumbers(2, 3, 7) = {{ result.sum }}</li>
8
+ <li>DEMO_ANSWER = {{ result.answer }}</li>
9
+ <li>formatPrice(12.5) = {{ result.price }}</li>
10
+ </ul>
11
+ <p v-else>加载中…</p>
12
+ <button data-testid="demo-utils-reload" @click="load">重新调用</button>
13
+ </div>
14
+ </template>
15
+
16
+ <script setup lang="ts">
17
+ import { onMounted, ref } from 'vue'
18
+ import { loadRemote } from '@fulgurjs/federation/runtime'
19
+
20
+ // 普通 TS 模块同样走 loadRemote:拿到命名空间后像本地模块一样调用
21
+ interface RemoteUtils {
22
+ DEMO_ANSWER: number
23
+ sumNumbers: (...numbers: number[]) => number
24
+ formatPrice: (yuan: number) => string
25
+ }
26
+
27
+ const result = ref<{ sum: number; answer: number; price: string } | null>(null)
28
+ const error = ref('')
29
+
30
+ async function load(): Promise<void> {
31
+ error.value = ''
32
+ try {
33
+ const utils = await loadRemote<RemoteUtils>('vue-remote/utils')
34
+ result.value = {
35
+ sum: utils.sumNumbers(2, 3, 7),
36
+ answer: utils.DEMO_ANSWER,
37
+ price: utils.formatPrice(12.5),
38
+ }
39
+ } catch (e) {
40
+ error.value = String((e as Error)?.message ?? e)
41
+ }
42
+ }
43
+
44
+ onMounted(load)
45
+ </script>
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "strict": true,
7
+ "jsx": "preserve",
8
+ "skipLibCheck": true,
9
+ "noEmit": true,
10
+ "types": ["vite/client"]
11
+ },
12
+ "include": ["src", "fulgurjs.config.ts", "vite.config.ts"]
13
+ }
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from 'vite'
2
+ import vue from '@vitejs/plugin-vue'
3
+ import { federation } from '@fulgurjs/federation'
4
+ import fulgurjsConfig from './fulgurjs.config.ts'
5
+
6
+ export default defineConfig({
7
+ plugins: [vue(), federation(fulgurjsConfig)],
8
+ })
@@ -0,0 +1,47 @@
1
+ # vue-remote —— Vue 远程(fulgurjs 联邦演示)
2
+
3
+ > **角色:Vue 远程(被消费方)。** 向 [`../host/`](../host/)(vue-host,端口 5214)暴露可点击按钮组件、普通 TS 工具模块、联邦首页与参数详情页;也可独立运行调试。
4
+
5
+ ## 从干净拷贝运行
6
+
7
+ ```bash
8
+ cd vue-remote # 把本目录复制到任意位置后进入
9
+ npm install
10
+ npm run dev # http://localhost:5213
11
+ ```
12
+
13
+ 独立运行画面:`vue-remote 独立运行` 页面 + 一枚可点击计数按钮(这是远程自己的壳,`src/main.ts` + `src/App.vue`;联邦消费走 `fulgurjs-remoteEntry.js` 容器入口,与壳无关)。
14
+
15
+ ## 暴露的模块(exposes)
16
+
17
+ | expose 键 | 源文件 | 说明 |
18
+ | --- | --- | --- |
19
+ | `./ClickButton` | `src/exposes/ClickButton.vue` | 可点击计数按钮(本地状态在组件内) |
20
+ | `./utils` | `src/exposes/utils.ts` | 普通 TS 模块:`sumNumbers` / `DEMO_ANSWER` / `formatPrice` |
21
+ | `./pages/HomePage` | `src/exposes/pages/HomePage.vue` | 联邦页面:远程首页 |
22
+ | `./pages/DetailPage` | `src/exposes/pages/DetailPage.vue` | 联邦页面:详情页(接收宿主路由的 `id` / `tab` props) |
23
+
24
+ 宿主如何调用 `./utils`(宿主侧代码):
25
+
26
+ ```ts
27
+ import { loadRemote } from '@fulgurjs/federation/runtime'
28
+ const utils = await loadRemote<{ sumNumbers: (...n: number[]) => number }>('vue-remote/utils')
29
+ utils.sumNumbers(2, 3, 7) // 12
30
+ ```
31
+
32
+ ## 关键配置
33
+
34
+ - `fulgurjs.config.ts`:**联邦唯一声明**(name = `vue-remote`、exposes、shared)。改 expose 键名时宿主页面表/引用需同步。
35
+ - `vite.config.ts`:`federation(fulgurjsConfig)` 挂载插件;端口在 `package.json` 的 `dev` 脚本(5213)。
36
+
37
+ ## 生产构建与部署
38
+
39
+ ```bash
40
+ npm run build # dist/(含 fulgurjs-remoteEntry.js 与 fulgurjs-manifest.json)
41
+ ```
42
+
43
+ 部署到 Nginx 子路径 `/vue-remote/`(与宿主 `fulgurjs.config.ts` 的 `prod: '/vue-remote'` 对应;产物内 chunk 引用为相对路径,天然适配子路径)。完整 Nginx 示例见[组合说明](../README.md#生产构建与最小-nginx-部署)。
44
+
45
+ ## 故障演练(配合宿主)
46
+
47
+ 停掉本 dev server → 宿主远程页面出现插件默认错误占位 → 重启本服务 → 宿主占位上「重试加载」同页恢复,或「刷新页面重试」整页恢复。
@@ -0,0 +1,20 @@
1
+ import type { FederationOptions } from '@fulgurjs/federation'
2
+
3
+ /**
4
+ * 远程端联邦配置(唯一需要维护的联邦声明文件)。
5
+ * exposes:宿主将按这里的键加载模块;改键名时宿主页面表/引用需同步。
6
+ */
7
+ export default {
8
+ name: 'vue-remote',
9
+ // 生产构建的容器入口文件名(部署后宿主 prod 地址指向它所在目录)
10
+ filename: 'fulgurjs-remoteEntry.js',
11
+ exposes: {
12
+ './ClickButton': './src/exposes/ClickButton.vue',
13
+ './utils': './src/exposes/utils.ts',
14
+ './pages/HomePage': './src/exposes/pages/HomePage.vue',
15
+ './pages/DetailPage': './src/exposes/pages/DetailPage.vue',
16
+ },
17
+ shared: {
18
+ vue: { singleton: true, requiredVersion: '^3.4.0' },
19
+ },
20
+ } satisfies FederationOptions
@@ -0,0 +1,12 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>vue-remote(fulgurjs 演示远程)</title>
7
+ </head>
8
+ <body>
9
+ <div id="app"></div>
10
+ <script type="module" src="/src/main.ts"></script>
11
+ </body>
12
+ </html>