@hhfenpm/micro-app 1.0.1 → 1.0.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.
package/dist/index.esm.js CHANGED
@@ -1,8 +1,3 @@
1
- /**
2
- * 微前端通信桥接
3
- * 支持父子应用之间的方法调用和通信
4
- */
5
-
6
1
  const BRIDGE$1 = {
7
2
  INVOKE: 'bridge-invoke',
8
3
  CALLBACK: 'bridge-callback',
@@ -24,27 +19,15 @@ const safeClone = value => {
24
19
  }
25
20
  };
26
21
 
27
- /**
28
- * 初始化通信桥接
29
- * @param {Object} options - 配置选项
30
- * @param {boolean} options.isParent - 是否为父应用,默认 false
31
- * @param {Object} options.vm - Vue 实例(可选)
32
- * @param {string} options.iframeSelector - iframe 选择器,默认 '#microApp'
33
- * @param {Object} options.handlers - 处理器对象(父应用需要提供)
34
- * @returns {void}
35
- */
36
22
  const initBridge = ({
37
23
  isParent = false,
38
24
  vm,
39
25
  iframeSelector = '#microApp',
40
26
  handlers = {},
41
27
  } = {}) => {
42
- // 父应用使用传入的 handlers,子应用为空对象
43
28
  const finalHandlers = isParent ? handlers : Object.create(null);
44
-
45
29
  const pending = Object.create(null);
46
30
 
47
- // 子应用:创建 $base 代理对象,用于调用父应用方法
48
31
  if (!isParent && vm) {
49
32
  vm.$base = new Proxy(
50
33
  {},
@@ -76,7 +59,6 @@ const initBridge = ({
76
59
  vm.$baseReady = true;
77
60
  }
78
61
 
79
- // 处理来自子应用的调用请求(父应用)
80
62
  const handleInvoke = e => {
81
63
  const { action, params, callbackId } = e.data || {};
82
64
  if (!action || !callbackId) return
@@ -107,7 +89,6 @@ const initBridge = ({
107
89
  }
108
90
  };
109
91
 
110
- // 处理来自父应用的响应(子应用)
111
92
  const handleCallback = e => {
112
93
  const { callbackId, result } = e.data || {};
113
94
  if (!callbackId || !pending[callbackId]) return
@@ -116,7 +97,6 @@ const initBridge = ({
116
97
  delete pending[callbackId];
117
98
  };
118
99
 
119
- // 监听消息
120
100
  window.addEventListener('message', e => {
121
101
  if (e.origin !== window.location.origin) return
122
102
 
@@ -182,7 +162,6 @@ const KEYS = [
182
162
  'get_version',
183
163
  ];
184
164
 
185
- /** @param {Object} [electron] 宿主传入的 electron 模块,无或 require 失败时传 {} */
186
165
  function createCs(electron = {}) {
187
166
  const base = Object.fromEntries(
188
167
  KEYS.map(k => [k, typeof electron[k] === 'function' ? electron[k] : noop])
@@ -197,9 +176,6 @@ function createCs(electron = {}) {
197
176
  }
198
177
  }
199
178
 
200
- /**
201
- * @param {() => Object} [getElectron] 返回 electron 模块,如 () => require('@/utils/electron')
202
- */
203
179
  function createRegisterHandlers(getElectron) {
204
180
  let electron = {};
205
181
  try {
@@ -212,18 +188,6 @@ function createRegisterHandlers(getElectron) {
212
188
  })
213
189
  }
214
190
 
215
- /**
216
- * 微前端核心功能
217
- * 提供模块路由映射、URL 生成、部署检测等功能
218
- */
219
-
220
- /**
221
- * 创建微前端核心功能
222
- * @param {Object} options - 配置选项
223
- * @param {Function} options.modelMap - 模块路由映射函数,返回 { [moduleName]: string[] }
224
- * @param {Array<string>|Function} options.enabledModules - 启用的模块列表或获取模块列表的函数
225
- * @returns {Object} 核心功能对象
226
- */
227
191
  function createMicroAppCore({
228
192
  modelMap,
229
193
  enabledModules,
@@ -234,10 +198,6 @@ function createMicroAppCore({
234
198
 
235
199
  const MAP = modelMap();
236
200
 
237
- /**
238
- * 获取启用的模块列表
239
- * @returns {Array<string>|null}
240
- */
241
201
  const getEnabledModules = () => {
242
202
  if (Array.isArray(enabledModules)) {
243
203
  return enabledModules
@@ -245,18 +205,12 @@ function createMicroAppCore({
245
205
  if (typeof enabledModules === 'function') {
246
206
  return enabledModules()
247
207
  }
248
- // 默认从 window.GLOBAL_CONFIG.microApp 获取
249
208
  if (typeof window !== 'undefined' && window.GLOBAL_CONFIG) {
250
209
  return window.GLOBAL_CONFIG.microApp || null
251
210
  }
252
211
  return null
253
212
  };
254
213
 
255
- /**
256
- * 根据路径获取对应的微前端模块名
257
- * @param {string} path - 路由路径
258
- * @returns {string|null} 模块名,如果不在任何模块中则返回 null
259
- */
260
214
  const microAppModule = path => {
261
215
  const value = path.split('?')[0];
262
216
  const key = Object.keys(MAP).find(key => MAP[key].includes(value));
@@ -264,22 +218,12 @@ function createMicroAppCore({
264
218
  return DATA?.includes(key) ? key : null
265
219
  };
266
220
 
267
- /**
268
- * 生成微前端应用的完整 URL
269
- * @param {string} path - 路由路径
270
- * @returns {string} 完整的 URL
271
- */
272
221
  const microAppSrc = path => {
273
222
  const module = microAppModule(path);
274
223
  const base = window.location.href.split('#')[0];
275
224
  return `${base}${module ? `${module}/` : ''}#${path}`
276
225
  };
277
226
 
278
- /**
279
- * 检测微前端模块是否已部署
280
- * @param {string} module - 模块名
281
- * @returns {Promise<boolean>} 是否已部署
282
- */
283
227
  const microAppDeployed = async module => {
284
228
  try {
285
229
  const result = await fetch(`/${module}`, {
@@ -299,23 +243,6 @@ function createMicroAppCore({
299
243
  }
300
244
  }
301
245
 
302
- /**
303
- * 创建默认的微前端核心功能实例
304
- * 使用 window.GLOBAL_CONFIG.microApp 作为配置源
305
- * @param {Function} modelMap - 模块路由映射函数
306
- * @returns {Object} 核心功能对象
307
- */
308
- function createDefaultMicroAppCore(modelMap) {
309
- return createMicroAppCore({
310
- modelMap,
311
- })
312
- }
313
-
314
- /**
315
- * Vuex 状态同步插件
316
- * 用于在父子应用之间同步 Vuex store 状态
317
- */
318
-
319
246
  const MESSAGE = {
320
247
  SYNC_FROM_CHILD: 'sync-base-from-child',
321
248
  SYNC_FROM_PARENT: 'sync-base-from-parent',
@@ -369,13 +296,6 @@ const syncRouteFromChild = fullPath => {
369
296
  }
370
297
  };
371
298
 
372
- /**
373
- * 创建状态同步插件
374
- * @param {Object} options - 配置选项
375
- * @param {boolean} options.isParent - 是否为父应用,默认 false
376
- * @param {string} options.iframeSelector - iframe 选择器,默认 '#microApp'
377
- * @returns {Function} Vuex 插件函数
378
- */
379
299
  function baseSyncPlugin({
380
300
  isParent = false,
381
301
  iframeSelector = '#microApp',
@@ -383,7 +303,6 @@ function baseSyncPlugin({
383
303
  return store => {
384
304
  let latestBaseState = store.state.base;
385
305
 
386
- // 监听 base 状态变化,自动同步
387
306
  store.watch(
388
307
  state => state.base,
389
308
  newBase => {
@@ -403,7 +322,6 @@ function baseSyncPlugin({
403
322
  { deep: true }
404
323
  );
405
324
 
406
- // 附加路由同步功能(子应用使用)
407
325
  store.attachRouterSync = router => {
408
326
  if (!router || isParent) return
409
327
  router.afterEach(to => {
@@ -423,7 +341,6 @@ function baseSyncPlugin({
423
341
  });
424
342
  };
425
343
 
426
- // 调用微前端生命周期(父应用使用)
427
344
  store.callMicroAppLifeCycle = (type, payload) => {
428
345
  if (!isParent) return
429
346
  if (!isLifeCycleMessage(type)) return
@@ -434,30 +351,25 @@ function baseSyncPlugin({
434
351
  });
435
352
  };
436
353
 
437
- // 处理消息
438
354
  const handleMessage = event => {
439
355
  if (event.origin !== window.location.origin) return
440
356
  const { type, payload, fullPath } = event.data || {};
441
357
 
442
- // 子应用处理生命周期消息
443
358
  if (!isParent && isLifeCycleMessage(type)) {
444
359
  window.__MICRO_APP_LIFECYCLE__?.[type]?.(payload);
445
360
  return
446
361
  }
447
362
 
448
- // 父应用处理来自子应用的状态同步
449
363
  if (isParent && type === MESSAGE.SYNC_FROM_CHILD) {
450
364
  store.commit('base/SYNC_STATE', payload);
451
365
  return
452
366
  }
453
367
 
454
- // 子应用处理来自父应用的状态同步
455
368
  if (!isParent && type === MESSAGE.SYNC_FROM_PARENT) {
456
369
  store.commit('base/SYNC_STATE', payload);
457
370
  return
458
371
  }
459
372
 
460
- // 父应用响应子应用的状态请求
461
373
  if (isParent && type === MESSAGE.REQUEST_FROM_CHILD) {
462
374
  postToChild(iframeSelector, {
463
375
  type: MESSAGE.SYNC_FROM_PARENT,
@@ -466,13 +378,11 @@ function baseSyncPlugin({
466
378
  return
467
379
  }
468
380
 
469
- // 父应用处理子应用的路由变化
470
381
  if (isParent && type === MESSAGE.CHILD_ROUTE_CHANGE) {
471
382
  syncRouteFromChild(fullPath);
472
383
  return
473
384
  }
474
385
 
475
- // 父应用转发其他消息到父窗口
476
386
  if (isParent && !isInternalMessage(type) && !isBridgeMessage(type)) {
477
387
  window.parent?.postMessage(event.data || {}, '*');
478
388
  }
@@ -480,23 +390,12 @@ function baseSyncPlugin({
480
390
 
481
391
  window.addEventListener('message', handleMessage);
482
392
 
483
- // 子应用初始化时请求父应用状态
484
393
  if (!isParent && window.parent) {
485
394
  postToParent({ type: MESSAGE.REQUEST_FROM_CHILD });
486
395
  }
487
396
  }
488
397
  }
489
398
 
490
- /**
491
- * @hhfenpm/micro-app
492
- * 微前端通信桥接和状态同步工具
493
- */
494
-
495
-
496
- var index = {
497
- initBridge,
498
- createDefaultMicroAppCore,
499
- baseSyncPlugin,
500
- };
399
+ var index = { initBridge, baseSyncPlugin };
501
400
 
502
- export { baseSyncPlugin, createDefaultMicroAppCore, createMicroAppCore, createRegisterHandlers, index as default, initBridge };
401
+ export { baseSyncPlugin, createMicroAppCore, createRegisterHandlers, index as default, initBridge };
package/dist/index.js CHANGED
@@ -2,11 +2,6 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- /**
6
- * 微前端通信桥接
7
- * 支持父子应用之间的方法调用和通信
8
- */
9
-
10
5
  const BRIDGE$1 = {
11
6
  INVOKE: 'bridge-invoke',
12
7
  CALLBACK: 'bridge-callback',
@@ -28,27 +23,15 @@ const safeClone = value => {
28
23
  }
29
24
  };
30
25
 
31
- /**
32
- * 初始化通信桥接
33
- * @param {Object} options - 配置选项
34
- * @param {boolean} options.isParent - 是否为父应用,默认 false
35
- * @param {Object} options.vm - Vue 实例(可选)
36
- * @param {string} options.iframeSelector - iframe 选择器,默认 '#microApp'
37
- * @param {Object} options.handlers - 处理器对象(父应用需要提供)
38
- * @returns {void}
39
- */
40
26
  const initBridge = ({
41
27
  isParent = false,
42
28
  vm,
43
29
  iframeSelector = '#microApp',
44
30
  handlers = {},
45
31
  } = {}) => {
46
- // 父应用使用传入的 handlers,子应用为空对象
47
32
  const finalHandlers = isParent ? handlers : Object.create(null);
48
-
49
33
  const pending = Object.create(null);
50
34
 
51
- // 子应用:创建 $base 代理对象,用于调用父应用方法
52
35
  if (!isParent && vm) {
53
36
  vm.$base = new Proxy(
54
37
  {},
@@ -80,7 +63,6 @@ const initBridge = ({
80
63
  vm.$baseReady = true;
81
64
  }
82
65
 
83
- // 处理来自子应用的调用请求(父应用)
84
66
  const handleInvoke = e => {
85
67
  const { action, params, callbackId } = e.data || {};
86
68
  if (!action || !callbackId) return
@@ -111,7 +93,6 @@ const initBridge = ({
111
93
  }
112
94
  };
113
95
 
114
- // 处理来自父应用的响应(子应用)
115
96
  const handleCallback = e => {
116
97
  const { callbackId, result } = e.data || {};
117
98
  if (!callbackId || !pending[callbackId]) return
@@ -120,7 +101,6 @@ const initBridge = ({
120
101
  delete pending[callbackId];
121
102
  };
122
103
 
123
- // 监听消息
124
104
  window.addEventListener('message', e => {
125
105
  if (e.origin !== window.location.origin) return
126
106
 
@@ -186,7 +166,6 @@ const KEYS = [
186
166
  'get_version',
187
167
  ];
188
168
 
189
- /** @param {Object} [electron] 宿主传入的 electron 模块,无或 require 失败时传 {} */
190
169
  function createCs(electron = {}) {
191
170
  const base = Object.fromEntries(
192
171
  KEYS.map(k => [k, typeof electron[k] === 'function' ? electron[k] : noop])
@@ -201,9 +180,6 @@ function createCs(electron = {}) {
201
180
  }
202
181
  }
203
182
 
204
- /**
205
- * @param {() => Object} [getElectron] 返回 electron 模块,如 () => require('@/utils/electron')
206
- */
207
183
  function createRegisterHandlers(getElectron) {
208
184
  let electron = {};
209
185
  try {
@@ -216,18 +192,6 @@ function createRegisterHandlers(getElectron) {
216
192
  })
217
193
  }
218
194
 
219
- /**
220
- * 微前端核心功能
221
- * 提供模块路由映射、URL 生成、部署检测等功能
222
- */
223
-
224
- /**
225
- * 创建微前端核心功能
226
- * @param {Object} options - 配置选项
227
- * @param {Function} options.modelMap - 模块路由映射函数,返回 { [moduleName]: string[] }
228
- * @param {Array<string>|Function} options.enabledModules - 启用的模块列表或获取模块列表的函数
229
- * @returns {Object} 核心功能对象
230
- */
231
195
  function createMicroAppCore({
232
196
  modelMap,
233
197
  enabledModules,
@@ -238,10 +202,6 @@ function createMicroAppCore({
238
202
 
239
203
  const MAP = modelMap();
240
204
 
241
- /**
242
- * 获取启用的模块列表
243
- * @returns {Array<string>|null}
244
- */
245
205
  const getEnabledModules = () => {
246
206
  if (Array.isArray(enabledModules)) {
247
207
  return enabledModules
@@ -249,18 +209,12 @@ function createMicroAppCore({
249
209
  if (typeof enabledModules === 'function') {
250
210
  return enabledModules()
251
211
  }
252
- // 默认从 window.GLOBAL_CONFIG.microApp 获取
253
212
  if (typeof window !== 'undefined' && window.GLOBAL_CONFIG) {
254
213
  return window.GLOBAL_CONFIG.microApp || null
255
214
  }
256
215
  return null
257
216
  };
258
217
 
259
- /**
260
- * 根据路径获取对应的微前端模块名
261
- * @param {string} path - 路由路径
262
- * @returns {string|null} 模块名,如果不在任何模块中则返回 null
263
- */
264
218
  const microAppModule = path => {
265
219
  const value = path.split('?')[0];
266
220
  const key = Object.keys(MAP).find(key => MAP[key].includes(value));
@@ -268,22 +222,12 @@ function createMicroAppCore({
268
222
  return DATA?.includes(key) ? key : null
269
223
  };
270
224
 
271
- /**
272
- * 生成微前端应用的完整 URL
273
- * @param {string} path - 路由路径
274
- * @returns {string} 完整的 URL
275
- */
276
225
  const microAppSrc = path => {
277
226
  const module = microAppModule(path);
278
227
  const base = window.location.href.split('#')[0];
279
228
  return `${base}${module ? `${module}/` : ''}#${path}`
280
229
  };
281
230
 
282
- /**
283
- * 检测微前端模块是否已部署
284
- * @param {string} module - 模块名
285
- * @returns {Promise<boolean>} 是否已部署
286
- */
287
231
  const microAppDeployed = async module => {
288
232
  try {
289
233
  const result = await fetch(`/${module}`, {
@@ -303,23 +247,6 @@ function createMicroAppCore({
303
247
  }
304
248
  }
305
249
 
306
- /**
307
- * 创建默认的微前端核心功能实例
308
- * 使用 window.GLOBAL_CONFIG.microApp 作为配置源
309
- * @param {Function} modelMap - 模块路由映射函数
310
- * @returns {Object} 核心功能对象
311
- */
312
- function createDefaultMicroAppCore(modelMap) {
313
- return createMicroAppCore({
314
- modelMap,
315
- })
316
- }
317
-
318
- /**
319
- * Vuex 状态同步插件
320
- * 用于在父子应用之间同步 Vuex store 状态
321
- */
322
-
323
250
  const MESSAGE = {
324
251
  SYNC_FROM_CHILD: 'sync-base-from-child',
325
252
  SYNC_FROM_PARENT: 'sync-base-from-parent',
@@ -373,13 +300,6 @@ const syncRouteFromChild = fullPath => {
373
300
  }
374
301
  };
375
302
 
376
- /**
377
- * 创建状态同步插件
378
- * @param {Object} options - 配置选项
379
- * @param {boolean} options.isParent - 是否为父应用,默认 false
380
- * @param {string} options.iframeSelector - iframe 选择器,默认 '#microApp'
381
- * @returns {Function} Vuex 插件函数
382
- */
383
303
  function baseSyncPlugin({
384
304
  isParent = false,
385
305
  iframeSelector = '#microApp',
@@ -387,7 +307,6 @@ function baseSyncPlugin({
387
307
  return store => {
388
308
  let latestBaseState = store.state.base;
389
309
 
390
- // 监听 base 状态变化,自动同步
391
310
  store.watch(
392
311
  state => state.base,
393
312
  newBase => {
@@ -407,7 +326,6 @@ function baseSyncPlugin({
407
326
  { deep: true }
408
327
  );
409
328
 
410
- // 附加路由同步功能(子应用使用)
411
329
  store.attachRouterSync = router => {
412
330
  if (!router || isParent) return
413
331
  router.afterEach(to => {
@@ -427,7 +345,6 @@ function baseSyncPlugin({
427
345
  });
428
346
  };
429
347
 
430
- // 调用微前端生命周期(父应用使用)
431
348
  store.callMicroAppLifeCycle = (type, payload) => {
432
349
  if (!isParent) return
433
350
  if (!isLifeCycleMessage(type)) return
@@ -438,30 +355,25 @@ function baseSyncPlugin({
438
355
  });
439
356
  };
440
357
 
441
- // 处理消息
442
358
  const handleMessage = event => {
443
359
  if (event.origin !== window.location.origin) return
444
360
  const { type, payload, fullPath } = event.data || {};
445
361
 
446
- // 子应用处理生命周期消息
447
362
  if (!isParent && isLifeCycleMessage(type)) {
448
363
  window.__MICRO_APP_LIFECYCLE__?.[type]?.(payload);
449
364
  return
450
365
  }
451
366
 
452
- // 父应用处理来自子应用的状态同步
453
367
  if (isParent && type === MESSAGE.SYNC_FROM_CHILD) {
454
368
  store.commit('base/SYNC_STATE', payload);
455
369
  return
456
370
  }
457
371
 
458
- // 子应用处理来自父应用的状态同步
459
372
  if (!isParent && type === MESSAGE.SYNC_FROM_PARENT) {
460
373
  store.commit('base/SYNC_STATE', payload);
461
374
  return
462
375
  }
463
376
 
464
- // 父应用响应子应用的状态请求
465
377
  if (isParent && type === MESSAGE.REQUEST_FROM_CHILD) {
466
378
  postToChild(iframeSelector, {
467
379
  type: MESSAGE.SYNC_FROM_PARENT,
@@ -470,13 +382,11 @@ function baseSyncPlugin({
470
382
  return
471
383
  }
472
384
 
473
- // 父应用处理子应用的路由变化
474
385
  if (isParent && type === MESSAGE.CHILD_ROUTE_CHANGE) {
475
386
  syncRouteFromChild(fullPath);
476
387
  return
477
388
  }
478
389
 
479
- // 父应用转发其他消息到父窗口
480
390
  if (isParent && !isInternalMessage(type) && !isBridgeMessage(type)) {
481
391
  window.parent?.postMessage(event.data || {}, '*');
482
392
  }
@@ -484,27 +394,15 @@ function baseSyncPlugin({
484
394
 
485
395
  window.addEventListener('message', handleMessage);
486
396
 
487
- // 子应用初始化时请求父应用状态
488
397
  if (!isParent && window.parent) {
489
398
  postToParent({ type: MESSAGE.REQUEST_FROM_CHILD });
490
399
  }
491
400
  }
492
401
  }
493
402
 
494
- /**
495
- * @hhfenpm/micro-app
496
- * 微前端通信桥接和状态同步工具
497
- */
498
-
499
-
500
- var index = {
501
- initBridge,
502
- createDefaultMicroAppCore,
503
- baseSyncPlugin,
504
- };
403
+ var index = { initBridge, baseSyncPlugin };
505
404
 
506
405
  exports.baseSyncPlugin = baseSyncPlugin;
507
- exports.createDefaultMicroAppCore = createDefaultMicroAppCore;
508
406
  exports.createMicroAppCore = createMicroAppCore;
509
407
  exports.createRegisterHandlers = createRegisterHandlers;
510
408
  exports.default = index;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hhfenpm/micro-app",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "微前端通信桥接和状态同步工具,支持父子应用通信、状态同步、生命周期管理",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",