micro-ice-vue3-ts 1.0.1 → 1.0.3

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/README.md CHANGED
@@ -1,11 +1,48 @@
1
1
  # micro-ice-app
2
2
  ## vue3+ts 版本
3
3
  ##
4
- 该框架有极限性,针对指定项目使用,并非适用大众,请知悉!
5
4
  站在巨人的肩膀上,该框架基于腾讯[wujie](https://github.com/Tencent/wujie)进行封装
6
- ## Project setup
5
+
6
+ ## 安装
7
7
  ```
8
8
  yarn add micro-ice-vue3-ts
9
9
  ```
10
+
11
+ ## 组件 Props
12
+
13
+ | 参数 | 说明 | 类型 | 默认值 |
14
+ | --- | --- | --- | --- |
15
+ | name | 应用名称,wujie 按 name 缓存应用 | string | - |
16
+ | url | 应用入口 HTML 地址 | string | - |
17
+ | width / height | 容器尺寸 | string | 100% |
18
+ | alive | 保活模式,卸载后保留沙箱 | boolean | false |
19
+ | sync | 路由同步 | boolean | false |
20
+ | props | 传递给子应用的数据(`window.$wujie.props` 读取) | object | - |
21
+ | plugins | wujie 插件(cssBeforeLoaders / jsBeforeLoaders / jsLoader 等) | array | - |
22
+ | events | 只把指定事件透传为组件的 emit 事件;不填则透传全部事件 | string[] | - |
23
+ | destroyOnUnmount | 非保活应用卸载时自动销毁沙箱 | boolean | false |
24
+ | fiber / degrade / fetch / replace / prefix / loading | 透传 wujie startApp,见 wujie 文档 | - | - |
25
+ | beforeLoad / beforeMount / afterMount / beforeUnmount / afterUnmount / activated / deactivated / loadError | 生命周期钩子,透传 wujie | function | - |
26
+
27
+ ## 事件通信
28
+
29
+ - `eventBus`(即 wujie `bus`):`$on` / `$emit` / `$off`。
30
+ - **事件总线是主应用与所有微应用全局共享的**,任何应用 `$emit('xxx')` 会触发所有监听 `xxx` 的地方——事件名务必加应用前缀避免串线,如 `iot:onLoading`、`gauge:save`。
31
+ - 组件内推荐用 `useMicroEvent(event, handler)`(卸载时自动取消订阅)代替直接 `eventBus.$on`:
32
+
33
+ ```ts
34
+ import { useMicroEvent } from 'micro-ice-vue3-ts'
35
+ useMicroEvent('iot:onLoading', (flag: boolean) => { /* ... */ })
36
+ ```
37
+
38
+ ## 注意事项
39
+
40
+ - **url 变更不会作用于已在运行的同名应用**(wujie 按 name 缓存),切换前需先 `destroyApp(name)`;
41
+ - `destroyOnUnmount` 只对非保活应用(`alive=false`)生效;保活应用由框架保留沙箱供下次激活;
42
+ - 子应用启动失败时组件会 `console.error` 并 `emit('start-error', error)`。
43
+
44
+ ## 其他
45
+ 该框架有极限性,针对指定项目使用,并非适用大众,请知悉!
46
+
10
47
  ### Customize configuration
11
48
  See [Configuration Reference](https://cli.vuejs.org/config/).
package/lib/demo.html ADDED
@@ -0,0 +1 @@
1
+ <!doctype html><meta charset="utf-8"><title>index demo</title><script src="./index.umd.js"></script><link rel="stylesheet" href="./index.css"><script>console.log(index)</script>
@@ -2990,7 +2990,8 @@ __webpack_require__.d(__webpack_exports__, {
2990
2990
  destroyApp: function() { return /* reexport */ destroyApp; },
2991
2991
  eventBus: function() { return /* reexport */ bus; },
2992
2992
  preloadApp: function() { return /* reexport */ preloadApp; },
2993
- setupApp: function() { return /* reexport */ setupApp; }
2993
+ setupApp: function() { return /* reexport */ setupApp; },
2994
+ useMicroEvent: function() { return /* reexport */ useMicroEvent; }
2994
2995
  });
2995
2996
 
2996
2997
  ;// CONCATENATED MODULE: ./node_modules/@vue/cli-service/lib/commands/build/setPublicPath.js
@@ -7731,6 +7732,7 @@ var external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObject = require("vue"
7731
7732
  ;// CONCATENATED MODULE: ./packages/MicroIceApp/index.ts
7732
7733
 
7733
7734
 
7735
+
7734
7736
  const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObject.defineComponent)({
7735
7737
  name: "MicroIceApp",
7736
7738
  props: {
@@ -7790,6 +7792,14 @@ const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObj
7790
7792
  type: Array,
7791
7793
  default: null
7792
7794
  },
7795
+ events: {
7796
+ type: Array,
7797
+ default: null
7798
+ },
7799
+ destroyOnUnmount: {
7800
+ type: Boolean,
7801
+ default: false
7802
+ },
7793
7803
  beforeLoad: {
7794
7804
  type: Function,
7795
7805
  default: null
@@ -7828,6 +7838,9 @@ const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObj
7828
7838
  }) {
7829
7839
  const MicroIceAppRef = (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObject.ref)(null);
7830
7840
  let startAppQueue = Promise.resolve();
7841
+ let startedName = "";
7842
+ let startedUrl = "";
7843
+ const subscribedEvents = [];
7831
7844
  const handleEmit = (event, ...args) => {
7832
7845
  emit(event, ...args);
7833
7846
  };
@@ -7856,52 +7869,64 @@ const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObj
7856
7869
  deactivated: props.deactivated,
7857
7870
  loadError: props.loadError
7858
7871
  });
7859
- // const iframe = window.document.querySelector<HTMLIFrameElement>(
7860
- // `iframe[name="${props.name}"]`
7861
- // );
7862
- // iframeApp.value = iframe;
7863
- // if (iframe) {
7864
- // setGlobal(iframe.contentWindow);
7865
- // }
7872
+ startedName = props.name;
7873
+ startedUrl = props.url;
7866
7874
  } catch (error) {
7867
- console.log(error);
7875
+ console.error(`[micro-ice] 应用 ${props.name} 启动失败:`, error);
7876
+ emit("start-error", error);
7868
7877
  }
7869
7878
  };
