@lvetechs/micro-app 1.1.0 → 1.1.2

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.
@@ -0,0 +1,138 @@
1
+ /**
2
+ * ============================================
3
+ * 微应用组件 - Vue 版本
4
+ * ============================================
5
+ *
6
+ * 轻量级微前端框架 - Vue 集成
7
+ */
8
+
9
+ <script setup lang="ts">
10
+ import { ref, onMounted, onUnmounted, computed, watch, nextTick } from 'vue'
11
+ import { createMicroApp } from '../core/MicroAppManager'
12
+ import type { SubAppConfig, MicroAppStatus, MicroAppService } from '../types'
13
+
14
+ // Props 定义
15
+ interface Props {
16
+ /** 子应用配置数组 */
17
+ apps: SubAppConfig[]
18
+ /** 当前激活的应用名称(只渲染一个应用) */
19
+ activeApp?: string
20
+ /** 错误回调 */
21
+ onError?: (error: Error, appName?: string) => void
22
+ /** 状态变化回调 */
23
+ onStatusChange?: (name: string, status: MicroAppStatus) => void
24
+ /** 加载完成回调 */
25
+ onLoad?: (appName: string) => void
26
+ /** 自定义加载中 UI */
27
+ loading?: any
28
+ /** 自定义错误 UI */
29
+ errorFallback?: any
30
+ }
31
+
32
+ const props = withDefaults(defineProps<Props>(), {
33
+ activeApp: '',
34
+ })
35
+
36
+ // 记录当前已启动的应用
37
+ const startedAppsRef = ref<Set<string>>(new Set())
38
+
39
+ // 微应用服务实例
40
+ let service: MicroAppService | null = null
41
+
42
+ // 初始化微应用服务
43
+ onMounted(() => {
44
+ service = createMicroApp({
45
+ apps: props.apps,
46
+ onError: props.onError,
47
+ onStatusChange: (name, status) => {
48
+ props.onStatusChange?.(name, status)
49
+ },
50
+ onLoad: props.onLoad,
51
+ })
52
+
53
+ // 初始启动 activeApp
54
+ if (props.activeApp) {
55
+ startApp(props.activeApp)
56
+ }
57
+ })
58
+
59
+ // 清理
60
+ onUnmounted(() => {
61
+ // 停止所有已启动的应用
62
+ startedAppsRef.value.forEach(appName => {
63
+ service?.stopApp(appName)
64
+ })
65
+ })
66
+
67
+ // 监听 activeApp 变化
68
+ watch(() => props.activeApp, async (newApp, oldApp) => {
69
+ if (!service) return
70
+
71
+ // 停止旧应用
72
+ if (oldApp && oldApp !== newApp && startedAppsRef.value.has(oldApp)) {
73
+ service.stopApp(oldApp)
74
+ startedAppsRef.value.delete(oldApp)
75
+ }
76
+
77
+ // 启动新应用
78
+ if (newApp && newApp !== oldApp) {
79
+ await startApp(newApp)
80
+ }
81
+ })
82
+
83
+ // 启动应用(等待 DOM 就绪)
84
+ async function startApp(appName: string) {
85
+ if (!service) return
86
+
87
+ const appConfig = props.apps.find(app => app.name === appName)
88
+ if (!appConfig) return
89
+
90
+ // 等待 DOM 渲染完成
91
+ await nextTick()
92
+
93
+ const tryStartApp = () => {
94
+ const containerElement = document.querySelector(appConfig.container)
95
+ if (!containerElement) {
96
+ requestAnimationFrame(tryStartApp)
97
+ return
98
+ }
99
+
100
+ if (!startedAppsRef.value.has(appName)) {
101
+ service?.startApp(appName)
102
+ startedAppsRef.value.add(appName)
103
+ }
104
+ }
105
+
106
+ requestAnimationFrame(tryStartApp)
107
+ }
108
+
109
+ // 获取当前激活的应用配置
110
+ const activeAppConfig = computed(() => {
111
+ if (!props.activeApp) return null
112
+ return props.apps.find(app => app.name === props.activeApp)
113
+ })
114
+ </script>
115
+
116
+ <template>
117
+ <div class="micro-app-wrapper">
118
+ <!-- 渲染激活的应用容器 -->
119
+ <div
120
+ v-if="activeAppConfig"
121
+ :id="activeAppConfig.container.replace('#', '')"
122
+ class="micro-app-container"
123
+ :data-micro-app="activeAppConfig.name"
124
+ />
125
+ </div>
126
+ </template>
127
+
128
+ <style scoped>
129
+ .micro-app-wrapper {
130
+ width: 100%;
131
+ height: 100%;
132
+ }
133
+
134
+ .micro-app-container {
135
+ width: 100%;
136
+ height: 100%;
137
+ }
138
+ </style>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvetechs/micro-app",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "轻量级微前端框架 - 支持函数式和组件式实例化",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -64,7 +64,7 @@
64
64
  "license": "MIT",
65
65
  "scripts": {
66
66
  "dev": "vite",
67
- "build": "tsc && vite build",
67
+ "build": "tsc && vite build && node scripts/postbuild.mjs",
68
68
  "preview": "vite preview",
69
69
  "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
70
70
  }