@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 +360 -279
- package/dist/types/utils.d.ts +37 -0
- package/package.json +1 -1
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
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
for (var n in
|
|
8
|
-
|
|
9
|
-
if (
|
|
10
|
-
for (var n of
|
|
11
|
-
|
|
12
|
-
return
|
|
13
|
-
},
|
|
14
|
-
var
|
|
15
|
-
var
|
|
16
|
-
var a = (
|
|
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
|
-
|
|
19
|
-
} catch (
|
|
20
|
-
r(
|
|
18
|
+
s(n.next(d));
|
|
19
|
+
} catch (y) {
|
|
20
|
+
r(y);
|
|
21
21
|
}
|
|
22
|
-
},
|
|
22
|
+
}, l = (d) => {
|
|
23
23
|
try {
|
|
24
|
-
|
|
25
|
-
} catch (
|
|
26
|
-
r(
|
|
24
|
+
s(n.throw(d));
|
|
25
|
+
} catch (y) {
|
|
26
|
+
r(y);
|
|
27
27
|
}
|
|
28
|
-
},
|
|
29
|
-
|
|
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
|
|
32
|
-
const
|
|
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
|
-
},
|
|
52
|
-
function
|
|
53
|
-
|
|
54
|
-
var n = new RegExp("[?&]" +
|
|
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
|
|
57
|
+
function b(t, e, n) {
|
|
58
58
|
try {
|
|
59
|
-
return
|
|
59
|
+
return t && (n && t.startsWith("#") ? t : new URL(t, e).href);
|
|
60
60
|
} catch (o) {
|
|
61
|
-
return
|
|
61
|
+
return t;
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
-
function
|
|
65
|
-
const
|
|
66
|
-
return
|
|
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
|
|
69
|
-
const
|
|
70
|
-
return [...new URLSearchParams(
|
|
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
|
|
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
|
-
|
|
78
|
-
},
|
|
77
|
+
t.apply(r, o);
|
|
78
|
+
}, e);
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
|
-
function
|
|
82
|
-
const [o, r] =
|
|
83
|
-
return
|
|
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
|
|
86
|
-
return new Promise((
|
|
85
|
+
const U = (t) => w(void 0, null, function* () {
|
|
86
|
+
return new Promise((e) => {
|
|
87
87
|
window.addEventListener("message", (n) => {
|
|
88
|
-
|
|
89
|
-
`子应用版本与主应用版本不一致,${
|
|
90
|
-
),
|
|
91
|
-
}),
|
|
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:
|
|
94
|
+
data: t
|
|
95
95
|
},
|
|
96
96
|
"*"
|
|
97
97
|
);
|
|
98
98
|
});
|
|
99
|
-
}),
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
|
198
|
+
return O.value;
|
|
112
199
|
},
|
|
113
|
-
set: function(
|
|
114
|
-
|
|
200
|
+
set: function(t) {
|
|
201
|
+
O.value = t;
|
|
115
202
|
}
|
|
116
203
|
});
|
|
117
|
-
const
|
|
118
|
-
function n(
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
const o = typeof
|
|
122
|
-
|
|
123
|
-
),
|
|
124
|
-
if (!
|
|
125
|
-
const
|
|
126
|
-
if (
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
a.searchParams.set(
|
|
130
|
-
}), a.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
|
-
|
|
133
|
-
}
|
|
134
|
-
if (a.searchParams.set(
|
|
135
|
-
for (let
|
|
136
|
-
a.searchParams.set(
|
|
137
|
-
return
|
|
138
|
-
},
|
|
139
|
-
const
|
|
140
|
-
return
|
|
141
|
-
},
|
|
142
|
-
|
|
143
|
-
|
|
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
|
-
},
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
const n =
|
|
149
|
-
|
|
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) =>
|
|
152
|
-
),
|
|
238
|
+
(r) => t.onError.call(t, r)
|
|
239
|
+
), e.addEventListener(
|
|
153
240
|
"unload",
|
|
154
|
-
(r) =>
|
|
241
|
+
(r) => t.onUnload.call(t, r)
|
|
155
242
|
);
|
|
156
243
|
const o = new MutationObserver((r) => {
|
|
157
|
-
|
|
158
|
-
Array.from(a.removedNodes).includes(
|
|
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(
|
|
248
|
+
return o.observe(t.panel.parentNode, {
|
|
162
249
|
childList: !0,
|
|
163
250
|
subtree: !1
|
|
164
|
-
}),
|
|
251
|
+
}), e;
|
|
165
252
|
};
|
|
166
|
-
class
|
|
167
|
-
constructor(
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return this.app =
|
|
174
|
-
}
|
|
175
|
-
mount(
|
|
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 (
|
|
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(
|
|
180
|
-
r || (r =
|
|
181
|
-
const a =
|
|
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
|
-
|
|
191
|
-
const
|
|
192
|
-
if (
|
|
193
|
-
this.app.preload ? (
|
|
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(
|
|
203
|
-
this.mount(
|
|
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(
|
|
298
|
+
reload(e = !1) {
|
|
212
299
|
let n = this.dom.src;
|
|
213
|
-
|
|
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(
|
|
218
|
-
|
|
304
|
+
changeOptions(e) {
|
|
305
|
+
i.log("changeOptions", this.app, e), this.app = m(m({}, this.app), e);
|
|
219
306
|
}
|
|
220
|
-
onLoad(
|
|
221
|
-
|
|
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(
|
|
224
|
-
|
|
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(
|
|
227
|
-
|
|
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
|
|
231
|
-
var
|
|
232
|
-
(n = (
|
|
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/${
|
|
321
|
+
cmd: `$xt/micro/${t.app.name}`,
|
|
235
322
|
data: {
|
|
236
|
-
name:
|
|
237
|
-
url:
|
|
238
|
-
preload:
|
|
239
|
-
sync:
|
|
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
|
-
},
|
|
246
|
-
window[
|
|
247
|
-
if (
|
|
248
|
-
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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(
|
|
255
|
-
|
|
341
|
+
window.decodeURIComponent(t.data.data.url),
|
|
342
|
+
t.origin
|
|
256
343
|
);
|
|
257
|
-
for (let
|
|
258
|
-
n.searchParams.delete(
|
|
259
|
-
|
|
260
|
-
const o =
|
|
261
|
-
r.searchParams.forEach((
|
|
262
|
-
|
|
263
|
-
}),
|
|
264
|
-
const a =
|
|
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
|
-
|
|
353
|
+
t.data.data.name,
|
|
267
354
|
n.href
|
|
268
355
|
// tempUrl.pathname + tempUrl.search + tempUrl.hash
|
|
269
356
|
);
|
|
270
|
-
|
|
271
|
-
} else if (
|
|
272
|
-
|
|
273
|
-
const
|
|
274
|
-
|
|
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:
|
|
290
|
-
name:
|
|
291
|
-
url:
|
|
292
|
-
params:
|
|
293
|
-
sync:
|
|
294
|
-
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:
|
|
384
|
+
{ targetOrigin: t.origin }
|
|
298
385
|
);
|
|
299
386
|
}
|
|
300
387
|
});
|
|
301
|
-
},
|
|
302
|
-
window[
|
|
303
|
-
const n =
|
|
304
|
-
if (
|
|
305
|
-
window[
|
|
306
|
-
microVersion:
|
|
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[
|
|
309
|
-
let r = function(
|
|
310
|
-
var
|
|
311
|
-
if (!
|
|
312
|
-
var
|
|
313
|
-
document.head.appendChild(
|
|
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
|
-
|
|
402
|
+
s.insertRule(l, s.cssRules.length);
|
|
316
403
|
};
|
|
317
|
-
window[
|
|
318
|
-
r(`${
|
|
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
|
-
`${
|
|
321
|
-
)), yield
|
|
322
|
-
|
|
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 =
|
|
325
|
-
|
|
411
|
+
const a = g(c.MICRO_TOKEN);
|
|
412
|
+
t.authHandler && a && (yield t.authHandler(a));
|
|
326
413
|
} else
|
|
327
|
-
|
|
414
|
+
i.warn("当前环境非微应用环境");
|
|
328
415
|
});
|
|
329
|
-
function
|
|
330
|
-
const n =
|
|
331
|
-
n.pushState = function(a,
|
|
332
|
-
const
|
|
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,
|
|
338
|
-
}, n.replaceState = function(a,
|
|
339
|
-
const
|
|
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,
|
|
431
|
+
r.call(n, d, l, s);
|
|
345
432
|
};
|
|
346
433
|
}
|
|
347
|
-
function
|
|
348
|
-
return
|
|
349
|
-
if (
|
|
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:
|
|
352
|
-
|
|
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:
|
|
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
|
|
365
|
-
const
|
|
451
|
+
let p = {};
|
|
452
|
+
const ne = {
|
|
366
453
|
url: "",
|
|
367
454
|
name: "",
|
|
368
455
|
sync: !0
|
|
369
|
-
},
|
|
370
|
-
if (!
|
|
371
|
-
|
|
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
|
|
375
|
-
return n ?
|
|
376
|
-
instance: new
|
|
377
|
-
}),
|
|
378
|
-
},
|
|
379
|
-
let r =
|
|
380
|
-
r ? (
|
|
381
|
-
}),
|
|
382
|
-
let
|
|
383
|
-
|
|
384
|
-
},
|
|
385
|
-
let n =
|
|
386
|
-
n && n.instance.reload(
|
|
387
|
-
},
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
const
|
|
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
|
-
},
|
|
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 (
|
|
488
|
+
if (ie())
|
|
398
489
|
o = window.__XT_MICRO_APP.mainLocation;
|
|
399
|
-
else if (!
|
|
400
|
-
|
|
490
|
+
else if (!e) {
|
|
491
|
+
i.error("在主应用中需要传入应用名称");
|
|
401
492
|
return;
|
|
402
493
|
}
|
|
403
|
-
return `${o.origin}${o.pathname}?__XT_MICRO_${
|
|
404
|
-
},
|
|
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(
|
|
422
|
-
const
|
|
423
|
-
|
|
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
|
-
|
|
505
|
+
i.log("on unload", a.app.name), f(a.app.name);
|
|
427
506
|
}
|
|
428
507
|
}).then((r) => {
|
|
429
|
-
|
|
508
|
+
i.debug("app mounted", e.appId);
|
|
430
509
|
});
|
|
431
510
|
};
|
|
432
|
-
return F(() =>
|
|
433
|
-
r !== a && a &&
|
|
434
|
-
}),
|
|
511
|
+
return F(() => e.appId, (r, a) => {
|
|
512
|
+
r !== a && a && ae(a), o();
|
|
513
|
+
}), K(() => {
|
|
435
514
|
o();
|
|
436
|
-
}),
|
|
437
|
-
|
|
438
|
-
}), (r, a) => (
|
|
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[
|
|
445
|
-
window.
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
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
|
};
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -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
|
+
}
|