7870
7879
  const execStartApp = () => {
7871
7880
  startAppQueue = startAppQueue.then(startApp);
7872
7881
  };
7882
+ const subscribeEvents = () => {
7883
+ // 指定了 events 时只订阅这些事件,避免 $onAll 把其他应用的事件也转发进来
7884
+ if (props.events?.length) {
7885
+ props.events.forEach(event => {
7886
+ const handler = (...args) => handleEmit(event, ...args);
7887
+ subscribedEvents.push({
7888
+ event,
7889
+ handler
7890
+ });
7891
+ bus.$on(event, handler);
7892
+ });
7893
+ } else {
7894
+ bus.$onAll(handleEmit);
7895
+ }
7896
+ };
7897
+ const unsubscribeEvents = () => {
7898
+ if (props.events?.length) {
7899
+ subscribedEvents.forEach(({
7900
+ event,
7901
+ handler
7902
+ }) => bus.$off(event, handler));
7903
+ subscribedEvents.length = 0;
7904
+ } else {
7905
+ bus.$offAll(handleEmit);
7906
+ }
7907
+ };
7873
7908
  (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObject.onMounted)(() => {
7874
- bus.$onAll(handleEmit);
7909
+ subscribeEvents();
7875
7910
  execStartApp();
7876
- (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObject.watch)(() => props.name + props.url, () => execStartApp());
7911
+ (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObject.watch)(() => props.name + props.url, () => {
7912
+ // name 变化视为全新应用,直接重新启动
7913
+ if (props.name !== startedName) {
7914
+ execStartApp();
7915
+ return;
7916
+ }
7917
+ // wujie 按 name 缓存,url 变更对已在运行的应用不生效,需要显式销毁后重启
7918
+ if (props.url !== startedUrl) {
7919
+ console.warn(`[micro-ice] 应用 ${props.name} 已在运行,url 变更不会生效,请先 destroyApp("${props.name}") 后再切换`);
7920
+ }
7921
+ });
7877
7922
  });
