Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
monorepo
/
venus
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
1
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 2576916e
authored
Nov 21, 2025
by
Lin
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat:子应用模版构建流程优化(交互式)
1 parent
52fe1c52
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
243 additions
and
13 deletions
apps/frontend-micro/venus-base/src/layouts/BasicLayout.vue
docs/COMMUNICATION_REFACTOR.md
package.json
packages/cli/package.json
packages/cli/src/index.ts
pnpm-lock.yaml
scripts/turbo-run/src/index.ts
apps/frontend-micro/venus-base/src/layouts/BasicLayout.vue
View file @
2576916
<
script
setup
lang=
"ts"
>
<
script
setup
lang=
"ts"
>
import
type
{
BreadcrumbItem
,
MenuItem
}
from
'@/types/menu'
;
import
type
{
BreadcrumbItem
,
MenuItem
}
from
'@/types/menu'
;
import
{
computed
,
ref
,
watch
}
from
'vue'
;
import
{
computed
,
ref
,
watch
}
from
'vue'
;
import
{
useRoute
,
useRouter
}
from
'vue-router'
;
import
{
useRoute
,
useRouter
}
from
'vue-router'
;
import
NotificationCenter
from
'@/components/NotificationCenter.vue'
;
import
NotificationCenter
from
'@/components/NotificationCenter.vue'
;
import
SidebarMenuItem
from
'@/components/SidebarMenuItem.vue'
;
import
SidebarMenuItem
from
'@/components/SidebarMenuItem.vue'
;
import
UserInfo
from
'@/components/UserInfo.vue'
;
import
UserInfo
from
'@/components/UserInfo.vue'
;
...
...
docs/COMMUNICATION_REFACTOR.md
0 → 100644
View file @
2576916
# 子应用通信模块重构说明
## 重构目标
将子应用的
`communication.ts`
重构为轻量级版本,直接使用基座通过 props 下发的通信管理器实例,避免维护本地实现。
## 主要变化
### 1. **communication.ts 重构**
#### 之前的实现
-
包含完整的本地通信管理器实现(
`LocalSubAppCommunicationManager`
)
-
包含降级方案逻辑
-
代码量约 287 行,逻辑复杂
#### 现在的实现
-
仅保留类型定义和接口(与基座保持一致)
-
移除所有本地实现逻辑
-
通过全局变量存储基座传递的实例
-
提供简洁的工具函数封装
-
代码量约 200 行,逻辑清晰
**核心代码结构:**
```
typescript
// 1. 类型定义(与基座保持一致)
export
interface
GlobalState
{
...
}
export
interface
Notification
{
...
}
export
interface
SubAppCommunicationManager
{
...
}
// 2. 全局实例存储
let
communicationManagerInstance
:
SubAppCommunicationManager
|
null
=
null
;
// 3. 实例管理函数
export
function
setCommunicationManager
(
manager
)
{
...
}
export
function
getCommunicationManager
()
{
...
}
// 4. 便捷工具函数
export
function
useGlobalState
(
callback
)
{
...
}
export
function
notify
(
type
,
title
,
message
)
{
...
}
export
const
subAppCommunication
=
new
Proxy
(...)
// 兼容旧代码
```
### 2. **main.ts 简化**
#### 之前的逻辑
```
typescript
// 复杂的判断逻辑,包含降级方案
if
(
props
.
communicationManager
)
{
setCommunicationManager
(
props
.
communicationManager
);
props
.
communicationManager
.
init
(
props
);
}
else
if
(
props
.
onGlobalStateChange
)
{
// 降级方案
subAppCommunication
.
init
(
props
);
}
```
#### 现在的逻辑
```
typescript
// 简洁直接,只使用基座传递的实例
if
(
props
.
communicationManager
)
{
setCommunicationManager
(
props
.
communicationManager
);
props
.
communicationManager
.
init
(
props
);
app
.
config
.
globalProperties
.
$communication
=
props
.
communicationManager
;
}
else
{
console
.
error
(
'[子应用] 基座未传递通信管理器'
);
}
```
### 3. **类型安全增强**
-
所有类型定义在子应用中明确声明,避免跨项目导入问题
-
与基座类型定义保持完全一致
-
保持完整的 TypeScript 类型检查
## 架构流程
```
┌─────────────────────────────────────────────────────────┐
│ 微前端基座 │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ CommunicationManager (基座通信管理器) │ │
│ │ - 管理全局状态 │ │
│ │ - 监听状态变化 │ │
│ │ - 广播通知 │ │
│ └──────────────────┬───────────────────────────────┘ │
│ │ │
│ ┌──────────────────▼───────────────────────────────┐ │
│ │ SubAppCommunicationAdapter (子应用适配器) │ │
│ │ - 适配基座管理器到子应用接口 │ │
│ │ - 处理状态同步 │ │
│ │ - 实现 SubAppCommunicationManager 接口 │ │
│ └──────────────────┬───────────────────────────────┘ │
│ │ │
└─────────────────────┼───────────────────────────────────┘
│ props.communicationManager
│ (通过 qiankun 传递)
┌─────────────────────▼───────────────────────────────────┐
│ Vue3 子应用 │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ main.ts │ │
│ │ - 接收 props.communicationManager │ │
│ │ - 调用 setCommunicationManager() │ │
│ │ - 初始化实例 │ │
│ └──────────────────┬───────────────────────────────┘ │
│ │ │
│ ┌──────────────────▼───────────────────────────────┐ │
│ │ communication.ts (轻量级) │ │
│ │ - 存储实例引用 │ │
│ │ - 提供工具函数 │ │
│ │ - 导出类型定义 │ │
│ └──────────────────┬───────────────────────────────┘ │
│ │ │
│ ┌──────────────────▼───────────────────────────────┐ │
│ │ 组件使用 │ │
│ │ - useGlobalState(callback) │ │
│ │ - notify(type, title, message) │ │
│ │ - subAppCommunication.xxx() │ │
│ └──────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────┘
```
## 优势
### 1. **代码简化**
-
移除 287 行的复杂实现,保留核心功能
-
不再需要维护本地降级方案
-
代码更易理解和维护
### 2. **统一管理**
-
通信逻辑完全由基座控制
-
确保所有子应用使用相同的通信机制
-
便于统一升级和修改
### 3. **类型安全**
-
类型定义在子应用中明确声明
-
避免跨项目导入的复杂性
-
保持完整的 TypeScript 支持
### 4. **向后兼容**
-
保留
`useGlobalState`
、
`notify`
、
`subAppCommunication`
等 API
-
组件代码无需修改
-
平滑迁移
## 使用示例
### 在组件中监听全局状态
```
typescript
import
{
useGlobalState
}
from
'@/utils/communication'
;
import
type
{
GlobalState
}
from
'@/utils/communication'
;
// 在 setup 中使用
useGlobalState
((
state
:
GlobalState
,
prev
:
GlobalState
)
=>
{
console
.
log
(
'状态更新:'
,
state
);
// 处理状态变化
});
```
### 发送通知到基座
```
typescript
import
{
notify
}
from
'@/utils/communication'
;
// 发送成功通知
notify
(
'success'
,
'操作成功'
,
'数据已保存'
);
// 发送错误通知
notify
(
'error'
,
'操作失败'
,
'网络连接错误'
);
```
### 直接使用通信管理器
```
typescript
import
{
subAppCommunication
}
from
'@/utils/communication'
;
// 获取当前状态
const
state
=
subAppCommunication
.
getCurrentState
();
// 设置全局状态
subAppCommunication
.
setGlobalState
({
theme
:
{
mode
:
'dark'
,
primaryColor
:
'#1890ff'
}
});
```
## 注意事项
### 1. **基座依赖**
子应用现在完全依赖基座传递通信管理器实例,如果基座未传递:
-
会在控制台输出错误日志
-
通信功能不可用
-
不会影响应用其他功能
### 2. **类型同步**
当基座的类型定义发生变化时,需要同步更新子应用的类型定义,保持一致性。
### 3. **独立运行**
在独立运行模式下(不通过基座加载),通信功能不可用,但不会影响应用正常运行。
## 后续优化建议
### 1. **类型共享**
可以考虑将类型定义提取到独立的 npm 包或 monorepo 共享包中,避免重复定义。
### 2. **TypeScript Path Mapping**
配置 TypeScript 路径映射,直接从基座导入类型:
```
json
{
"compilerOptions"
:
{
"paths"
:
{
"@base/*"
:
[
"../venus-base/src/*"
]
}
}
}
```
### 3. **运行时类型检查**
添加运行时类型检查,确保基座传递的实例符合预期接口。
## 总结
此次重构将子应用的通信模块从"自主实现+降级方案"改为"完全依赖基座",实现了:
-
✅ 代码大幅简化
-
✅ 职责更加清晰
-
✅ 维护成本降低
-
✅ 保持向后兼容
-
✅ 类型安全保证
这是微前端架构中"基座控制,子应用依赖"模式的最佳实践。
package.json
View file @
2576916
...
@@ -25,29 +25,18 @@
...
@@ -25,29 +25,18 @@
"build:filter"
:
"node scripts/build/buildToRoot.mjs"
,
"build:filter"
:
"node scripts/build/buildToRoot.mjs"
,
"build:docs"
:
"pnpm run build --filter=@venus/docs"
,
"build:docs"
:
"pnpm run build --filter=@venus/docs"
,
"changeset"
:
"pnpm exec changeset"
,
"changeset"
:
"pnpm exec changeset"
,
"check"
:
"pnpm run check:circular && pnpm run check:dep && pnpm run check:type && pnpm check:cspell"
,
"check:circular"
:
"vsh check-circular"
,
"check:cspell"
:
"cspell lint **/*.ts **/README.md .changeset/*.md --no-progress"
,
"check:dep"
:
"vsh check-dep"
,
"check:type"
:
"turbo run typecheck"
,
"clean"
:
"node ./scripts/clean.mjs"
,
"clean"
:
"node ./scripts/clean.mjs"
,
"commit"
:
"powershell -ExecutionPolicy Bypass -File ./scripts/commit.ps1"
,
"format"
:
"vsh lint --format"
,
"lint"
:
"vsh lint"
,
"postinstall"
:
"pnpm -r run stub --if-present"
,
"postinstall"
:
"pnpm -r run stub --if-present"
,
"prepare"
:
"husky"
,
"prepare"
:
"husky"
,
"preinstall"
:
"npx only-allow pnpm"
,
"preinstall"
:
"npx only-allow pnpm"
,
"preview"
:
"turbo-run preview"
,
"publint"
:
"vsh publint"
,
"reinstall"
:
"pnpm clean --del-lock && pnpm install"
,
"reinstall"
:
"pnpm clean --del-lock && pnpm install"
,
"test:unit"
:
"vitest run --dom"
,
"test:e2e"
:
"turbo run test:e2e"
,
"update:deps"
:
"npx taze -r -w"
,
"update:deps"
:
"npx taze -r -w"
,
"version"
:
"pnpm exec changeset version && pnpm install --no-frozen-lockfile"
,
"version"
:
"pnpm exec changeset version && pnpm install --no-frozen-lockfile"
,
"catalog"
:
"pnpx codemod pnpm/catalog"
,
"catalog"
:
"pnpx codemod pnpm/catalog"
,
"venus"
:
"node packages/cli/dist/index.cjs.js"
,
"venus"
:
"node packages/cli/dist/index.cjs.js"
,
"venus:build"
:
"node scripts/build/buildSingle.js cli"
,
"venus:build"
:
"node scripts/build/buildSingle.js cli"
,
"venus:create"
:
"pnpm venus create"
,
"venus:create"
:
"pnpm venus create"
,
"venus:create:interactive"
:
"pnpm venus create"
,
"venus:ls"
:
"pnpm venus ls-templates"
,
"venus:ls"
:
"pnpm venus ls-templates"
,
"command:test"
:
"node scripts/build/commandTest.mjs"
"command:test"
:
"node scripts/build/commandTest.mjs"
},
},
...
...
packages/cli/package.json
View file @
2576916
...
@@ -35,5 +35,8 @@
...
@@ -35,5 +35,8 @@
},
},
"devDependencies"
:
{
"devDependencies"
:
{
"typescript"
:
"*"
"typescript"
:
"*"
},
"dependencies"
:
{
"@clack/prompts"
:
"catalog:"
}
}
}
}
packages/cli/src/index.ts
View file @
2576916
This diff is collapsed.
Click to expand it.
pnpm-lock.yaml
View file @
2576916
...
@@ -892,6 +892,10 @@ importers:
...
@@ -892,6 +892,10 @@ importers:
version
:
5.9.3
version
:
5.9.3
packages/cli
:
packages/cli
:
dependencies
:
'
@clack/prompts'
:
specifier
:
'
catalog:'
version
:
0.10.1
devDependencies
:
devDependencies
:
typescript
:
typescript
:
specifier
:
'
*'
specifier
:
'
*'
...
...
scripts/turbo-run/src/index.ts
View file @
2576916
...
@@ -6,7 +6,7 @@ import { run } from './run';
...
@@ -6,7 +6,7 @@ import { run } from './run';
try
{
try
{
const
turboRun
=
cac
(
'turbo-run'
);
const
turboRun
=
cac
(
'turbo-run'
);
console
.
log
(
process
.
argv
);
turboRun
turboRun
.
command
(
'[script]'
)
.
command
(
'[script]'
)
.
usage
(
`Run turbo interactively.`
)
.
usage
(
`Run turbo interactively.`
)
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment