@xingtukeji/micro 1.1.19 → 1.1.21
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 +357 -273
- package/dist/types/utils.d.ts +37 -0
- package/package.json +2 -2
- package/src/index.ts +22 -0
package/dist/index.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
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 O = 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 (O)
|
|
10
|
+
for (var n of O(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
|
+
c(n.next(d));
|
|
19
|
+
} catch (y) {
|
|
20
|
+
r(y);
|
|
21
21
|
}
|
|
22
|
-
},
|
|
22
|
+
}, l = (d) => {
|
|
23
23
|
try {
|
|
24
|
-
|
|
25
|
-
} catch (
|
|
26
|
-
r(
|
|
24
|
+
c(n.throw(d));
|
|
25
|
+
} catch (y) {
|
|
26
|
+
r(y);
|
|
27
27
|
}
|
|
28
|
-
},
|
|
29
|
-
|
|
28
|
+
}, c = (d) => d.done ? o(d.value) : Promise.resolve(d.value).then(a, l);
|
|
29
|
+
c((n = n.apply(t, e)).next());
|
|
30
30
|
});
|
|
31
|
-
import { ref as
|
|
31
|
+
import { ref as b, defineComponent as j, watch as F, onMounted as K, onUnmounted as Y, openBlock as N, createElementBlock as V, mergeProps as B } from "vue";
|
|
32
32
|
const s = {
|
|
33
33
|
KEY: "$xtm",
|
|
34
34
|
// 父应用注入子应用实例的key
|
|
@@ -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
|
+
}, f = "1.1.21";
|
|
52
|
+
function _(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 S(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 U(t, e, n) {
|
|
82
|
+
const [o, r] = t.split("#"), a = new URL(o), l = a.searchParams;
|
|
83
|
+
return e = s.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 E = (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 !== f && i.warn(
|
|
89
|
+
`子应用版本与主应用版本不一致,${t}子应用版本:${f},主应用版本:${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, c] = a;
|
|
135
|
+
return `[${l.split("/").slice(-2).join("/")}:${c}]`;
|
|
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
|
-
|
|
187
|
+
ConsoleUtil: i,
|
|
188
|
+
addOrReplaceUrlParam: U,
|
|
189
|
+
anchorElementGenerator: D,
|
|
190
|
+
debounce: W,
|
|
191
|
+
getAbsolutePath: S,
|
|
192
|
+
getAnchorElementQueryMap: G,
|
|
193
|
+
getQueryParameter: _,
|
|
194
|
+
ipcSyncAppInfo: E
|
|
195
|
+
}, Symbol.toStringTag, { value: "Module" })), C = b(!1);
|
|
109
196
|
Object.defineProperty(window, s.IS_MICRO, {
|
|
110
197
|
get: function() {
|
|
111
|
-
return
|
|
198
|
+
return C.value;
|
|
112
199
|
},
|
|
113
|
-
set: function(
|
|
114
|
-
|
|
200
|
+
set: function(t) {
|
|
201
|
+
C.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) => `${s.DOM_ID_PREFIX}${t}`, L = (t, e = !1) => {
|
|
205
|
+
function n(c) {
|
|
206
|
+
return c !== null && typeof c == "object" && !Array.isArray(c);
|
|
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 = _(s.URL_PARAM_PREFIX + t.name);
|
|
211
|
+
if (!e && l) {
|
|
212
|
+
const c = S(l, t.url, !0);
|
|
213
|
+
if (c) {
|
|
214
|
+
const d = new URL(c, 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(s.IS_MICRO, !0 + ""), a.searchParams.set(s.MICRO_NAME,
|
|
135
|
-
for (let
|
|
136
|
-
a.searchParams.set(
|
|
137
|
-
return
|
|
138
|
-
},
|
|
139
|
-
const
|
|
140
|
-
return
|
|
141
|
-
},
|
|
142
|
-
|
|
143
|
-
|
|
219
|
+
g(t.name);
|
|
220
|
+
}
|
|
221
|
+
if (a.searchParams.set(s.IS_MICRO, !0 + ""), a.searchParams.set(s.MICRO_NAME, t.name), r && a.searchParams.set(s.MICRO_TOKEN, r), o)
|
|
222
|
+
for (let c of Object.keys(o))
|
|
223
|
+
a.searchParams.set(c, o[c]);
|
|
224
|
+
return g(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), g(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[s.MICRO_TYPE] = "parent", window.addEventListener("message", (
|
|
247
|
-
if (
|
|
248
|
-
|
|
249
|
-
const
|
|
332
|
+
}, ee = () => {
|
|
333
|
+
window[s.MICRO_TYPE] = "parent", window.addEventListener("message", (t) => {
|
|
334
|
+
if (t.data.cmd === "$xt/micro/sync") {
|
|
335
|
+
i.log("父页面接受子应用同步路由信息", t.data);
|
|
336
|
+
const e = [
|
|
250
337
|
s.IS_MICRO,
|
|
251
338
|
s.MICRO_NAME,
|
|
252
339
|
s.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 = g(t.data.data.name), r = new URL(o);
|
|
348
|
+
r.searchParams.forEach((l, c) => {
|
|
349
|
+
c.startsWith(s.URL_PARAM_PREFIX) && r.searchParams.delete(c);
|
|
350
|
+
}), i.log("mainURL.href", r.href);
|
|
351
|
+
const a = U(
|
|
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,77 @@ 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: f,
|
|
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
|
-
},
|
|
388
|
+
}, me = (...e) => w(void 0, [...e], function* (t = {}) {
|
|
302
389
|
window[s.MICRO_TYPE] = "child";
|
|
303
|
-
const n =
|
|
304
|
-
if (
|
|
305
|
-
window[s.MICRO_APP] =
|
|
306
|
-
microVersion:
|
|
390
|
+
const n = _(s.IS_MICRO), o = _(s.MICRO_NAME);
|
|
391
|
+
if (i.debug("child microInit", n, o), n && (window[s.IS_MICRO] = n === "true"), o && E(o).then((r) => {
|
|
392
|
+
window[s.MICRO_APP] = m({
|
|
393
|
+
microVersion: f
|
|
307
394
|
}, r);
|
|
308
395
|
}), window[s.IS_MICRO]) {
|
|
309
|
-
let r = function(
|
|
310
|
-
var
|
|
311
|
-
if (!
|
|
312
|
-
var
|
|
313
|
-
document.head.appendChild(
|
|
396
|
+
let r = function(l) {
|
|
397
|
+
var c = document.styleSheets[0];
|
|
398
|
+
if (!c) {
|
|
399
|
+
var d = document.createElement("style");
|
|
400
|
+
document.head.appendChild(d), c = d.sheet;
|
|
314
401
|
}
|
|
315
|
-
|
|
402
|
+
c.insertRule(l, c.cssRules.length);
|
|
316
403
|
};
|
|
317
|
-
window[s.IS_MICRO] &&
|
|
318
|
-
r(`${
|
|
404
|
+
window[s.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 = _(s.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, c) {
|
|
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
|
|
338
|
-
}, n.replaceState = function(a,
|
|
339
|
-
const
|
|
424
|
+
o.call(n, d, l, c), T(t);
|
|
425
|
+
}, n.replaceState = function(a, l, c) {
|
|
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
|
|
431
|
+
r.call(n, d, l, c);
|
|
345
432
|
};
|
|
346
433
|
}
|
|
347
|
-
function
|
|
348
|
-
return
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
434
|
+
function T(t) {
|
|
435
|
+
return w(this, null, function* () {
|
|
436
|
+
i.log("syncUrlToWindow", t, window[s.MICRO_APP]), window[s.MICRO_APP] || (yield E(t).then((a) => {
|
|
437
|
+
window[s.MICRO_APP] = m({
|
|
438
|
+
microVersion: f
|
|
439
|
+
}, a);
|
|
440
|
+
}));
|
|
441
|
+
const { name: e, sync: n, mainLocation: o } = window[s.MICRO_APP], r = window.location.pathname + window.location.search + window.location.hash;
|
|
442
|
+
i.log("curUrl", r), window.parent.postMessage(
|
|
353
443
|
{
|
|
354
444
|
cmd: "$xt/micro/sync",
|
|
355
445
|
data: {
|
|
356
|
-
name:
|
|
446
|
+
name: e,
|
|
357
447
|
url: window.encodeURIComponent(r)
|
|
358
448
|
}
|
|
359
449
|
},
|
|
@@ -361,105 +451,99 @@ function L(e) {
|
|
|
361
451
|
);
|
|
362
452
|
});
|
|
363
453
|
}
|
|
364
|
-
let
|
|
365
|
-
const
|
|
454
|
+
let p = {};
|
|
455
|
+
const ne = {
|
|
366
456
|
url: "",
|
|
367
457
|
name: "",
|
|
368
458
|
sync: !0
|
|
369
|
-
},
|
|
370
|
-
if (!
|
|
371
|
-
|
|
459
|
+
}, he = (t) => {
|
|
460
|
+
if (!t.url || t.url === "" || t.url === "undefined") {
|
|
461
|
+
i.error("app url is required", t);
|
|
372
462
|
return;
|
|
373
463
|
}
|
|
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
|
|
464
|
+
const e = Object.assign({}, ne, t), n = p[t.name];
|
|
465
|
+
return n ? p[t.name] = m(m({}, n), e) : p[t.name] = R(m({}, e), {
|
|
466
|
+
instance: new J(t)
|
|
467
|
+
}), p[t.name];
|
|
468
|
+
}, I = (t) => p[t], oe = (t, e) => p[t.name] = m(m({}, t), e), re = (t, e) => new Promise((n, o) => {
|
|
469
|
+
let r = I(t);
|
|
470
|
+
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");
|
|
471
|
+
}), ae = (t) => {
|
|
472
|
+
let e = I(t);
|
|
473
|
+
e && e.instance.unmount();
|
|
474
|
+
}, ue = (t, e = !1) => {
|
|
475
|
+
let n = I(t);
|
|
476
|
+
n && n.instance.reload(e);
|
|
477
|
+
}, g = (t) => {
|
|
478
|
+
i.log(
|
|
479
|
+
"clearParentUrl",
|
|
480
|
+
t,
|
|
481
|
+
s.URL_PARAM_PREFIX + t
|
|
482
|
+
);
|
|
483
|
+
const e = window.location.href.split("#");
|
|
484
|
+
i.log("url", e);
|
|
485
|
+
let n = new URL(e[0]);
|
|
486
|
+
n.searchParams.delete(s.URL_PARAM_PREFIX + t);
|
|
487
|
+
const o = e.length > 1 ? n.href + "#" + e[1] : n.href;
|
|
394
488
|
return window.history.replaceState({}, "", o), o;
|
|
395
|
-
},
|
|
489
|
+
}, ie = () => window[s.IS_MICRO] === !0, pe = () => window[s.MICRO_TYPE] === "parent", fe = (t, e = ((n) => (n = window.__XT_MICRO_APP) == null ? void 0 : n.name)()) => {
|
|
396
490
|
let o = window.location;
|
|
397
|
-
if (
|
|
491
|
+
if (ie())
|
|
398
492
|
o = window.__XT_MICRO_APP.mainLocation;
|
|
399
|
-
else if (!
|
|
400
|
-
|
|
493
|
+
else if (!e) {
|
|
494
|
+
i.error("在主应用中需要传入应用名称");
|
|
401
495
|
return;
|
|
402
496
|
}
|
|
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({
|
|
497
|
+
return `${o.origin}${o.pathname}?__XT_MICRO_${e}=${encodeURIComponent(t)}${o.hash}`;
|
|
498
|
+
}, se = /* @__PURE__ */ j({
|
|
417
499
|
__name: "XTMicroView",
|
|
418
500
|
props: {
|
|
419
501
|
appId: String
|
|
420
502
|
},
|
|
421
|
-
setup(
|
|
422
|
-
const
|
|
423
|
-
|
|
503
|
+
setup(t) {
|
|
504
|
+
const e = t, n = b(), o = () => {
|
|
505
|
+
e.appId && re(e.appId, {
|
|
424
506
|
el: n.value,
|
|
425
507
|
onUnload(r, a) {
|
|
426
|
-
|
|
508
|
+
i.log("on unload", a.app.name);
|
|
427
509
|
}
|
|
428
510
|
}).then((r) => {
|
|
429
|
-
|
|
511
|
+
i.debug("app mounted", e.appId);
|
|
430
512
|
});
|
|
431
513
|
};
|
|
432
|
-
return F(() =>
|
|
433
|
-
r !== a && a &&
|
|
434
|
-
}),
|
|
514
|
+
return F(() => e.appId, (r, a) => {
|
|
515
|
+
r !== a && a && ae(a), o();
|
|
516
|
+
}), K(() => {
|
|
435
517
|
o();
|
|
436
|
-
}),
|
|
437
|
-
|
|
438
|
-
}), (r, a) => (
|
|
518
|
+
}), Y(() => {
|
|
519
|
+
i.debug("「micro」", "app unmount", e.appId), g(e.appId);
|
|
520
|
+
}), (r, a) => (N(), V("div", B({
|
|
439
521
|
ref_key: "refPanel",
|
|
440
522
|
ref: n
|
|
441
523
|
}, r.$attrs, { class: "xt-micro" }), null, 16));
|
|
442
524
|
}
|
|
443
525
|
});
|
|
444
|
-
window[s.MICRO_VERSION] =
|
|
445
|
-
window.
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
526
|
+
window[s.MICRO_VERSION] = f;
|
|
527
|
+
window.micro_api = {
|
|
528
|
+
debug: i.setEnable
|
|
529
|
+
};
|
|
530
|
+
const we = {
|
|
531
|
+
install: (t, e = {}) => {
|
|
532
|
+
t.component("xt-micro-view", se), ee();
|
|
449
533
|
}
|
|
450
534
|
};
|
|
451
535
|
export {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
536
|
+
se as XTMicroView,
|
|
537
|
+
g as clearParentUrl,
|
|
538
|
+
fe as createAppUrl,
|
|
539
|
+
we as default,
|
|
540
|
+
I as getApp,
|
|
541
|
+
ie as isMicro,
|
|
542
|
+
pe as isParent,
|
|
543
|
+
me as microInit,
|
|
544
|
+
ue as reloadApp,
|
|
545
|
+
he as setupApp,
|
|
546
|
+
re as startApp,
|
|
547
|
+
ae as stopApp,
|
|
548
|
+
de as utils
|
|
465
549
|
};
|
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
|
+
}
|
package/package.json
CHANGED
package/src/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { App } from "vue";
|
|
2
|
+
import XTMicroView from "./components/XTMicroView.vue";
|
|
3
|
+
|
|
4
|
+
import { mainMicroInit } from "./parent";
|
|
5
|
+
export * from "./api";
|
|
6
|
+
import * as utils from "./utils";
|
|
7
|
+
|
|
8
|
+
export { XTMicroView, utils };
|
|
9
|
+
import { version } from "../package.json";
|
|
10
|
+
import { CONSTANT } from "./types";
|
|
11
|
+
window[CONSTANT.MICRO_VERSION] = version;
|
|
12
|
+
//开放部分 api 直接在 window 上挂载
|
|
13
|
+
window["micro_api"] = {
|
|
14
|
+
debug: utils.ConsoleUtil.setEnable,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default {
|
|
18
|
+
install: (app: App, _options: any = {}) => {
|
|
19
|
+
app.component("xt-micro-view", XTMicroView);
|
|
20
|
+
mainMicroInit();
|
|
21
|
+
},
|
|
22
|
+
};
|