7878
7923
  (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObject.onBeforeUnmount)(() => {
7879
- bus.$offAll(handleEmit);
7924
+ unsubscribeEvents();
7925
+ // 仅非保活应用支持卸载即销毁;保活应用应保留沙箱以便下次激活
7926
+ if (props.destroyOnUnmount && !props.alive && props.name) {
7927
+ destroyApp(props.name);
7928
+ }
7880
7929
  });
7881
- // const setGlobal = (iframeWindow: Window | null) => {
7882
- // if (!iframeWindow) return;
7883
- // const global = {
7884
- // __POWERED_BY_MICRO__: iframeWindow.__POWERED_BY_WUJIE__,
7885
- // __MICRO_PUBLIC_PATH__: iframeWindow.__WUJIE_PUBLIC_PATH__,
7886
- // __MICRO_RAW_DOCUMENT_QUERY_SELECTOR__:
7887
- // iframeWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR__,
7888
- // __MICRO_RAW_DOCUMENT_QUERY_SELECTOR_ALL__:
7889
- // iframeWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR_ALL__,
7890
- // __MICRO_RAW_WINDOW__: iframeWindow.__WUJIE_RAW_WINDOW__,
7891
- // __MICRO: iframeWindow.__WUJIE,
7892
- // __MICRO_MOUNT: () => iframeWindow.__WUJIE_MOUNT(),
7893
- // __MICRO_UNMOUNT: () => iframeWindow.__WUJIE_UNMOUNT(),
7894
- // $micro_ice: {
7895
- // bus: iframeWindow.$wujie.bus,
7896
- // shadowRoot: iframeWindow.$wujie.shadowRoot,
7897
- // props: iframeWindow.$wujie.props,
7898
- // location: iframeWindow.$wujie.location,
7899
- // },
7900
- // };
7901
- // Object.keys(global).forEach((key) => {
7902
- // (iframeWindow as any)[key] = global[key];
7903
- // });
7904
- // };
7905
7930
  return () => (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObject.h)("div", {
7906
7931
  style: {
7907
7932
  width: props.width,
@@ -7914,6 +7939,16 @@ const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObj
7914
7939
  MicroIceApp.install = function (app) {
7915
7940
  app.component("MicroIceApp", MicroIceApp);
7916
7941
  };
7942
+ /**
7943
+ * 监听微应用事件,组件卸载时自动取消订阅
7944
+ * 注意:事件总线为主应用与所有微应用全局共享,事件名请加应用前缀避免串线(如 iot:onLoading)
7945
+ */
7946
+ function useMicroEvent(event, handler) {
7947
+ bus.$on(event, handler);
7948
+ (0,external_commonjs_vue_commonjs2_vue_root_Vue_namespaceObject.onBeforeUnmount)(() => {
7949
+ bus.$off(event, handler);
7950
+ });
7951
+ }
7917
7952
  /* harmony default export */ var packages_MicroIceApp = (MicroIceApp);
7918
7953
 
7919
7954
  ;// CONCATENATED MODULE: ./packages/index.ts
package/lib/index.umd.js CHANGED
@@ -3008,7 +3008,8 @@ __webpack_require__.d(__webpack_exports__, {
3008
3008
  destroyApp: function() { return /* reexport */ destroyApp; },
3009
3009
  eventBus: function() { return /* reexport */ bus; },
3010
3010
  preloadApp: function() { return /* reexport */ preloadApp; },
3011
- setupApp: function() { return /* reexport */ setupApp; }
3011
+ setupApp: function() { return /* reexport */ setupApp; },
3012
+ useMicroEvent: function() { return /* reexport */ useMicroEvent; }
3012
3013
  });
3013
3014
 
3014
3015
  ;// CONCATENATED MODULE: ./node_modules/@vue/cli-service/lib/commands/build/setPublicPath.js
@@ -7749,6 +7750,7 @@ var external_commonjs_vue_commonjs2_vue_root_Vue_ = __webpack_require__(9274);
7749
7750
  ;// CONCATENATED MODULE: ./packages/MicroIceApp/index.ts
7750
7751
 
7751
7752
 
7753
+
7752
7754
  const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_.defineComponent)({
7753
7755
  name: "MicroIceApp",
7754
7756
  props: {
@@ -7808,6 +7810,14 @@ const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_.defineCompo
7808
7810
  type: Array,
7809
7811
  default: null
7810
7812
  },
7813
+ events: {
7814
+ type: Array,
7815
+ default: null
7816
+ },
7817
+ destroyOnUnmount: {
7818
+ type: Boolean,
7819
+ default: false
7820
+ },
7811
7821
  beforeLoad: {
7812
7822
  type: Function,
7813
7823
  default: null
@@ -7846,6 +7856,9 @@ const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_.defineCompo
7846
7856
  }) {
7847
7857
  const MicroIceAppRef = (0,external_commonjs_vue_commonjs2_vue_root_Vue_.ref)(null);
7848
7858
  let startAppQueue = Promise.resolve();
7859
+ let startedName = "";
7860
+ let startedUrl = "";
7861
+ const subscribedEvents = [];
7849
7862
  const handleEmit = (event, ...args) => {
7850
7863
  emit(event, ...args);
7851
7864
  };
@@ -7874,52 +7887,64 @@ const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_.defineCompo
7874
7887
  deactivated: props.deactivated,
7875
7888
  loadError: props.loadError
7876
7889
  });
7877
- // const iframe = window.document.querySelector<HTMLIFrameElement>(
7878
- // `iframe[name="${props.name}"]`
7879
- // );
7880
- // iframeApp.value = iframe;
7881
- // if (iframe) {
7882
- // setGlobal(iframe.contentWindow);
7883
- // }
7890
+ startedName = props.name;
7891
+ startedUrl = props.url;
7884
7892
  } catch (error) {
7885
- console.log(error);
7893
+ console.error(`[micro-ice] 应用 ${props.name} 启动失败:`, error);
7894
+ emit("start-error", error);
7886
7895
  }
7887
7896
  };
