@xingtukeji/micro 1.1.19 → 1.1.20

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.js CHANGED
@@ -1,35 +1,35 @@
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 x = Object.defineProperty, X = Object.defineProperties;
3
- var k = Object.getOwnPropertyDescriptors;
4
- var M = Object.getOwnPropertySymbols;
5
- var H = Object.prototype.hasOwnProperty, j = Object.prototype.propertyIsEnumerable;
6
- var R = (e, t, n) => t in e ? x(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, d = (e, t) => {
7
- for (var n in t || (t = {}))
8
- H.call(t, n) && R(e, n, t[n]);
9
- if (M)
10
- for (var n of M(t))
11
- j.call(t, n) && R(e, n, t[n]);
12
- return e;
13
- }, y = (e, t) => X(e, k(t));
14
- var p = (e, t, n) => (R(e, typeof t != "symbol" ? t + "" : t, n), n);
15
- var h = (e, t, n) => new Promise((o, r) => {
16
- var a = (l) => {
2
+ var $ = Object.defineProperty, x = Object.defineProperties;
3
+ var X = Object.getOwnPropertyDescriptors;
4
+ var E = Object.getOwnPropertySymbols;
5
+ var k = Object.prototype.hasOwnProperty, H = Object.prototype.propertyIsEnumerable;
6
+ var M = (t, e, n) => e in t ? $(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n, m = (t, e) => {
7
+ for (var n in e || (e = {}))
8
+ k.call(e, n) && M(t, n, e[n]);
9
+ if (E)
10
+ for (var n of E(e))
11
+ H.call(e, n) && M(t, n, e[n]);
12
+ return t;
13
+ }, R = (t, e) => x(t, X(e));
14
+ var h = (t, e, n) => (M(t, typeof e != "symbol" ? e + "" : e, n), n);
15
+ var w = (t, e, n) => new Promise((o, r) => {
16
+ var a = (d) => {
17
17
  try {
18
- i(n.next(l));
19
- } catch (g) {
20
- r(g);
18
+ s(n.next(d));
19
+ } catch (y) {
20
+ r(y);
21
21
  }
22
- }, c = (l) => {
22
+ }, l = (d) => {
23
23
  try {
24
- i(n.throw(l));
25
- } catch (g) {
26
- r(g);
24
+ s(n.throw(d));
25
+ } catch (y) {
26
+ r(y);
27
27
  }
28
- }, i = (l) => l.done ? o(l.value) : Promise.resolve(l.value).then(a, c);
29
- i((n = n.apply(e, t)).next());
28
+ }, s = (d) => d.done ? o(d.value) : Promise.resolve(d.value).then(a, l);
29
+ s((n = n.apply(t, e)).next());
30
30
  });
31
- import { ref as O, defineComponent as N, watch as F, onMounted as B, onUnmounted as V, openBlock as D, createElementBlock as K, mergeProps as Y } from "vue";
32
- const s = {
31
+ import { ref as C, defineComponent as j, watch as F, onMounted as K, onUnmounted as N, openBlock as Y, createElementBlock as B, mergeProps as V } from "vue";
32
+ const c = {
33
33
  KEY: "$xtm",
34
34
  // 父应用注入子应用实例的key
35
35
  IS_MICRO: "__XT_MICRO",
@@ -48,137 +48,224 @@ const s = {
48
48
  // 微应用在 url 参数中的前缀
49
49
  MICRO_VERSION: "__XT_MICRO_VERSION"
50
50
  // 微应用版本
51
- }, w = "1.1.19";
52
- function f(e, t = window.location.href) {
53
- e = e.replace(/[\[\]]/g, "\\$&");
54
- var n = new RegExp("[?&]" + e + "(=([^&#]*)|&|#|$)"), o = n.exec(t);
51
+ }, _ = "1.1.20";
52
+ function g(t, e = window.location.href) {
53
+ t = t.replace(/[\[\]]/g, "\\$&");
54
+ var n = new RegExp("[?&]" + t + "(=([^&#]*)|&|#|$)"), o = n.exec(e);
55
55
  return o ? o[2] ? decodeURIComponent(o[2].replace(/\+/g, " ")).trim() : "" : null;
56
56
  }
57
- function E(e, t, n) {
57
+ function b(t, e, n) {
58
58
  try {
59
- return e && (n && e.startsWith("#") ? e : new URL(e, t).href);
59
+ return t && (n && t.startsWith("#") ? t : new URL(t, e).href);
60
60
  } catch (o) {
61
- return e;
61
+ return t;
62
62
  }
63
63
  }
64
- function W(e) {
65
- const t = window.document.createElement("a");
66
- return t.href = e, t.href = t.href, t;
64
+ function D(t) {
65
+ const e = window.document.createElement("a");
66
+ return e.href = t, e.href = e.href, e;
67
67
  }
68
- function z(e) {
69
- const t = e.search || "";
70
- return [...new URLSearchParams(t).entries()].reduce((n, o) => (n[o[0]] = o[1], n), {});
68
+ function G(t) {
69
+ const e = t.search || "";
70
+ return [...new URLSearchParams(e).entries()].reduce((n, o) => (n[o[0]] = o[1], n), {});
71
71
  }
72
- function q(e, t) {
72
+ function W(t, e) {
73
73
  let n = null;
74
74
  return function(...o) {
75
75
  const r = this;
76
76
  n && clearTimeout(n), n = setTimeout(() => {
77
- e.apply(r, o);
78
- }, t);
77
+ t.apply(r, o);
78
+ }, e);
79
79
  };
80
80
  }
81
- function C(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(), console.trace("urlObj", a), a.origin + a.pathname + a.search + a.hash;
81
+ function S(t, e, n) {
82
+ const [o, r] = t.split("#"), a = new URL(o), l = a.searchParams;
83
+ return e = c.URL_PARAM_PREFIX + e, l.has(e) ? l.set(e, n) : l.append(e, n), r && (a.hash = r), a.search = l.toString(), i.trace("urlObj", a), a.origin + a.pathname + a.search + a.hash;
84
84
  }
85
- const S = (e) => h(void 0, null, function* () {
86
- return new Promise((t) => {
85
+ const U = (t) => w(void 0, null, function* () {
86
+ return new Promise((e) => {
87
87
  window.addEventListener("message", (n) => {
88
- console.debug("child ipcSyncAppInfo receive message", n.data), n.data.cmd === "$xt/micro/info" && (n.data.data.mainMicroVersion !== w && console.warn(
89
- `子应用版本与主应用版本不一致,${e}子应用版本:${w},主应用版本:${n.data.data.mainMicroVersion}`
90
- ), t(n.data.data));
91
- }), console.debug("child ipcSyncAppInfo send message", e), window.parent.postMessage(
88
+ i.debug("child ipcSyncAppInfo receive message", n.data), n.data.cmd === "$xt/micro/info" && (n.data.data.mainMicroVersion !== _ && i.warn(
89
+ `子应用版本与主应用版本不一致,${t}子应用版本:${_},主应用版本:${n.data.data.mainMicroVersion}`
90
+ ), e(n.data.data));
91
+ }), i.debug("child ipcSyncAppInfo send message", t), window.parent.postMessage(
92
92
  {
93
93
  cmd: "$xt/micro/info",
94
- data: e
94
+ data: t
95
95
  },
96
96
  "*"
97
97
  );
98
98
  });
99
- }), he = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
99
+ }), u = class u {
100
+ // 日志开关,默认为开启状态
101
+ // private static enable = localStorage;
102
+ /**
103
+ * 从 localStorage 读取开关状态,默认开启
104
+ */
105
+ static get enable() {
106
+ return localStorage.getItem(u.STORAGE_KEY) === "true";
107
+ }
108
+ /**
109
+ * 设置开关状态并同步到 localStorage
110
+ * @param status 是否开启日志
111
+ */
112
+ static setEnable(e) {
113
+ localStorage.setItem(u.STORAGE_KEY, e.toString());
114
+ }
115
+ /**
116
+ * 获取当前日志开关状态
117
+ * @returns 当前开关状态
118
+ */
119
+ static getEnable() {
120
+ return u.enable;
121
+ }
122
+ /**
123
+ * 提取调用日志的文件路径和行号
124
+ * @returns 格式化的位置信息(如 "src/utils/xxx.ts:123")
125
+ */
126
+ static getCallerInfo() {
127
+ try {
128
+ const o = (new Error().stack || "").split(`
129
+ `).slice(4);
130
+ if (o.length === 0)
131
+ return "unknown location";
132
+ const r = o[0].trim(), a = r.match(/(?:http[s]?:\/\/.*?\/|)([^:]+):(\d+):(\d+)/);
133
+ if (a) {
134
+ const [, l, s] = a;
135
+ return `[${l.split("/").slice(-2).join("/")}:${s}]`;
136
+ }
137
+ return `[${r}]`;
138
+ } catch (e) {
139
+ return "[unknown location]";
140
+ }
141
+ }
142
+ /**
143
+ * 通用日志输出方法(内部使用)
144
+ * @param type 日志类型(log/info/warn/error 等)
145
+ * @param args 日志参数
146
+ */
147
+ static _log(e, ...n) {
148
+ if (u.enable && typeof console[e] == "function") {
149
+ const o = this.getCallerInfo();
150
+ console[e](o, ...n);
151
+ }
152
+ }
153
+ // 以下为常用日志方法封装,与 console 用法一致
154
+ static log(...e) {
155
+ this._log("log", ...e);
156
+ }
157
+ static info(...e) {
158
+ this._log("info", ...e);
159
+ }
160
+ static warn(...e) {
161
+ this._log("warn", ...e);
162
+ }
163
+ static error(...e) {
164
+ this._log("error", ...e);
165
+ }
166
+ static debug(...e) {
167
+ this._log("debug", ...e);
168
+ }
169
+ static trace(...e) {
170
+ this._log("trace", ...e);
171
+ }
172
+ static table(e, n) {
173
+ this.enable && console.table(e, n);
174
+ }
175
+ static time(e) {
176
+ this.enable && console.time(e);
177
+ }
178
+ static timeEnd(e) {
179
+ this.enable && console.timeEnd(e);
180
+ }
181
+ };
182
+ // 存储在 localStorage 中的键名
183
+ h(u, "STORAGE_KEY", "@xt/micro/console/enable");
184
+ let i = u;
185
+ const de = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
100
186
  __proto__: null,
101
- addOrReplaceUrlParam: C,
102
- anchorElementGenerator: W,
103
- debounce: q,
104
- getAbsolutePath: E,
105
- getAnchorElementQueryMap: z,
106
- getQueryParameter: f,
107
- ipcSyncAppInfo: S
108
- }, Symbol.toStringTag, { value: "Module" })), P = O(!1);
109
- Object.defineProperty(window, s.IS_MICRO, {
187
+ ConsoleUtil: i,
188
+ addOrReplaceUrlParam: S,
189
+ anchorElementGenerator: D,
190
+ debounce: W,
191
+ getAbsolutePath: b,
192
+ getAnchorElementQueryMap: G,
193
+ getQueryParameter: g,
194
+ ipcSyncAppInfo: U
195
+ }, Symbol.toStringTag, { value: "Module" })), O = C(!1);
196
+ Object.defineProperty(window, c.IS_MICRO, {
110
197
  get: function() {
111
- return P.value;
198
+ return O.value;
112
199
  },
113
- set: function(e) {
114
- P.value = e;
200
+ set: function(t) {
201
+ O.value = t;
115
202
  }
116
203
  });
117
- const I = (e) => `${s.DOM_ID_PREFIX}${e}`, U = (e, t = !1) => {
118
- function n(i) {
119
- return i !== null && typeof i == "object" && !Array.isArray(i);
120
- }
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(
122
- e.url.indexOf("http") > -1 ? e.url : window.location.origin + e.url
123
- ), c = f(s.URL_PARAM_PREFIX + e.name);
124
- if (!t && c) {
125
- const i = E(c, e.url, !0);
126
- if (i) {
127
- const l = new URL(i, e.url);
128
- l.searchParams.forEach((A, $) => {
129
- a.searchParams.set($, A);
130
- }), a.hash = l.hash;
204
+ const P = (t) => `${c.DOM_ID_PREFIX}${t}`, L = (t, e = !1) => {
205
+ function n(s) {
206
+ return s !== null && typeof s == "object" && !Array.isArray(s);
207
+ }
208
+ const o = typeof t.params == "function" ? t.params() : n(t.params) ? t.params : {}, r = typeof t.token == "function" ? t.token() : n(t.token) ? t.token : "", a = new URL(
209
+ t.url.indexOf("http") > -1 ? t.url : window.location.origin + t.url
210
+ ), l = g(c.URL_PARAM_PREFIX + t.name);
211
+ if (!e && l) {
212
+ const s = b(l, t.url, !0);
213
+ if (s) {
214
+ const d = new URL(s, t.url);
215
+ d.searchParams.forEach((A, v) => {
216
+ a.searchParams.set(v, A);
217
+ }), a.hash = d.hash;
131
218
  }
132
- m(e.name);
133
- }
134
- 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)
135
- for (let i of Object.keys(o))
136
- a.searchParams.set(i, o[i]);
137
- return m(e.name), a.href;
138
- }, G = () => {
139
- const e = document.createElement("div");
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;
141
- }, Q = (e) => {
142
- e.style.opacity = "0", setTimeout(() => {
143
- e.remove();
219
+ f(t.name);
220
+ }
221
+ if (a.searchParams.set(c.IS_MICRO, !0 + ""), a.searchParams.set(c.MICRO_NAME, t.name), r && a.searchParams.set(c.MICRO_TOKEN, r), o)
222
+ for (let s of Object.keys(o))
223
+ a.searchParams.set(s, o[s]);
224
+ return f(t.name), a.href;
225
+ }, q = () => {
226
+ const t = document.createElement("div");
227
+ return t.innerHTML = "应用加载中...", t.style.textAlign = "center", t.style.lineHeight = "100px", t.style.fontSize = "20px", t.style.color = "#999", t.style.position = "absolute", t.style.top = "0", t.style.left = "0", t.style.right = "0", t.style.bottom = "0", t.style.zIndex = "999", t.style.background = "transparent", t.style["mix-blend-mode"] = "difference", t;
228
+ }, z = (t) => {
229
+ t.style.opacity = "0", setTimeout(() => {
230
+ t.remove();
144
231
  }, 500);
145
- }, J = (e) => {
146
- const t = document.createElement("iframe");
147
- t.id = I(e.app.name);
148
- const n = U(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(
232
+ }, Q = (t) => {
233
+ const e = document.createElement("iframe");
234
+ e.id = P(t.app.name);
235
+ const n = L(t.app);
236
+ e.src = n, e.style.width = "100%", e.style.height = "100%", e.style.border = "none", e.style.margin = "0", e.style.padding = "0", e.style.backgroundColor = "transparent", e.style["color-scheme"] = "unset", e.style.visibility = "hidden", e.addEventListener("load", (r) => (e.style.visibility = "visible", t.onLoad.call(t, r))), e.addEventListener(
150
237
  "error",
151
- (r) => e.onError.call(e, r)
152
- ), t.addEventListener(
238
+ (r) => t.onError.call(t, r)
239
+ ), e.addEventListener(
153
240
  "unload",
154
- (r) => e.onUnload.call(e, r)
241
+ (r) => t.onUnload.call(t, r)
155
242
  );
156
243
  const o = new MutationObserver((r) => {
157
- console.log("监听 body 中子节点的变化2", e.panel), r.forEach((a) => {
158
- Array.from(a.removedNodes).includes(t) && (e.onUnload(new Event("unload")), o.disconnect());
244
+ i.log("监听 body 中子节点的变化2", t.panel), r.forEach((a) => {
245
+ Array.from(a.removedNodes).includes(e) && (t.onUnload(new Event("unload")), o.disconnect());
159
246
  });
160
247
  });
161
- return o.observe(e.panel.parentNode, {
248
+ return o.observe(t.panel.parentNode, {
162
249
  childList: !0,
163
250
  subtree: !1
164
- }), t;
251
+ }), e;
165
252
  };
166
- class Z {
167
- constructor(t) {
168
- p(this, "app");
169
- p(this, "dom");
170
- p(this, "panel");
171
- p(this, "loading");
172
- p(this, "loadingTimer");
173
- return this.app = t, this.dom = void 0, this.panel = void 0, t.preload && this.preload(), this;
174
- }
175
- mount(t, n = !1) {
253
+ class J {
254
+ constructor(e) {
255
+ h(this, "app");
256
+ h(this, "dom");
257
+ h(this, "panel");
258
+ h(this, "loading");
259
+ h(this, "loadingTimer");
260
+ return this.app = e, this.dom = void 0, this.panel = void 0, e.preload && this.preload(), this;
261
+ }
262
+ mount(e, n = !1) {
176
263
  let o;
177
- if (t ? (typeof t == "string" ? o = document.querySelector(t) : o = t, o.innerHTML = "") : o = document.body, o) {
264
+ if (e ? (typeof e == "string" ? o = document.querySelector(e) : o = e, o.innerHTML = "") : o = document.body, o) {
178
265
  this.panel = o;
179
- let r = document.getElementById(I(this.app.name));
180
- r || (r = J(this), this.dom = r), n ? r.style.display = "none" : r.style.removeProperty("display");
181
- const a = G();
266
+ let r = document.getElementById(P(this.app.name));
267
+ r || (r = Q(this), this.dom = r), n ? r.style.display = "none" : r.style.removeProperty("display");
268
+ const a = q();
182
269
  this.loadingTimer = setTimeout(() => {
183
270
  this.loading = a, o.appendChild(a);
184
271
  }, 1e3), o.appendChild(r);
@@ -187,10 +274,10 @@ class Z {
187
274
  }
188
275
  unmount() {
189
276
  var n;
190
- console.debug("micro unmount app", this.app.name);
191
- const t = document.getElementById(I(this.app.name));
192
- if (t)
193
- this.app.preload ? (t.style.display = "none", document.body.appendChild(t)) : (n = this.panel) == null || n.removeChild(t);
277
+ i.debug("micro unmount app", this.app.name);
278
+ const e = document.getElementById(P(this.app.name));
279
+ if (e)
280
+ this.app.preload ? (e.style.display = "none", document.body.appendChild(e)) : (n = this.panel) == null || n.removeChild(e);
194
281
  else
195
282
  throw new Error("app dom not found");
196
283
  }
@@ -199,8 +286,8 @@ class Z {
199
286
  * TODO 还差用户切走之后,如何将ifr还原回去,等待下次载入
200
287
  * @param el 挂载dom对象
201
288
  */
202
- preloadRender(t) {
203
- this.mount(t);
289
+ preloadRender(e) {
290
+ this.mount(e);
204
291
  }
205
292
  /**
206
293
  * 预加载app,先放到body,待启动时,挪过来
@@ -208,70 +295,70 @@ class Z {
208
295
  preload() {
209
296
  this.mount(void 0, !0);
210
297
  }
211
- reload(t = !1) {
298
+ reload(e = !1) {
212
299
  let n = this.dom.src;
213
- t && (n = U(this.app, !0), console.log("go home", n)), console.log("reload app", this.app, n);
300
+ e && (n = L(this.app, !0), i.log("go home", n)), i.log("reload app", this.app, n);
214
301
  const o = new URL(n);
215
302
  o.searchParams.set("__t", Date.now().toString()), this.dom.style.visibility = "hidden", this.dom.src = o.toString();
216
303
  }
217
- changeOptions(t) {
218
- console.log("changeOptions", this.app, t), this.app = d(d({}, this.app), t);
304
+ changeOptions(e) {
305
+ i.log("changeOptions", this.app, e), this.app = m(m({}, this.app), e);
219
306
  }
220
- onLoad(t) {
221
- console.debug("app onLoad", this.app.name), this.loadingTimer && clearTimeout(this.loadingTimer), this.loading && Q(this.loading), ee(this), console.log("this.app.onLoad", this.app.onLoad), this.app.onLoad && this.app.onLoad(t, this);
307
+ onLoad(e) {
308
+ i.debug("app onLoad", this.app.name), this.loadingTimer && clearTimeout(this.loadingTimer), this.loading && z(this.loading), Z(this), i.log("this.app.onLoad", this.app.onLoad), this.app.onLoad && this.app.onLoad(e, this);
222
309
  }
223
- onUnload(t) {
224
- console.debug("app onUnload", this.app.name), m(this.app.name), this.app.onUnload && this.app.onUnload(t, this);
310
+ onUnload(e) {
311
+ i.debug("app onUnload", this.app.name), f(this.app.name), this.app.onUnload && this.app.onUnload(e, this);
225
312
  }
226
- onError(t) {
227
- console.error("app onError", this.app.name), this.app.onError && this.app.onError(t, this);
313
+ onError(e) {
314
+ i.error("app onError", this.app.name), this.app.onError && this.app.onError(e, this);
228
315
  }
229
316
  }
230
- const ee = (e) => {
231
- var t, n;
232
- (n = (t = e.dom) == null ? void 0 : t.contentWindow) == null || n.postMessage(
317
+ const Z = (t) => {
318
+ var e, n;
319
+ (n = (e = t.dom) == null ? void 0 : e.contentWindow) == null || n.postMessage(
233
320
  {
234
- cmd: `$xt/micro/${e.app.name}`,
321
+ cmd: `$xt/micro/${t.app.name}`,
235
322
  data: {
236
- name: e.app.name,
237
- url: e.app.url,
238
- preload: e.app.preload,
239
- sync: e.app.sync || !0
323
+ name: t.app.name,
324
+ url: t.app.url,
325
+ preload: t.app.preload,
326
+ sync: t.app.sync || !0
240
327
  // params: app.params
241
328
  }
242
329
  },
243
330
  "*"
244
331
  );
245
- }, te = () => {
246
- window[s.MICRO_TYPE] = "parent", window.addEventListener("message", (e) => {
247
- if (e.data.cmd === "$xt/micro/sync") {
248
- console.log("父页面接受子应用同步路由信息", e.data);
249
- const t = [
250
- s.IS_MICRO,
251
- s.MICRO_NAME,
252
- s.MICRO_TOKEN
332
+ }, ee = () => {
333
+ window[c.MICRO_TYPE] = "parent", window.addEventListener("message", (t) => {
334
+ if (t.data.cmd === "$xt/micro/sync") {
335
+ i.log("父页面接受子应用同步路由信息", t.data);
336
+ const e = [
337
+ c.IS_MICRO,
338
+ c.MICRO_NAME,
339
+ c.MICRO_TOKEN
253
340
  ], n = new URL(
254
- window.decodeURIComponent(e.data.data.url),
255
- e.origin
341
+ window.decodeURIComponent(t.data.data.url),
342
+ t.origin
256
343
  );
257
- for (let c of t)
258
- n.searchParams.delete(c);
259
- console.log("tempUrl", n);
260
- const o = m(e.data.data.name), r = new URL(o);
261
- r.searchParams.forEach((c, i) => {
262
- i.startsWith(s.URL_PARAM_PREFIX) && r.searchParams.delete(i);
263
- }), console.log("mainURL.href", r.href);
264
- const a = C(
344
+ for (let l of e)
345
+ n.searchParams.delete(l);
346
+ i.log("tempUrl", n);
347
+ const o = f(t.data.data.name), r = new URL(o);
348
+ r.searchParams.forEach((l, s) => {
349
+ s.startsWith(c.URL_PARAM_PREFIX) && r.searchParams.delete(s);
350
+ }), i.log("mainURL.href", r.href);
351
+ const a = S(
265
352
  r.href,
266
- e.data.data.name,
353
+ t.data.data.name,
267
354
  n.href
268
355
  // tempUrl.pathname + tempUrl.search + tempUrl.hash
269
356
  );
270
- console.log("curUrl", a), window.history.replaceState(null, "", a);
271
- } else if (e.data.cmd === "$xt/micro/info") {
272
- console.debug("parent 「$xt/micro/info」 receive message", e.data);
273
- const t = _(e.data.data);
274
- e.source.postMessage(
357
+ i.log("curUrl", a), window.history.replaceState(null, "", a);
358
+ } else if (t.data.cmd === "$xt/micro/info") {
359
+ i.debug("parent 「$xt/micro/info」 receive message", t.data);
360
+ const e = I(t.data.data);
361
+ t.source.postMessage(
275
362
  {
276
363
  cmd: "$xt/micro/info",
277
364
  data: {
@@ -286,74 +373,74 @@ const ee = (e) => {
286
373
  port: window.location.port,
287
374
  protocol: window.location.protocol
288
375
  },
289
- mainMicroVersion: w,
290
- name: t.name,
291
- url: t.url,
292
- params: t.params,
293
- sync: t.sync,
294
- preload: t.preload
376
+ mainMicroVersion: _,
377
+ name: e.name,
378
+ url: e.url,
379
+ params: e.params,
380
+ sync: e.sync,
381
+ preload: e.preload
295
382
  }
296
383
  },
297
- { targetOrigin: e.origin }
384
+ { targetOrigin: t.origin }
298
385
  );
299
386
  }
300
387
  });
301
- }, ne = (...t) => h(void 0, [...t], function* (e = {}) {
302
- window[s.MICRO_TYPE] = "child";
303
- const n = f(s.IS_MICRO), o = f(s.MICRO_NAME);
304
- if (console.debug("child microInit", n, o), n && (window[s.IS_MICRO] = n === "true"), o && S(o).then((r) => {
305
- window[s.MICRO_APP] = d({
306
- microVersion: w
388
+ }, me = (...e) => w(void 0, [...e], function* (t = {}) {
389
+ window[c.MICRO_TYPE] = "child";
390
+ const n = g(c.IS_MICRO), o = g(c.MICRO_NAME);
391
+ if (i.debug("child microInit", n, o), n && (window[c.IS_MICRO] = n === "true"), o && U(o).then((r) => {
392
+ window[c.MICRO_APP] = m({
393
+ microVersion: _
307
394
  }, r);
308
- }), window[s.IS_MICRO]) {
309
- let r = function(c) {
310
- var i = document.styleSheets[0];
311
- if (!i) {
312
- var l = document.createElement("style");
313
- document.head.appendChild(l), i = l.sheet;
395
+ }), window[c.IS_MICRO]) {
396
+ let r = function(l) {
397
+ var s = document.styleSheets[0];
398
+ if (!s) {
399
+ var d = document.createElement("style");
400
+ document.head.appendChild(d), s = d.sheet;
314
401
  }
315
- i.insertRule(c, i.cssRules.length);
402
+ s.insertRule(l, s.cssRules.length);
316
403
  };
317
- window[s.IS_MICRO] && e.hideHeaderCssSelector && (Array.isArray(e.hideHeaderCssSelector) ? e.hideHeaderCssSelector.forEach((c) => {
318
- r(`${c} { display: none !important; }`);
404
+ window[c.IS_MICRO] && t.hideHeaderCssSelector && (Array.isArray(t.hideHeaderCssSelector) ? t.hideHeaderCssSelector.forEach((l) => {
405
+ r(`${l} { display: none !important; }`);
319
406
  }) : r(
320
- `${e.hideHeaderCssSelector} { display: none !important; }`
321
- )), yield oe(o, window), window.addEventListener("message", (c) => h(void 0, null, function* () {
322
- c.data.cmd === `$xt/micro/${o}` && L(o);
407
+ `${t.hideHeaderCssSelector} { display: none !important; }`
408
+ )), yield te(o, window), window.addEventListener("message", (l) => w(void 0, null, function* () {
409
+ l.data.cmd === `$xt/micro/${o}` && T(o);
323
410
  }));
324
- const a = f(s.MICRO_TOKEN);
325
- e.authHandler && a && (yield e.authHandler(a));
411
+ const a = g(c.MICRO_TOKEN);
412
+ t.authHandler && a && (yield t.authHandler(a));
326
413
  } else
327
- console.warn("当前环境非微应用环境");
414
+ i.warn("当前环境非微应用环境");
328
415
  });
329
- function oe(e, t) {
330
- const n = t.history, o = n.pushState, r = n.replaceState;
331
- n.pushState = function(a, c, i) {
332
- const l = y(d(d({}, n.state || {}), a), {
416
+ function te(t, e) {
417
+ const n = e.history, o = n.pushState, r = n.replaceState;
418
+ n.pushState = function(a, l, s) {
419
+ const d = R(m(m({}, n.state || {}), a), {
333
420
  // 添加新数据
334
421
  _patchedByParent: !0
335
422
  // 可选:标记这是被父窗口修改的状态
336
423
  });
337
- o.call(n, l, c, i), L(e);
338
- }, n.replaceState = function(a, c, i) {
339
- const l = y(d(d({}, n.state || {}), a), {
424
+ o.call(n, d, l, s), T(t);
425
+ }, n.replaceState = function(a, l, s) {
426
+ const d = R(m(m({}, n.state || {}), a), {
340
427
  // 添加新数据
341
428
  _patchedByParent: !0
342
429
  // 可选:标记这是被父窗口修改的状态
343
430
  });
344
- r.call(n, l, c, i);
431
+ r.call(n, d, l, s);
345
432
  };
346
433
  }
347
- function L(e) {
348
- return h(this, null, function* () {
349
- if (console.log("syncUrlToWindow", e, window[s.MICRO_APP]), !window[s.MICRO_APP])
434
+ function T(t) {
435
+ return w(this, null, function* () {
436
+ if (i.log("syncUrlToWindow", t, window[c.MICRO_APP]), !window[c.MICRO_APP])
350
437
  return;
351
- const { name: t, sync: n, mainLocation: o } = window[s.MICRO_APP], r = window.location.pathname + window.location.search + window.location.hash;
352
- console.log("curUrl", r), window.parent.postMessage(
438
+ const { name: e, sync: n, mainLocation: o } = window[c.MICRO_APP], r = window.location.pathname + window.location.search + window.location.hash;
439
+ i.log("curUrl", r), window.parent.postMessage(
353
440
  {
354
441
  cmd: "$xt/micro/sync",
355
442
  data: {
356
- name: t,
443
+ name: e,
357
444
  url: window.encodeURIComponent(r)
358
445
  }
359
446
  },
@@ -361,105 +448,99 @@ function L(e) {
361
448
  );
362
449
  });
363
450
  }
364
- let u = {};
365
- const re = {
451
+ let p = {};
452
+ const ne = {
366
453
  url: "",
367
454
  name: "",
368
455
  sync: !0
369
- }, ae = (e) => {
370
- if (!e.url || e.url === "" || e.url === "undefined") {
371
- console.error("app url is required", e);
456
+ }, he = (t) => {
457
+ if (!t.url || t.url === "" || t.url === "undefined") {
458
+ i.error("app url is required", t);
372
459
  return;
373
460
  }
374
- const t = Object.assign({}, re, e), n = u[e.name];
375
- return n ? u[e.name] = d(d({}, n), t) : u[e.name] = y(d({}, t), {
376
- instance: new Z(e)
377
- }), u[e.name];
378
- }, _ = (e) => u[e], se = (e, t) => u[e.name] = d(d({}, e), t), b = (e, t) => new Promise((n, o) => {
379
- let r = _(e);
380
- r ? (t && (r.instance.changeOptions(t), r = se(r, t)), r.preload ? r.instance.preloadRender(r.el) : r.instance.mount(r.el), n(r)) : o("app not found, please setup app first");
381
- }), T = (e) => {
382
- let t = _(e);
383
- t && t.instance.unmount();
384
- }, ie = (e, t = !1) => {
385
- let n = _(e);
386
- n && n.instance.reload(t);
387
- }, m = (e) => {
388
- console.log("clearParentUrl", e, s.URL_PARAM_PREFIX + e);
389
- const t = window.location.href.split("#");
390
- console.log("url", t);
391
- let n = new URL(t[0]);
392
- n.searchParams.delete(s.URL_PARAM_PREFIX + e);
393
- const o = t.length > 1 ? n.href + "#" + t[1] : n.href;
461
+ const e = Object.assign({}, ne, t), n = p[t.name];
462
+ return n ? p[t.name] = m(m({}, n), e) : p[t.name] = R(m({}, e), {
463
+ instance: new J(t)
464
+ }), p[t.name];
465
+ }, I = (t) => p[t], oe = (t, e) => p[t.name] = m(m({}, t), e), re = (t, e) => new Promise((n, o) => {
466
+ let r = I(t);
467
+ r ? (e && (r.instance.changeOptions(e), r = oe(r, e)), r.preload ? r.instance.preloadRender(r.el) : r.instance.mount(r.el), n(r)) : o("app not found, please setup app first");
468
+ }), ae = (t) => {
469
+ let e = I(t);
470
+ e && e.instance.unmount();
471
+ }, ue = (t, e = !1) => {
472
+ let n = I(t);
473
+ n && n.instance.reload(e);
474
+ }, f = (t) => {
475
+ i.log(
476
+ "clearParentUrl",
477
+ t,
478
+ c.URL_PARAM_PREFIX + t
479
+ );
480
+ const e = window.location.href.split("#");
481
+ i.log("url", e);
482
+ let n = new URL(e[0]);
483
+ n.searchParams.delete(c.URL_PARAM_PREFIX + t);
484
+ const o = e.length > 1 ? n.href + "#" + e[1] : n.href;
394
485
  return window.history.replaceState({}, "", o), o;
395
- }, v = () => window[s.IS_MICRO] === !0, ce = () => window[s.MICRO_TYPE] === "parent", le = (e, t = ((n) => (n = window.__XT_MICRO_APP) == null ? void 0 : n.name)()) => {
486
+ }, ie = () => window[c.IS_MICRO] === !0, pe = () => window[c.MICRO_TYPE] === "parent", fe = (t, e = ((n) => (n = window.__XT_MICRO_APP) == null ? void 0 : n.name)()) => {
396
487
  let o = window.location;
397
- if (v())
488
+ if (ie())
398
489
  o = window.__XT_MICRO_APP.mainLocation;
399
- else if (!t) {
400
- console.error("在主应用中需要传入应用名称");
490
+ else if (!e) {
491
+ i.error("在主应用中需要传入应用名称");
401
492
  return;
402
493
  }
403
- return `${o.origin}${o.pathname}?__XT_MICRO_${t}=${encodeURIComponent(e)}${o.hash}`;
404
- }, de = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
405
- __proto__: null,
406
- clearParentUrl: m,
407
- createAppUrl: le,
408
- getApp: _,
409
- isMicro: v,
410
- isParent: ce,
411
- microInit: ne,
412
- reloadApp: ie,
413
- setupApp: ae,
414
- startApp: b,
415
- stopApp: T
416
- }, Symbol.toStringTag, { value: "Module" })), me = /* @__PURE__ */ N({
494
+ return `${o.origin}${o.pathname}?__XT_MICRO_${e}=${encodeURIComponent(t)}${o.hash}`;
495
+ }, se = /* @__PURE__ */ j({
417
496
  __name: "XTMicroView",
418
497
  props: {
419
498
  appId: String
420
499
  },
421
- setup(e) {
422
- const t = e, n = O(), o = () => {
423
- t.appId && b(t.appId, {
500
+ setup(t) {
501
+ const e = t, n = C(), o = () => {
502
+ e.appId && re(e.appId, {
424
503
  el: n.value,
425
504
  onUnload(r, a) {
426
- console.log("on unload", a.app.name), m(a.app.name);
505
+ i.log("on unload", a.app.name), f(a.app.name);
427
506
  }
428
507
  }).then((r) => {
429
- console.debug("app mounted", t.appId);
508
+ i.debug("app mounted", e.appId);
430
509
  });
431
510
  };
432
- return F(() => t.appId, (r, a) => {
433
- r !== a && a && T(a), o();
434
- }), B(() => {
511
+ return F(() => e.appId, (r, a) => {
512
+ r !== a && a && ae(a), o();
513
+ }), K(() => {
435
514
  o();
436
- }), V(() => {
437
- console.debug("「micro」", "app unmount", t.appId), m(t.appId);
438
- }), (r, a) => (D(), K("div", Y({
515
+ }), N(() => {
516
+ i.debug("「micro」", "app unmount", e.appId), f(e.appId);
517
+ }), (r, a) => (Y(), B("div", V({
439
518
  ref_key: "refPanel",
440
519
  ref: n
441
520
  }, r.$attrs, { class: "xt-micro" }), null, 16));
442
521
  }
443
522
  });
444
- window[s.MICRO_VERSION] = w;
445
- window.api = de;
446
- const fe = {
447
- install: (e, t = {}) => {
448
- e.component("xt-micro-view", me), te();
523
+ window[c.MICRO_VERSION] = _;
524
+ window.micro_api = {
525
+ debug: i.setEnable
526
+ };
527
+ const we = {
528
+ install: (t, e = {}) => {
529
+ t.component("xt-micro-view", se), ee();
449
530
  }
450
531
  };
451
532
  export {
452
- me as XTMicroView,
453
- m as clearParentUrl,
454
- le as createAppUrl,
455
- fe as default,
456
- _ as getApp,
457
- v as isMicro,
458
- ce as isParent,
459
- ne as microInit,
460
- ie as reloadApp,
461
- ae as setupApp,
462
- b as startApp,
463
- T as stopApp,
464
- he as utils
533
+ se as XTMicroView,
534
+ f as clearParentUrl,
535
+ fe as createAppUrl,
536
+ we as default,
537
+ I as getApp,
538
+ ie as isMicro,
539
+ pe as isParent,
540
+ me as microInit,
541
+ ue as reloadApp,
542
+ he as setupApp,
543
+ re as startApp,
544
+ ae as stopApp,
545
+ de as utils
465
546
  };
@@ -14,3 +14,40 @@ export declare function debounce<T extends (...args: any[]) => any>(func: T, del
14
14
  */
15
15
  export declare function addOrReplaceUrlParam(url: string, paramName: string, paramValue: string): string;
16
16
  export declare const ipcSyncAppInfo: (microName: string) => Promise<unknown>;
17
+ export declare class ConsoleUtil {
18
+ private static readonly STORAGE_KEY;
19
+ /**
20
+ * 从 localStorage 读取开关状态,默认开启
21
+ */
22
+ private static get enable();
23
+ /**
24
+ * 设置开关状态并同步到 localStorage
25
+ * @param status 是否开启日志
26
+ */
27
+ static setEnable(status: boolean): void;
28
+ /**
29
+ * 获取当前日志开关状态
30
+ * @returns 当前开关状态
31
+ */
32
+ static getEnable(): boolean;
33
+ /**
34
+ * 提取调用日志的文件路径和行号
35
+ * @returns 格式化的位置信息(如 "src/utils/xxx.ts:123")
36
+ */
37
+ private static getCallerInfo;
38
+ /**
39
+ * 通用日志输出方法(内部使用)
40
+ * @param type 日志类型(log/info/warn/error 等)
41
+ * @param args 日志参数
42
+ */
43
+ private static _log;
44
+ static log(...args: any[]): void;
45
+ static info(...args: any[]): void;
46
+ static warn(...args: any[]): void;
47
+ static error(...args: any[]): void;
48
+ static debug(...args: any[]): void;
49
+ static trace(...args: any[]): void;
50
+ static table(data: any, columns?: string[]): void;
51
+ static time(label: string): void;
52
+ static timeEnd(label: string): void;
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xingtukeji/micro",
3
- "version": "1.1.19",
3
+ "version": "1.1.20",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "main": "dist/index.js",