@xingtukeji/micro 1.1.11 → 1.1.13

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
@@ -48,3 +48,8 @@ microInit({
48
48
  ```javascript
49
49
  http://192.168.100.127:8000/console/?__XT_MICRO_microtest2=http://localhost:5173/?test=123#/page2#/micro/microtest2
50
50
  ```
51
+
52
+ ```javascript
53
+ window.__XT_MICRO_VERSION; //获取微前端框架版本
54
+ window.__XT_MICRO_APP; //获取微应用信息,包括主应用的 location 信息
55
+ ```
package/dist/index.js CHANGED
@@ -1,41 +1,43 @@
1
1
  (function(){"use strict";try{if(typeof document!="undefined"){var e=document.createElement("style");e.appendChild(document.createTextNode('.xt-micro{width:100%;height:100%;overflow:hidden;background-color:transparent}.xt-micro:before{content:"加载中";position:absolute;inset:0;display:flex;justify-content:center;align-items:center;z-index:-1}.micro-iframe{width:100%;height:100%;border:none;margin:0;padding:0}.micro-iframe:after{content:"加载中";position:absolute;inset:0;display:flex;justify-content:center;align-items:center;background:#fff}')),document.head.appendChild(e)}}catch(t){console.error("vite-plugin-css-injected-by-js",t)}})();
2
- var L = Object.defineProperty, v = Object.defineProperties;
2
+ var T = Object.defineProperty, v = Object.defineProperties;
3
3
  var b = Object.getOwnPropertyDescriptors;
4
- var M = Object.getOwnPropertySymbols;
5
- var T = Object.prototype.hasOwnProperty, x = Object.prototype.propertyIsEnumerable;
6
- var _ = (e, t, n) => t in e ? L(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, d = (e, t) => {
4
+ var O = Object.getOwnPropertySymbols;
5
+ var x = Object.prototype.hasOwnProperty, $ = Object.prototype.propertyIsEnumerable;
6
+ var g = (e, t, n) => t in e ? T(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, d = (e, t) => {
7
7
  for (var n in t || (t = {}))
8
- T.call(t, n) && _(e, n, t[n]);
9
- if (M)
10
- for (var n of M(t))
11
- x.call(t, n) && _(e, n, t[n]);
8
+ x.call(t, n) && g(e, n, t[n]);
9
+ if (O)
10
+ for (var n of O(t))
11
+ $.call(t, n) && g(e, n, t[n]);
12
12
  return e;
13
- }, f = (e, t) => v(e, b(t));
14
- var m = (e, t, n) => (_(e, typeof t != "symbol" ? t + "" : t, n), n);
15
- var g = (e, t, n) => new Promise((r, o) => {
16
- var a = (l) => {
13
+ }, w = (e, t) => v(e, b(t));
14
+ var m = (e, t, n) => (g(e, typeof t != "symbol" ? t + "" : t, n), n);
15
+ var u = (e, t, n) => new Promise((o, r) => {
16
+ var a = (i) => {
17
17
  try {
18
- i(n.next(l));
19
- } catch (h) {
20
- o(h);
18
+ l(n.next(i));
19
+ } catch (p) {
20
+ r(p);
21
21
  }
22
- }, s = (l) => {
22
+ }, c = (i) => {
23
23
  try {
24
- i(n.throw(l));
25
- } catch (h) {
26
- o(h);
24
+ l(n.throw(i));
25
+ } catch (p) {
26
+ r(p);
27
27
  }
28
- }, i = (l) => l.done ? r(l.value) : Promise.resolve(l.value).then(a, s);
29
- i((n = n.apply(e, t)).next());
28
+ }, l = (i) => i.done ? o(i.value) : Promise.resolve(i.value).then(a, c);
29
+ l((n = n.apply(e, t)).next());
30
30
  });
31
- import { ref as P, defineComponent as $, watch as X, onMounted as k, onUnmounted as H, openBlock as N, createElementBlock as j, mergeProps as B } from "vue";
32
- const c = {
31
+ import { ref as E, defineComponent as X, watch as k, onMounted as H, onUnmounted as N, openBlock as j, createElementBlock as V, mergeProps as B } from "vue";
32
+ const s = {
33
33
  KEY: "$xtm",
34
34
  // 父应用注入子应用实例的key
35
35
  IS_MICRO: "__XT_MICRO",
36
36
  // 是否在微应用中
37
37
  MICRO_NAME: "__XT_MICRO_NAME",
38
38
  // 微应用名称
39
+ MICRO_TYPE: "__XT_MICRO_TYPE",
40
+ // 微应用类型 'parent' | 'child'
39
41
  MICRO_TOKEN: "__XT_MICRO_TOKEN",
40
42
  // 微应用token
41
43
  MICRO_APP: "__XT_MICRO_APP",
@@ -46,47 +48,47 @@ const c = {
46
48
  // 微应用在 url 参数中的前缀
47
49
  MICRO_VERSION: "__XT_MICRO_VERSION"
48
50
  // 微应用版本
49
- }, u = "1.1.11";
50
- function p(e, t = window.location.href) {
51
+ }, h = "1.1.12";
52
+ function f(e, t = window.location.href) {
51
53
  e = e.replace(/[\[\]]/g, "\\$&");
52
- var n = new RegExp("[?&]" + e + "(=([^&#]*)|&|#|$)"), r = n.exec(t);
53
- return r ? r[2] ? decodeURIComponent(r[2].replace(/\+/g, " ")).trim() : "" : null;
54
+ var n = new RegExp("[?&]" + e + "(=([^&#]*)|&|#|$)"), o = n.exec(t);
55
+ return o ? o[2] ? decodeURIComponent(o[2].replace(/\+/g, " ")).trim() : "" : null;
54
56
  }
55
- function E(e, t, n) {
57
+ function C(e, t, n) {
56
58
  try {
57
59
  return e && (n && e.startsWith("#") ? e : new URL(e, t).href);
58
- } catch (r) {
60
+ } catch (o) {
59
61
  return e;
60
62
  }
61
63
  }
62
- function V(e) {
64
+ function D(e) {
63
65
  const t = window.document.createElement("a");
64
66
  return t.href = e, t.href = t.href, t;
65
67
  }
66
68
  function F(e) {
67
69
  const t = e.search || "";
68
- return [...new URLSearchParams(t).entries()].reduce((n, r) => (n[r[0]] = r[1], n), {});
70
+ return [...new URLSearchParams(t).entries()].reduce((n, o) => (n[o[0]] = o[1], n), {});
69
71
  }
70
72
  function K(e, t) {
71
73
  let n = null;
72
- return function(...r) {
73
- const o = this;
74
+ return function(...o) {
75
+ const r = this;
74
76
  n && clearTimeout(n), n = setTimeout(() => {
75
- e.apply(o, r);
77
+ e.apply(r, o);
76
78
  }, t);
77
79
  };
78
80
  }
79
- function C(e, t, n) {
80
- const [r, o] = e.split("#"), a = new URL(r), s = a.searchParams;
81
- return t = c.URL_PARAM_PREFIX + t, s.has(t) ? s.set(t, n) : s.append(t, n), o && (a.hash = o), a.search = s.toString(), a.toString();
81
+ function S(e, t, n) {
82
+ const [o, r] = e.split("#"), a = new URL(o), c = a.searchParams;
83
+ return t = s.URL_PARAM_PREFIX + t, c.has(t) ? c.set(t, n) : c.append(t, n), r && (a.hash = r), a.search = c.toString(), a.toString();
82
84
  }
83
- const S = (e) => g(void 0, null, function* () {
85
+ const M = (e) => u(void 0, null, function* () {
84
86
  return new Promise((t) => {
85
87
  window.addEventListener("message", (n) => {
86
- n.data.cmd === "$xt/micro/info" && (n.data.data.mainMicroVersion !== u && console.warn(
87
- `子应用版本与主应用版本不一致,${e}子应用版本:${u},主应用版本:${n.data.data.mainMicroVersion}`
88
+ console.debug("child ipcSyncAppInfo receive message", n.data), n.data.cmd === "$xt/micro/info" && (n.data.data.mainMicroVersion !== h && console.warn(
89
+ `子应用版本与主应用版本不一致,${e}子应用版本:${h},主应用版本:${n.data.data.mainMicroVersion}`
88
90
  ), t(n.data.data));
89
- }), window.parent.postMessage(
91
+ }), console.debug("child ipcSyncAppInfo send message", e), window.parent.postMessage(
90
92
  {
91
93
  cmd: "$xt/micro/info",
92
94
  data: e
@@ -94,71 +96,71 @@ const S = (e) => g(void 0, null, function* () {
94
96
  "*"
95
97
  );
96
98
  });
97
- }), se = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
99
+ }), ie = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
98
100
  __proto__: null,
99
- addOrReplaceUrlParam: C,
100
- anchorElementGenerator: V,
101
+ addOrReplaceUrlParam: S,
102
+ anchorElementGenerator: D,
101
103
  debounce: K,
102
- getAbsolutePath: E,
104
+ getAbsolutePath: C,
103
105
  getAnchorElementQueryMap: F,
104
- getQueryParameter: p,
105
- ipcSyncAppInfo: S
106
- }, Symbol.toStringTag, { value: "Module" })), O = P(!1);
107
- Object.defineProperty(window, c.IS_MICRO, {
106
+ getQueryParameter: f,
107
+ ipcSyncAppInfo: M
108
+ }, Symbol.toStringTag, { value: "Module" })), P = E(!1);
109
+ Object.defineProperty(window, s.IS_MICRO, {
108
110
  get: function() {
109
- return O.value;
111
+ return P.value;
110
112
  },
111
113
  set: function(e) {
112
- O.value = e;
114
+ P.value = e;
113
115
  }
114
116
  });
115
- const R = (e) => `${c.DOM_ID_PREFIX}${e}`, D = (e) => {
116
- function t(i) {
117
+ const I = (e) => `${s.DOM_ID_PREFIX}${e}`, A = (e, t = !1) => {
118
+ function n(i) {
117
119
  return i !== null && typeof i == "object" && !Array.isArray(i);
118
120
  }
119
- const n = typeof e.params == "function" ? e.params() : t(e.params) ? e.params : {}, r = typeof e.token == "function" ? e.token() : t(e.token) ? e.token : "", o = new URL(
121
+ const o = typeof e.params == "function" ? e.params() : n(e.params) ? e.params : {}, r = typeof e.token == "function" ? e.token() : n(e.token) ? e.token : "", a = new URL(
120
122
  e.url.indexOf("http") > -1 ? e.url : window.location.origin + e.url
121
- ), a = p(c.URL_PARAM_PREFIX + e.name);
122
- if (a) {
123
- const i = E(a, e.url, !0);
123
+ ), c = f(s.URL_PARAM_PREFIX + e.name);
124
+ if (!t && c) {
125
+ const i = C(c, e.url, !0);
124
126
  if (i) {
125
- const l = new URL(i);
126
- l.searchParams.forEach((A, U) => {
127
- o.searchParams.set(U, A);
128
- }), o.hash = l.hash;
127
+ const p = new URL(i);
128
+ p.searchParams.forEach((U, L) => {
129
+ a.searchParams.set(L, U);
130
+ }), a.hash = p.hash;
129
131
  }
130
132
  }
131
- if (o.searchParams.set(c.IS_MICRO, !0 + ""), o.searchParams.set(c.MICRO_NAME, e.name), r && o.searchParams.set(c.MICRO_TOKEN, r), n)
132
- for (let i of Object.keys(n))
133
- o.searchParams.set(i, n[i]);
134
- const s = new URL(window.location.href);
135
- return s.searchParams.delete(e.name), window.history.replaceState({}, "", s.href), o.href;
133
+ if (a.searchParams.set(s.IS_MICRO, !0 + ""), a.searchParams.set(s.MICRO_NAME, e.name), r && a.searchParams.set(s.MICRO_TOKEN, r), o)
134
+ for (let i of Object.keys(o))
135
+ a.searchParams.set(i, o[i]);
136
+ const l = new URL(window.location.href);
137
+ return l.searchParams.delete(e.name), window.history.replaceState({}, "", l.href), a.href;
136
138
  }, W = () => {
137
139
  const e = document.createElement("div");
138
140
  return e.innerHTML = "应用加载中...", e.style.textAlign = "center", e.style.lineHeight = "100px", e.style.fontSize = "20px", e.style.color = "#999", e.style.position = "absolute", e.style.top = "0", e.style.left = "0", e.style.right = "0", e.style.bottom = "0", e.style.zIndex = "999", e.style.background = "transparent", e.style["mix-blend-mode"] = "difference", e;
139
- }, q = (e) => {
141
+ }, Y = (e) => {
140
142
  e.style.opacity = "0", setTimeout(() => {
141
143
  e.remove();
142
144
  }, 500);
143
- }, z = (e) => {
145
+ }, q = (e) => {
144
146
  const t = document.createElement("iframe");
145
- t.id = R(e.app.name);
146
- const n = D(e.app);
147
- t.src = n, t.style.width = "100%", t.style.height = "100%", t.style.border = "none", t.style.margin = "0", t.style.padding = "0", t.style.backgroundColor = "transparent", t.style["color-scheme"] = "unset", t.style.visibility = "hidden", t.addEventListener("load", (o) => (t.style.visibility = "visible", e.onLoad.call(e, o))), t.addEventListener(
147
+ t.id = I(e.app.name);
148
+ const n = A(e.app);
149
+ t.src = n, t.style.width = "100%", t.style.height = "100%", t.style.border = "none", t.style.margin = "0", t.style.padding = "0", t.style.backgroundColor = "transparent", t.style["color-scheme"] = "unset", t.style.visibility = "hidden", t.addEventListener("load", (r) => (t.style.visibility = "visible", e.onLoad.call(e, r))), t.addEventListener(
148
150
  "error",
149
- (o) => e.onError.call(e, o)
151
+ (r) => e.onError.call(e, r)
150
152
  ), t.addEventListener(
151
153
  "unload",
152
- (o) => e.onUnload.call(e, o)
154
+ (r) => e.onUnload.call(e, r)
153
155
  );
154
- const r = new MutationObserver((o) => {
155
- o.forEach((a) => {
156
- Array.from(a.removedNodes).includes(t) && (e.onUnload(new Event("unload")), r.disconnect());
156
+ const o = new MutationObserver((r) => {
157
+ r.forEach((a) => {
158
+ Array.from(a.removedNodes).includes(t) && (e.onUnload(new Event("unload")), o.disconnect());
157
159
  });
158
160
  });
159
- return r.observe(e.panel, { childList: !0 }), t;
161
+ return o.observe(e.panel, { childList: !0 }), t;
160
162
  };
161
- class G {
163
+ class z {
162
164
  constructor(t) {
163
165
  m(this, "app");
164
166
  m(this, "dom");
@@ -168,21 +170,21 @@ class G {
168
170
  return this.app = t, this.dom = void 0, this.panel = void 0, t.preload && this.preload(), this;
169
171
  }
170
172
  mount(t, n = !1) {
171
- let r;
172
- if (t ? (typeof t == "string" ? r = document.querySelector(t) : r = t, r.innerHTML = "") : r = document.body, r) {
173
- this.panel = r;
174
- let o = document.getElementById(R(this.app.name));
175
- o || (o = z(this), this.dom = o), n ? o.style.display = "none" : o.style.removeProperty("display");
173
+ let o;
174
+ if (t ? (typeof t == "string" ? o = document.querySelector(t) : o = t, o.innerHTML = "") : o = document.body, o) {
175
+ this.panel = o;
176
+ let r = document.getElementById(I(this.app.name));
177
+ r || (r = q(this), this.dom = r), n ? r.style.display = "none" : r.style.removeProperty("display");
176
178
  const a = W();
177
179
  this.loadingTimer = setTimeout(() => {
178
- this.loading = a, r.appendChild(a);
179
- }, 1e3), r.appendChild(o);
180
+ this.loading = a, o.appendChild(a);
181
+ }, 1e3), o.appendChild(r);
180
182
  } else
181
183
  throw new Error("el not found");
182
184
  }
183
185
  unmount() {
184
186
  var n;
185
- const t = document.getElementById(R(this.app.name));
187
+ const t = document.getElementById(I(this.app.name));
186
188
  if (t)
187
189
  this.app.preload ? (t.style.display = "none", document.body.appendChild(t)) : (n = this.panel) == null || n.removeChild(t);
188
190
  else
@@ -202,15 +204,17 @@ class G {
202
204
  preload() {
203
205
  this.mount(void 0, !0);
204
206
  }
205
- reload() {
206
- const t = this.dom.src;
207
- this.dom.src = t;
207
+ reload(t = !1) {
208
+ let n = this.dom.src;
209
+ t && (n = A(this.app, !0), console.log("go home", n)), console.log("reload app", this.app, n);
210
+ const o = new URL(n);
211
+ o.searchParams.set("__t", Date.now().toString()), this.dom.src = o.toString();
208
212
  }
209
213
  changeOptions(t) {
210
214
  this.app = d(d({}, this.app), t);
211
215
  }
212
216
  onLoad(t) {
213
- console.debug("app onLoad", this.app.name), this.loadingTimer && clearTimeout(this.loadingTimer), this.loading && q(this.loading), Q(this), this.app.onLoad && this.app.onLoad(t, this);
217
+ console.debug("app onLoad", this.app.name), this.loadingTimer && clearTimeout(this.loadingTimer), this.loading && Y(this.loading), G(this), this.app.onLoad && this.app.onLoad(t, this);
214
218
  }
215
219
  onUnload(t) {
216
220
  console.debug("app onUnload", this.app.name), this.app.onUnload && this.app.onUnload(t, this);
@@ -219,7 +223,7 @@ class G {
219
223
  console.error("app onError", this.app.name), this.app.onError && this.app.onError(t, this);
220
224
  }
221
225
  }
222
- const Q = (e) => {
226
+ const G = (e) => {
223
227
  var t, n;
224
228
  (n = (t = e.dom) == null ? void 0 : t.contentWindow) == null || n.postMessage(
225
229
  {
@@ -234,30 +238,32 @@ const Q = (e) => {
234
238
  },
235
239
  "*"
236
240
  );
237
- }, Y = () => {
238
- window.addEventListener("message", (e) => {
241
+ }, Q = () => {
242
+ window[s.MICRO_TYPE] = "parent", window.addEventListener("message", (e) => {
239
243
  if (e.data.cmd === "$xt/micro/sync") {
244
+ console.log("父页面接受子应用同步路由信息", e.data);
240
245
  const t = [
241
- c.IS_MICRO,
242
- c.MICRO_NAME,
243
- c.MICRO_TOKEN
246
+ s.IS_MICRO,
247
+ s.MICRO_NAME,
248
+ s.MICRO_TOKEN
244
249
  ], n = new URL(
245
250
  window.decodeURIComponent(e.data.data.url),
246
251
  e.origin
247
252
  );
248
253
  for (let a of t)
249
254
  n.searchParams.delete(a);
250
- const r = new URL(window.location.href);
251
- r.searchParams.forEach((a, s) => {
252
- s.startsWith(c.URL_PARAM_PREFIX) && r.searchParams.delete(s);
255
+ const o = new URL(window.location.href);
256
+ o.searchParams.forEach((a, c) => {
257
+ c.startsWith(s.URL_PARAM_PREFIX) && o.searchParams.delete(c);
253
258
  });
254
- const o = C(
255
- r.href,
259
+ const r = S(
260
+ o.href,
256
261
  e.data.data.name,
257
262
  n.href
258
263
  );
259
- window.history.replaceState(null, "", o);
264
+ window.history.replaceState(null, "", r);
260
265
  } else if (e.data.cmd === "$xt/micro/info") {
266
+ console.debug("parent 「$xt/micro/info」 receive message", e.data);
261
267
  const t = y(e.data.data);
262
268
  e.source.postMessage(
263
269
  {
@@ -274,7 +280,7 @@ const Q = (e) => {
274
280
  port: window.location.port,
275
281
  protocol: window.location.protocol
276
282
  },
277
- mainMicroVersion: u,
283
+ mainMicroVersion: h,
278
284
  name: t.name,
279
285
  url: t.url,
280
286
  params: t.params,
@@ -286,126 +292,134 @@ const Q = (e) => {
286
292
  );
287
293
  }
288
294
  });
289
- }, ie = (...t) => g(void 0, [...t], function* (e = {}) {
290
- const n = p(c.IS_MICRO), r = p(c.MICRO_NAME);
291
- n && (window[c.IS_MICRO] = !0), r && S(r).then((s) => {
292
- window[c.MICRO_APP] = d({
293
- microVersion: u
294
- }, s);
295
- });
296
- function o(s) {
297
- var i = document.styleSheets[0];
298
- if (!i) {
299
- var l = document.createElement("style");
300
- document.head.appendChild(l), i = l.sheet;
301
- }
302
- i.insertRule(s, i.cssRules.length);
303
- }
304
- window[c.IS_MICRO] && e.hideHeaderCssSelector && (Array.isArray(e.hideHeaderCssSelector) ? e.hideHeaderCssSelector.forEach((s) => {
305
- o(`${s} { display: none !important; }`);
306
- }) : o(
307
- `${e.hideHeaderCssSelector} { display: none !important; }`
308
- )), window.addEventListener("message", (s) => {
309
- s.data.cmd === `$xt/micro/${r}` && I();
310
- }), J(window);
311
- const a = p(c.MICRO_TOKEN);
312
- e.authHandler && a && (yield e.authHandler(a));
295
+ }, ce = (...t) => u(void 0, [...t], function* (e = {}) {
296
+ window[s.MICRO_TYPE] = "child";
297
+ const n = f(s.IS_MICRO), o = f(s.MICRO_NAME);
298
+ if (console.debug("child microInit", n, o), n && (window[s.IS_MICRO] = n === "true"), o && M(o).then((r) => {
299
+ window[s.MICRO_APP] = d({
300
+ microVersion: h
301
+ }, r);
302
+ }), window[s.IS_MICRO]) {
303
+ let r = function(c) {
304
+ var l = document.styleSheets[0];
305
+ if (!l) {
306
+ var i = document.createElement("style");
307
+ document.head.appendChild(i), l = i.sheet;
308
+ }
309
+ l.insertRule(c, l.cssRules.length);
310
+ };
311
+ window[s.IS_MICRO] && e.hideHeaderCssSelector && (Array.isArray(e.hideHeaderCssSelector) ? e.hideHeaderCssSelector.forEach((c) => {
312
+ r(`${c} { display: none !important; }`);
313
+ }) : r(
314
+ `${e.hideHeaderCssSelector} { display: none !important; }`
315
+ )), yield J(o, window), window.addEventListener("message", (c) => u(void 0, null, function* () {
316
+ c.data.cmd === `$xt/micro/${o}` && R(o);
317
+ }));
318
+ const a = f(s.MICRO_TOKEN);
319
+ e.authHandler && a && (yield e.authHandler(a));
320
+ } else
321
+ console.warn("当前环境非微应用环境");
313
322
  });
314
- function J(e) {
315
- const t = e.history, n = t.pushState, r = t.replaceState;
316
- t.pushState = function(o, a, s) {
317
- const i = f(d(d({}, t.state || {}), o), {
323
+ function J(e, t) {
324
+ const n = t.history, o = n.pushState, r = n.replaceState;
325
+ n.pushState = function(a, c, l) {
326
+ const i = w(d(d({}, n.state || {}), a), {
318
327
  // 添加新数据
319
328
  _patchedByParent: !0
320
329
  // 可选:标记这是被父窗口修改的状态
321
330
  });
322
- n.call(t, i, a, s), I();
323
- }, t.replaceState = function(o, a, s) {
324
- const i = f(d(d({}, t.state || {}), o), {
331
+ o.call(n, i, c, l), R(e);
332
+ }, n.replaceState = function(a, c, l) {
333
+ const i = w(d(d({}, n.state || {}), a), {
325
334
  // 添加新数据
326
335
  _patchedByParent: !0
327
336
  // 可选:标记这是被父窗口修改的状态
328
337
  });
329
- r.call(t, i, a, s), I();
338
+ r.call(n, i, c, l), R(e);
330
339
  };
331
340
  }
332
- function I() {
333
- if (!window[c.MICRO_APP])
334
- return;
335
- const { name: e, sync: t, mainBaseURL: n } = window[c.MICRO_APP], r = window.location.pathname + window.location.search + window.location.hash;
336
- window.parent.postMessage(
337
- {
338
- cmd: "$xt/micro/sync",
339
- data: {
340
- name: e,
341
- url: window.encodeURIComponent(r)
342
- }
343
- },
344
- n
345
- );
341
+ function R(e) {
342
+ return u(this, null, function* () {
343
+ console.log("syncUrlToWindow", e, window[s.MICRO_APP]), window[s.MICRO_APP] || (yield M(e).then((a) => {
344
+ window[s.MICRO_APP] = d({
345
+ microVersion: h
346
+ }, a);
347
+ }));
348
+ const { name: t, sync: n, mainLocation: o } = window[s.MICRO_APP], r = window.location.pathname + window.location.search + window.location.hash;
349
+ window.parent.postMessage(
350
+ {
351
+ cmd: "$xt/micro/sync",
352
+ data: {
353
+ name: t,
354
+ url: window.encodeURIComponent(r)
355
+ }
356
+ },
357
+ o.origin
358
+ );
359
+ });
346
360
  }
347
- let w = {};
361
+ let _ = {};
348
362
  const Z = {
349
363
  url: "",
350
364
  name: "",
351
365
  sync: !0
352
- }, ce = (e) => {
366
+ }, le = (e) => {
353
367
  if (!e.url || e.url === "" || e.url === "undefined") {
354
368
  console.error("app url is required", e);
355
369
  return;
356
370
  }
357
371
  const t = Object.assign({}, Z, e);
358
- return w[e.name] = f(d({}, t), {
359
- instance: new G(e)
360
- }), w[e.name];
361
- }, y = (e) => w[e], ee = (e, t) => w[e.name] = d(d({}, e), t), te = (e, t) => new Promise((n, r) => {
362
- let o = y(e);
363
- o ? (t && (o.instance.changeOptions(t), o = ee(o, t)), o.preload ? o.instance.preloadRender(o.el) : o.instance.mount(o.el), n(o)) : r("app not found");
372
+ return _[e.name] = w(d({}, t), {
373
+ instance: new z(e)
374
+ }), _[e.name];
375
+ }, y = (e) => _[e], ee = (e, t) => _[e.name] = d(d({}, e), t), te = (e, t) => new Promise((n, o) => {
376
+ let r = y(e);
377
+ r ? (t && (r.instance.changeOptions(t), r = ee(r, t)), r.preload ? r.instance.preloadRender(r.el) : r.instance.mount(r.el), n(r)) : o("app not found");
364
378
  }), ne = (e) => {
365
379
  let t = y(e);
366
380
  t && t.instance.unmount();
367
- }, le = (e) => {
368
- let t = y(e);
369
- t && t.instance.reload();
370
- }, oe = /* @__PURE__ */ $({
381
+ }, de = (e, t = !1) => {
382
+ let n = y(e);
383
+ n && n.instance.reload(t);
384
+ }, oe = /* @__PURE__ */ X({
371
385
  __name: "XTMicroView",
372
386
  props: {
373
387
  appId: String
374
388
  },
375
389
  setup(e) {
376
- const t = e, n = P(), r = () => {
390
+ const t = e, n = E(), o = () => {
377
391
  t.appId && te(t.appId, {
378
392
  el: n.value
379
- }).then((o) => {
393
+ }).then((r) => {
380
394
  console.debug("app mounted", t.appId);
381
395
  });
382
396
  };
383
- return X(() => t.appId, (o, a) => {
384
- o !== a && a && ne(a), r();
385
- }), k(() => {
386
- r();
397
+ return k(() => t.appId, (r, a) => {
398
+ r !== a && a && ne(a), o();
387
399
  }), H(() => {
400
+ o();
401
+ }), N(() => {
388
402
  console.debug("app unmount", t.appId);
389
- }), (o, a) => (N(), j("div", B({
403
+ }), (r, a) => (j(), V("div", B({
390
404
  ref_key: "refPanel",
391
405
  ref: n
392
- }, o.$attrs, { class: "xt-micro" }), null, 16));
406
+ }, r.$attrs, { class: "xt-micro" }), null, 16));
393
407
  }
394
408
  });
395
- window[c.MICRO_VERSION] = u;
396
- const de = {
409
+ window[s.MICRO_VERSION] = h;
410
+ const pe = {
397
411
  install: (e, t = {}) => {
398
- e.component("xt-micro-view", oe), Y();
412
+ e.component("xt-micro-view", oe), Q();
399
413
  }
400
414
  };
401
415
  export {
402
416
  oe as XTMicroView,
403
- de as default,
417
+ pe as default,
404
418
  y as getApp,
405
- ie as microInit,
406
- le as reloadApp,
407
- ce as setupApp,
419
+ ce as microInit,
420
+ de as reloadApp,
421
+ le as setupApp,
408
422
  te as startApp,
409
423
  ne as stopApp,
410
- se as utils
424
+ ie as utils
411
425
  };
@@ -18,5 +18,5 @@ export declare const startApp: (name: string, options?: Partial<AppOption>) => P
18
18
  * @param {string} name - 应用程序的名称
19
19
  */
20
20
  export declare const stopApp: (name: string) => void;
21
- export declare const reloadApp: (name: string) => void;
21
+ export declare const reloadApp: (name: string, isGoHome?: boolean) => void;
22
22
  export { microInit };
@@ -9,7 +9,7 @@ export declare const microInit: (options?: MicroInitOptions) => Promise<void>;
9
9
  /**
10
10
  * 同步子应用路由到主应用路由
11
11
  */
12
- export declare function syncUrlToWindow(): void;
12
+ export declare function syncUrlToWindow(microName: string): Promise<void>;
13
13
  /**
14
14
  * 同步主应用路由到子应用
15
15
  */
@@ -19,7 +19,7 @@ export declare class MicroApp {
19
19
  * 预加载app,先放到body,待启动时,挪过来
20
20
  */
21
21
  preload(): void;
22
- reload(): void;
22
+ reload(isGoHome?: boolean): void;
23
23
  changeOptions(options: Partial<AppOption>): void;
24
24
  onLoad(_event: Event): void;
25
25
  onUnload(_event: Event): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xingtukeji/micro",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "main": "dist/index.js",