7888
7897
  const execStartApp = () => {
7889
7898
  startAppQueue = startAppQueue.then(startApp);
7890
7899
  };
7900
+ const subscribeEvents = () => {
7901
+ // 指定了 events 时只订阅这些事件,避免 $onAll 把其他应用的事件也转发进来
7902
+ if (props.events?.length) {
7903
+ props.events.forEach(event => {
7904
+ const handler = (...args) => handleEmit(event, ...args);
7905
+ subscribedEvents.push({
7906
+ event,
7907
+ handler
7908
+ });
7909
+ bus.$on(event, handler);
7910
+ });
7911
+ } else {
7912
+ bus.$onAll(handleEmit);
7913
+ }
7914
+ };
7915
+ const unsubscribeEvents = () => {
7916
+ if (props.events?.length) {
7917
+ subscribedEvents.forEach(({
7918
+ event,
7919
+ handler
7920
+ }) => bus.$off(event, handler));
7921
+ subscribedEvents.length = 0;
7922
+ } else {
7923
+ bus.$offAll(handleEmit);
7924
+ }
7925
+ };
7891
7926
  (0,external_commonjs_vue_commonjs2_vue_root_Vue_.onMounted)(() => {
7892
- bus.$onAll(handleEmit);
7927
+ subscribeEvents();
7893
7928
  execStartApp();
7894
- (0,external_commonjs_vue_commonjs2_vue_root_Vue_.watch)(() => props.name + props.url, () => execStartApp());
7929
+ (0,external_commonjs_vue_commonjs2_vue_root_Vue_.watch)(() => props.name + props.url, () => {
7930
+ // name 变化视为全新应用,直接重新启动
7931
+ if (props.name !== startedName) {
7932
+ execStartApp();
7933
+ return;
7934
+ }
7935
+ // wujie 按 name 缓存,url 变更对已在运行的应用不生效,需要显式销毁后重启
7936
+ if (props.url !== startedUrl) {
7937
+ console.warn(`[micro-ice] 应用 ${props.name} 已在运行,url 变更不会生效,请先 destroyApp("${props.name}") 后再切换`);
7938
+ }
7939
+ });
7895
7940
  });
7896
7941
  (0,external_commonjs_vue_commonjs2_vue_root_Vue_.onBeforeUnmount)(() => {
7897
- bus.$offAll(handleEmit);
7942
+ unsubscribeEvents();
7943
+ // 仅非保活应用支持卸载即销毁;保活应用应保留沙箱以便下次激活
7944
+ if (props.destroyOnUnmount && !props.alive && props.name) {
7945
+ destroyApp(props.name);
7946
+ }
7898
7947
  });
7899
- // const setGlobal = (iframeWindow: Window | null) => {
7900
- // if (!iframeWindow) return;
7901
- // const global = {
7902
- // __POWERED_BY_MICRO__: iframeWindow.__POWERED_BY_WUJIE__,
7903
- // __MICRO_PUBLIC_PATH__: iframeWindow.__WUJIE_PUBLIC_PATH__,
7904
- // __MICRO_RAW_DOCUMENT_QUERY_SELECTOR__:
7905
- // iframeWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR__,
7906
- // __MICRO_RAW_DOCUMENT_QUERY_SELECTOR_ALL__:
7907
- // iframeWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR_ALL__,
7908
- // __MICRO_RAW_WINDOW__: iframeWindow.__WUJIE_RAW_WINDOW__,
7909
- // __MICRO: iframeWindow.__WUJIE,
7910
- // __MICRO_MOUNT: () => iframeWindow.__WUJIE_MOUNT(),
7911
- // __MICRO_UNMOUNT: () => iframeWindow.__WUJIE_UNMOUNT(),
7912
- // $micro_ice: {
7913
- // bus: iframeWindow.$wujie.bus,
7914
- // shadowRoot: iframeWindow.$wujie.shadowRoot,
7915
- // props: iframeWindow.$wujie.props,
7916
- // location: iframeWindow.$wujie.location,
7917
- // },
7918
- // };
7919
- // Object.keys(global).forEach((key) => {
7920
- // (iframeWindow as any)[key] = global[key];
7921
- // });
7922
- // };
7923
7948
  return () => (0,external_commonjs_vue_commonjs2_vue_root_Vue_.h)("div", {
7924
7949
  style: {
7925
7950
  width: props.width,
@@ -7932,6 +7957,16 @@ const MicroIceApp = (0,external_commonjs_vue_commonjs2_vue_root_Vue_.defineCompo
7932
7957
  MicroIceApp.install = function (app) {
7933
7958
  app.component("MicroIceApp", MicroIceApp);
7934
7959
  };
7960
+ /**
7961
+ * 监听微应用事件,组件卸载时自动取消订阅
7962
+ * 注意:事件总线为主应用与所有微应用全局共享,事件名请加应用前缀避免串线(如 iot:onLoading)
7963
+ */
7964
+ function useMicroEvent(event, handler) {
7965
+ bus.$on(event, handler);
7966
+ (0,external_commonjs_vue_commonjs2_vue_root_Vue_.onBeforeUnmount)(() => {
7967
+ bus.$off(event, handler);
7968
+ });
7969
+ }
7935
7970
  /* harmony default export */ var packages_MicroIceApp = (MicroIceApp);
7936
7971
 
7937
7972
  ;// CONCATENATED MODULE: ./packages/index.ts