@xingtukeji/micro 1.1.23 → 1.1.25
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 +260 -253
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
(function(){try{if(typeof document<`u`){var e=document.createElement(`style`);e.appendChild(document.createTextNode(`.xt-micro{background-color:transparent;width:100%;height:100%;overflow:hidden}.micro-iframe{border:none;width:100%;height:100%;margin:0;padding:0}`)),document.head.appendChild(e)}}catch(e){console.error(`vite-plugin-css-injected-by-js`,e)}})();import { createElementBlock, defineComponent, mergeProps, onMounted, onUnmounted, openBlock, ref, watch } from "vue";
|
|
2
|
-
var __defProp = Object.defineProperty, __export = (
|
|
3
|
-
let
|
|
4
|
-
for (var
|
|
5
|
-
get:
|
|
2
|
+
var __defProp = Object.defineProperty, __export = (g) => {
|
|
3
|
+
let B = {};
|
|
4
|
+
for (var V in g) __defProp(B, V, {
|
|
5
|
+
get: g[V],
|
|
6
6
|
enumerable: !0
|
|
7
7
|
});
|
|
8
|
-
return
|
|
8
|
+
return B;
|
|
9
9
|
};
|
|
10
10
|
const CONSTANT = {
|
|
11
11
|
KEY: "$xtm",
|
|
@@ -17,7 +17,7 @@ const CONSTANT = {
|
|
|
17
17
|
DOM_ID_PREFIX: "xt-micro-ifr-",
|
|
18
18
|
URL_PARAM_PREFIX: "__XT_MICRO_",
|
|
19
19
|
MICRO_VERSION: "__XT_MICRO_VERSION"
|
|
20
|
-
}, version = "1.1.
|
|
20
|
+
}, version = "1.1.24";
|
|
21
21
|
var utils_exports = /* @__PURE__ */ __export({
|
|
22
22
|
ConsoleUtil: () => ConsoleUtil,
|
|
23
23
|
addOrReplaceUrlParam: () => addOrReplaceUrlParam,
|
|
@@ -28,239 +28,246 @@ var utils_exports = /* @__PURE__ */ __export({
|
|
|
28
28
|
getQueryParameter: () => getQueryParameter,
|
|
29
29
|
ipcSyncAppInfo: () => ipcSyncAppInfo
|
|
30
30
|
});
|
|
31
|
-
function getQueryParameter(
|
|
32
|
-
|
|
33
|
-
var
|
|
34
|
-
return
|
|
31
|
+
function getQueryParameter(g, B = window.location.href) {
|
|
32
|
+
g = g.replace(/[\[\]]/g, "\\$&");
|
|
33
|
+
var V = (/* @__PURE__ */ RegExp("[?&]" + g + "(=([^&#]*)|&|#|$)")).exec(B);
|
|
34
|
+
return V ? V[2] ? decodeURIComponent(V[2].replace(/\+/g, " ")).trim() : "" : null;
|
|
35
35
|
}
|
|
36
|
-
function getAbsolutePath(
|
|
36
|
+
function getAbsolutePath(g, B, V) {
|
|
37
37
|
try {
|
|
38
|
-
return
|
|
38
|
+
return g && (V && g.startsWith("#") ? g : new URL(g, B).href);
|
|
39
39
|
} catch {
|
|
40
|
-
return
|
|
40
|
+
return g;
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
-
function anchorElementGenerator(
|
|
44
|
-
let
|
|
45
|
-
return
|
|
43
|
+
function anchorElementGenerator(g) {
|
|
44
|
+
let B = window.document.createElement("a");
|
|
45
|
+
return B.href = g, B.href = B.href, B;
|
|
46
46
|
}
|
|
47
|
-
function getAnchorElementQueryMap(
|
|
48
|
-
let
|
|
49
|
-
return [...new URLSearchParams(
|
|
47
|
+
function getAnchorElementQueryMap(g) {
|
|
48
|
+
let B = g.search || "";
|
|
49
|
+
return [...new URLSearchParams(B).entries()].reduce((g, B) => (g[B[0]] = B[1], g), {});
|
|
50
50
|
}
|
|
51
|
-
function debounce(
|
|
52
|
-
let
|
|
53
|
-
return function(...
|
|
54
|
-
let
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
},
|
|
51
|
+
function debounce(g, B) {
|
|
52
|
+
let V = null;
|
|
53
|
+
return function(...H) {
|
|
54
|
+
let U = this;
|
|
55
|
+
V && clearTimeout(V), V = setTimeout(() => {
|
|
56
|
+
g.apply(U, H);
|
|
57
|
+
}, B);
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
|
-
function addOrReplaceUrlParam(
|
|
61
|
-
let [
|
|
62
|
-
return
|
|
60
|
+
function addOrReplaceUrlParam(g, B, V) {
|
|
61
|
+
let [H, U] = g.split("#"), W = new URL(H), G = W.searchParams;
|
|
62
|
+
return B = CONSTANT.URL_PARAM_PREFIX + B, G.has(B) ? G.set(B, V) : G.append(B, V), U && (W.hash = U), W.search = G.toString(), ConsoleUtil.trace("urlObj", W), W.origin + W.pathname + W.search + W.hash;
|
|
63
63
|
}
|
|
64
|
-
const ipcSyncAppInfo = async (
|
|
65
|
-
window.addEventListener("message", (
|
|
66
|
-
ConsoleUtil.trace("child ipcSyncAppInfo receive message",
|
|
67
|
-
}), ConsoleUtil.trace("child ipcSyncAppInfo send message",
|
|
64
|
+
const ipcSyncAppInfo = async (g) => new Promise((B) => {
|
|
65
|
+
window.addEventListener("message", (V) => {
|
|
66
|
+
ConsoleUtil.trace("child ipcSyncAppInfo receive message", V.data), V.data.cmd === "$xt/micro/info" && (V.data.data.mainMicroVersion !== "1.1.24" && ConsoleUtil.warn(`子应用版本与主应用版本不一致,${g}子应用版本:${version},主应用版本:${V.data.data.mainMicroVersion}`), B(V.data.data));
|
|
67
|
+
}), ConsoleUtil.trace("child ipcSyncAppInfo send message", g), window.parent.postMessage({
|
|
68
68
|
cmd: "$xt/micro/info",
|
|
69
|
-
data:
|
|
69
|
+
data: g
|
|
70
70
|
}, "*");
|
|
71
71
|
});
|
|
72
|
-
var ConsoleUtil = class
|
|
72
|
+
var ConsoleUtil = class g {
|
|
73
73
|
static STORAGE_KEY = "@xt/micro/console/enable";
|
|
74
74
|
static get enable() {
|
|
75
|
-
return localStorage.getItem(
|
|
75
|
+
return localStorage.getItem(g.STORAGE_KEY) === "true";
|
|
76
76
|
}
|
|
77
|
-
static setEnable(
|
|
78
|
-
localStorage.setItem(
|
|
77
|
+
static setEnable(B) {
|
|
78
|
+
localStorage.setItem(g.STORAGE_KEY, B.toString());
|
|
79
79
|
}
|
|
80
80
|
static getEnable() {
|
|
81
|
-
return
|
|
81
|
+
return g.enable;
|
|
82
82
|
}
|
|
83
83
|
static getCallerInfo() {
|
|
84
84
|
try {
|
|
85
|
-
let
|
|
86
|
-
if (
|
|
87
|
-
let
|
|
88
|
-
if (
|
|
89
|
-
let [,
|
|
90
|
-
return `[${
|
|
85
|
+
let g = /* @__PURE__ */ ((/* @__PURE__ */ Error()).stack || "").split("\n").slice(4);
|
|
86
|
+
if (g.length === 0) return "unknown location";
|
|
87
|
+
let B = g[0].trim(), V = B.match(/(?:http[s]?:\/\/.*?\/|)([^:]+):(\d+):(\d+)/);
|
|
88
|
+
if (V) {
|
|
89
|
+
let [, g, B] = V;
|
|
90
|
+
return `[${g.split("/").slice(-2).join("/")}:${B}]`;
|
|
91
91
|
}
|
|
92
|
-
return `[${
|
|
92
|
+
return `[${B}]`;
|
|
93
93
|
} catch {
|
|
94
94
|
return "[unknown location]";
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
static _log(
|
|
98
|
-
if (
|
|
99
|
-
let
|
|
100
|
-
console[
|
|
97
|
+
static _log(B, ...V) {
|
|
98
|
+
if (g.enable && typeof console[B] == "function") {
|
|
99
|
+
let g = this.getCallerInfo();
|
|
100
|
+
console[B](g, ...V);
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
-
static log(...
|
|
104
|
-
this._log("log", ...
|
|
103
|
+
static log(...g) {
|
|
104
|
+
this._log("log", ...g);
|
|
105
105
|
}
|
|
106
|
-
static info(...
|
|
107
|
-
this._log("info", ...
|
|
106
|
+
static info(...g) {
|
|
107
|
+
this._log("info", ...g);
|
|
108
108
|
}
|
|
109
|
-
static warn(...
|
|
110
|
-
this._log("warn", ...
|
|
109
|
+
static warn(...g) {
|
|
110
|
+
this._log("warn", ...g);
|
|
111
111
|
}
|
|
112
|
-
static error(...
|
|
113
|
-
this._log("error", ...
|
|
112
|
+
static error(...g) {
|
|
113
|
+
this._log("error", ...g);
|
|
114
114
|
}
|
|
115
|
-
static debug(...
|
|
116
|
-
this._log("debug", ...
|
|
115
|
+
static debug(...g) {
|
|
116
|
+
this._log("debug", ...g);
|
|
117
117
|
}
|
|
118
|
-
static trace(...
|
|
119
|
-
this._log("trace", ...
|
|
118
|
+
static trace(...g) {
|
|
119
|
+
this._log("trace", ...g);
|
|
120
120
|
}
|
|
121
|
-
static table(
|
|
122
|
-
this.enable && console.table(
|
|
121
|
+
static table(g, B) {
|
|
122
|
+
this.enable && console.table(g, B);
|
|
123
123
|
}
|
|
124
|
-
static time(
|
|
125
|
-
this.enable && console.time(
|
|
124
|
+
static time(g) {
|
|
125
|
+
this.enable && console.time(g);
|
|
126
126
|
}
|
|
127
|
-
static timeEnd(
|
|
128
|
-
this.enable && console.timeEnd(
|
|
127
|
+
static timeEnd(g) {
|
|
128
|
+
this.enable && console.timeEnd(g);
|
|
129
129
|
}
|
|
130
|
-
}, _isMicro = ref(!1);
|
|
131
|
-
Object.defineProperty(window,
|
|
132
|
-
get:
|
|
133
|
-
|
|
130
|
+
}, _isMicro = ref(!1), descriptor = Object.getOwnPropertyDescriptor(window, CONSTANT.IS_MICRO);
|
|
131
|
+
descriptor ? descriptor.configurable ? Object.defineProperty(window, "__XT_MICRO", {
|
|
132
|
+
get: () => _isMicro.value,
|
|
133
|
+
set: (g) => {
|
|
134
|
+
_isMicro.value = g;
|
|
134
135
|
},
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
configurable: !0,
|
|
137
|
+
enumerable: !0
|
|
138
|
+
}) : console.warn("__XT_MICRO 不可配置,无法重定义,已跳过") : Object.defineProperty(window, "__XT_MICRO", {
|
|
139
|
+
get: () => _isMicro.value,
|
|
140
|
+
set: (g) => {
|
|
141
|
+
_isMicro.value = g;
|
|
142
|
+
},
|
|
143
|
+
configurable: !0,
|
|
144
|
+
enumerable: !0
|
|
138
145
|
});
|
|
139
|
-
var generateId = (
|
|
140
|
-
function
|
|
141
|
-
return typeof
|
|
146
|
+
var generateId = (g) => `${CONSTANT.DOM_ID_PREFIX}${g}`, generateURL = (g, B = !1) => {
|
|
147
|
+
function V(g) {
|
|
148
|
+
return typeof g == "object" && !!g && !Array.isArray(g);
|
|
142
149
|
}
|
|
143
|
-
let
|
|
144
|
-
if (!
|
|
145
|
-
let
|
|
146
|
-
if (
|
|
147
|
-
let
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}),
|
|
150
|
+
let H = typeof g.params == "function" ? g.params() : V(g.params) ? g.params : {}, U = typeof g.token == "function" ? g.token() : V(g.token) ? g.token : "", W = new URL(g.url.indexOf("http") > -1 ? g.url : window.location.origin + g.url), G = getQueryParameter(CONSTANT.URL_PARAM_PREFIX + g.name);
|
|
151
|
+
if (!B && G) {
|
|
152
|
+
let B = getAbsolutePath(G, g.url, !0);
|
|
153
|
+
if (B) {
|
|
154
|
+
let V = new URL(B, g.url);
|
|
155
|
+
V.searchParams.forEach((g, B) => {
|
|
156
|
+
W.searchParams.set(B, g);
|
|
157
|
+
}), W.hash = V.hash;
|
|
151
158
|
}
|
|
152
|
-
clearParentUrl(
|
|
159
|
+
clearParentUrl(g.name);
|
|
153
160
|
}
|
|
154
|
-
if (
|
|
155
|
-
return clearParentUrl(
|
|
156
|
-
}, generateLoading = (
|
|
157
|
-
ConsoleUtil.log("generateLoading",
|
|
158
|
-
let
|
|
159
|
-
return
|
|
160
|
-
}, removeLoading = (
|
|
161
|
-
|
|
162
|
-
document.querySelectorAll("#" +
|
|
163
|
-
|
|
161
|
+
if (W.searchParams.set(CONSTANT.IS_MICRO, "true"), W.searchParams.set(CONSTANT.MICRO_NAME, g.name), U && W.searchParams.set(CONSTANT.MICRO_TOKEN, U), H) for (let g of Object.keys(H)) W.searchParams.set(g, H[g]);
|
|
162
|
+
return clearParentUrl(g.name), W.href;
|
|
163
|
+
}, generateLoading = (g) => {
|
|
164
|
+
ConsoleUtil.log("generateLoading", g);
|
|
165
|
+
let B = document.createElement("div");
|
|
166
|
+
return B.id = generateId(g) + "-loading", B.innerHTML = "应用加载中...", B.style.textAlign = "center", B.style.lineHeight = "100px", B.style.fontSize = "20px", B.style.color = "#999", B.style.position = "absolute", B.style.top = "0", B.style.left = "0", B.style.right = "0", B.style.bottom = "0", B.style.zIndex = "999", B.style.background = "transparent", B.style["mix-blend-mode"] = "difference", B;
|
|
167
|
+
}, removeLoading = (g) => {
|
|
168
|
+
g.style.opacity = "0", setTimeout(() => {
|
|
169
|
+
document.querySelectorAll("#" + g.id).forEach((g) => {
|
|
170
|
+
g.remove();
|
|
164
171
|
});
|
|
165
172
|
}, 500);
|
|
166
|
-
}, generateIframe = (
|
|
167
|
-
let
|
|
168
|
-
|
|
169
|
-
let
|
|
170
|
-
ConsoleUtil.trace("监听 body 中子节点的变化2",
|
|
171
|
-
Array.from(
|
|
173
|
+
}, generateIframe = (g) => {
|
|
174
|
+
let B = document.createElement("iframe");
|
|
175
|
+
B.id = generateId(g.app.name), B.src = generateURL(g.app), B.style.width = "100%", B.style.height = "100%", B.style.border = "none", B.style.margin = "0", B.style.padding = "0", B.style.backgroundColor = "transparent", B.style["color-scheme"] = "unset", B.style.visibility = "hidden", B.addEventListener("load", (V) => (B.style.visibility = "visible", g.onLoad.call(g, V))), B.addEventListener("error", (B) => g.onError.call(g, B)), B.addEventListener("unload", (B) => g.onUnload.call(g, B));
|
|
176
|
+
let V = new MutationObserver((H) => {
|
|
177
|
+
ConsoleUtil.trace("监听 body 中子节点的变化2", g.panel), H.forEach((H) => {
|
|
178
|
+
Array.from(H.removedNodes).includes(B) && (g.onUnload(new Event("unload")), V.disconnect());
|
|
172
179
|
});
|
|
173
180
|
});
|
|
174
|
-
return
|
|
181
|
+
return V.observe(g.panel.parentNode, {
|
|
175
182
|
childList: !0,
|
|
176
183
|
subtree: !1
|
|
177
|
-
}),
|
|
184
|
+
}), B;
|
|
178
185
|
}, MicroApp = class {
|
|
179
186
|
app;
|
|
180
187
|
dom;
|
|
181
188
|
panel;
|
|
182
189
|
loading;
|
|
183
190
|
loadingTimer;
|
|
184
|
-
constructor(
|
|
185
|
-
return this.app =
|
|
191
|
+
constructor(g) {
|
|
192
|
+
return this.app = g, this.dom = void 0, this.panel = void 0, g.preload && this.preload(), this;
|
|
186
193
|
}
|
|
187
|
-
mount(
|
|
188
|
-
let
|
|
189
|
-
if (
|
|
190
|
-
this.panel =
|
|
191
|
-
let
|
|
192
|
-
if (
|
|
193
|
-
let
|
|
194
|
-
this.loading =
|
|
194
|
+
mount(g, B = !1) {
|
|
195
|
+
let V;
|
|
196
|
+
if (g ? (V = typeof g == "string" ? document.querySelector(g) : g, V.innerHTML = "") : V = document.body, V) {
|
|
197
|
+
this.panel = V;
|
|
198
|
+
let g = !0, H = document.getElementById(generateId(this.app.name));
|
|
199
|
+
if (H || (g = !1, H = generateIframe(this), this.dom = H), B ? H.style.display = "none" : H.style.removeProperty("display"), !B) {
|
|
200
|
+
let g = generateLoading(this.app.name);
|
|
201
|
+
this.loading = g, V.appendChild(g);
|
|
195
202
|
}
|
|
196
|
-
ConsoleUtil.trace("micro app", this.app.name,
|
|
203
|
+
ConsoleUtil.trace("micro app", this.app.name, g ? "已加载迁移 dom" : "首次加载"), V.appendChild(H);
|
|
197
204
|
} else throw Error("没有找到转载子元素的父容器");
|
|
198
205
|
}
|
|
199
206
|
unmount() {
|
|
200
207
|
ConsoleUtil.trace("micro unmount app", this.app.name);
|
|
201
|
-
let
|
|
202
|
-
if (
|
|
208
|
+
let g = document.getElementById(generateId(this.app.name));
|
|
209
|
+
if (g) this.app.preload ? (g.style.display = "none", document.body.appendChild(g)) : this.panel?.removeChild(g);
|
|
203
210
|
else throw Error("app dom not found");
|
|
204
211
|
}
|
|
205
|
-
preloadRender(
|
|
206
|
-
this.mount(
|
|
212
|
+
preloadRender(g) {
|
|
213
|
+
this.mount(g);
|
|
207
214
|
}
|
|
208
215
|
preload() {
|
|
209
216
|
this.mount(void 0, !0);
|
|
210
217
|
}
|
|
211
|
-
reload(
|
|
212
|
-
let
|
|
213
|
-
|
|
214
|
-
let
|
|
215
|
-
|
|
218
|
+
reload(g = !1) {
|
|
219
|
+
let B = this.dom.src;
|
|
220
|
+
g && (B = generateURL(this.app, !0), ConsoleUtil.trace("go home", B)), ConsoleUtil.trace("reload app", this.app, B);
|
|
221
|
+
let V = new URL(B);
|
|
222
|
+
V.searchParams.set("__t", Date.now().toString()), this.dom.style.visibility = "hidden", this.dom.src = V.toString();
|
|
216
223
|
}
|
|
217
|
-
changeOptions(
|
|
218
|
-
ConsoleUtil.trace("changeOptions", this.app,
|
|
224
|
+
changeOptions(g) {
|
|
225
|
+
ConsoleUtil.trace("changeOptions", this.app, g), this.app = {
|
|
219
226
|
...this.app,
|
|
220
|
-
...
|
|
227
|
+
...g
|
|
221
228
|
};
|
|
222
229
|
}
|
|
223
|
-
onLoad(
|
|
224
|
-
ConsoleUtil.trace("app onLoad", this.app.name, this.loadingTimer, this.loading), this.loadingTimer && clearTimeout(this.loadingTimer), this.loading && removeLoading(this.loading), _postMicro(this), ConsoleUtil.trace("this.app.onLoad", this.app.onLoad), this.app.onLoad && this.app.onLoad(
|
|
230
|
+
onLoad(g) {
|
|
231
|
+
ConsoleUtil.trace("app onLoad", this.app.name, this.loadingTimer, this.loading), this.loadingTimer && clearTimeout(this.loadingTimer), this.loading && removeLoading(this.loading), _postMicro(this), ConsoleUtil.trace("this.app.onLoad", this.app.onLoad), this.app.onLoad && this.app.onLoad(g, this);
|
|
225
232
|
}
|
|
226
|
-
onUnload(
|
|
227
|
-
ConsoleUtil.trace("app onUnload", this.app.name), clearParentUrl(this.app.name), this.app.onUnload && this.app.onUnload(
|
|
233
|
+
onUnload(g) {
|
|
234
|
+
ConsoleUtil.trace("app onUnload", this.app.name), clearParentUrl(this.app.name), this.app.onUnload && this.app.onUnload(g, this);
|
|
228
235
|
}
|
|
229
|
-
onError(
|
|
230
|
-
ConsoleUtil.error("app onError", this.app.name), this.app.onError && this.app.onError(
|
|
236
|
+
onError(g) {
|
|
237
|
+
ConsoleUtil.error("app onError", this.app.name), this.app.onError && this.app.onError(g, this);
|
|
231
238
|
}
|
|
232
|
-
}, _postMicro = (
|
|
233
|
-
|
|
234
|
-
cmd: `$xt/micro/${
|
|
239
|
+
}, _postMicro = (g) => {
|
|
240
|
+
g.dom?.contentWindow?.postMessage({
|
|
241
|
+
cmd: `$xt/micro/${g.app.name}`,
|
|
235
242
|
data: {
|
|
236
|
-
name:
|
|
237
|
-
url:
|
|
238
|
-
preload:
|
|
239
|
-
sync:
|
|
243
|
+
name: g.app.name,
|
|
244
|
+
url: g.app.url,
|
|
245
|
+
preload: g.app.preload,
|
|
246
|
+
sync: g.app.sync || !0
|
|
240
247
|
}
|
|
241
248
|
}, "*");
|
|
242
249
|
};
|
|
243
250
|
const mainMicroInit = () => {
|
|
244
|
-
window[CONSTANT.MICRO_TYPE] = "parent", window.addEventListener("message", (
|
|
245
|
-
if (
|
|
246
|
-
ConsoleUtil.trace("父页面接受子应用同步路由信息",
|
|
247
|
-
let
|
|
251
|
+
window[CONSTANT.MICRO_TYPE] = "parent", window.addEventListener("message", (g) => {
|
|
252
|
+
if (g.data.cmd === "$xt/micro/sync") {
|
|
253
|
+
ConsoleUtil.trace("父页面接受子应用同步路由信息", g.data);
|
|
254
|
+
let B = [
|
|
248
255
|
CONSTANT.IS_MICRO,
|
|
249
256
|
CONSTANT.MICRO_NAME,
|
|
250
257
|
CONSTANT.MICRO_TOKEN
|
|
251
|
-
],
|
|
252
|
-
for (let
|
|
253
|
-
ConsoleUtil.trace("tempUrl",
|
|
254
|
-
let
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}), ConsoleUtil.trace("mainURL.href",
|
|
258
|
-
let
|
|
259
|
-
ConsoleUtil.trace("curUrl",
|
|
260
|
-
} else if (
|
|
261
|
-
ConsoleUtil.trace("parent 「$xt/micro/info」 receive message",
|
|
262
|
-
let
|
|
263
|
-
|
|
258
|
+
], V = new URL(window.decodeURIComponent(g.data.data.url), g.origin);
|
|
259
|
+
for (let g of B) V.searchParams.delete(g);
|
|
260
|
+
ConsoleUtil.trace("tempUrl", V);
|
|
261
|
+
let H = clearParentUrl(g.data.data.name), U = new URL(H);
|
|
262
|
+
U.searchParams.forEach((g, B) => {
|
|
263
|
+
B.startsWith(CONSTANT.URL_PARAM_PREFIX) && U.searchParams.delete(B);
|
|
264
|
+
}), ConsoleUtil.trace("mainURL.href", U.href);
|
|
265
|
+
let W = addOrReplaceUrlParam(U.href, g.data.data.name, V.href);
|
|
266
|
+
ConsoleUtil.trace("curUrl", W), window.history.replaceState(null, "", W);
|
|
267
|
+
} else if (g.data.cmd === "$xt/micro/info") {
|
|
268
|
+
ConsoleUtil.trace("parent 「$xt/micro/info」 receive message", g.data);
|
|
269
|
+
let B = getApp(g.data.data);
|
|
270
|
+
g.source.postMessage({
|
|
264
271
|
cmd: "$xt/micro/info",
|
|
265
272
|
data: {
|
|
266
273
|
mainLocation: {
|
|
@@ -275,70 +282,70 @@ const mainMicroInit = () => {
|
|
|
275
282
|
protocol: window.location.protocol
|
|
276
283
|
},
|
|
277
284
|
mainMicroVersion: version,
|
|
278
|
-
name:
|
|
279
|
-
url:
|
|
280
|
-
params:
|
|
281
|
-
sync:
|
|
282
|
-
preload:
|
|
285
|
+
name: B.name,
|
|
286
|
+
url: B.url,
|
|
287
|
+
params: B.params,
|
|
288
|
+
sync: B.sync,
|
|
289
|
+
preload: B.preload
|
|
283
290
|
}
|
|
284
|
-
}, { targetOrigin:
|
|
291
|
+
}, { targetOrigin: g.origin });
|
|
285
292
|
}
|
|
286
293
|
});
|
|
287
|
-
}, microInit = async (
|
|
294
|
+
}, microInit = async (g = {}) => {
|
|
288
295
|
window[CONSTANT.MICRO_TYPE] = "child";
|
|
289
|
-
let
|
|
290
|
-
if (ConsoleUtil.trace("child microInit",
|
|
291
|
-
function
|
|
292
|
-
var
|
|
293
|
-
if (!
|
|
294
|
-
var
|
|
295
|
-
document.head.appendChild(
|
|
296
|
+
let B = getQueryParameter(CONSTANT.IS_MICRO), V = getQueryParameter(CONSTANT.MICRO_NAME);
|
|
297
|
+
if (ConsoleUtil.trace("child microInit", B, V), B && (window[CONSTANT.IS_MICRO] = B === "true", V && await syncAppInfo(V)), window[CONSTANT.IS_MICRO]) {
|
|
298
|
+
function B(g) {
|
|
299
|
+
var B = document.styleSheets[0];
|
|
300
|
+
if (!B) {
|
|
301
|
+
var V = document.createElement("style");
|
|
302
|
+
document.head.appendChild(V), B = V.sheet;
|
|
296
303
|
}
|
|
297
|
-
|
|
304
|
+
B.insertRule(g, B.cssRules.length);
|
|
298
305
|
}
|
|
299
|
-
window[CONSTANT.IS_MICRO] &&
|
|
300
|
-
|
|
301
|
-
}) :
|
|
302
|
-
|
|
306
|
+
window[CONSTANT.IS_MICRO] && g.hideHeaderCssSelector && (Array.isArray(g.hideHeaderCssSelector) ? g.hideHeaderCssSelector.forEach((g) => {
|
|
307
|
+
B(`${g} { display: none !important; }`);
|
|
308
|
+
}) : B(`${g.hideHeaderCssSelector} { display: none !important; }`)), await patchIframeHistory(V, window), window.addEventListener("message", async (g) => {
|
|
309
|
+
g.data.cmd === `$xt/micro/${V}` && syncUrlToWindow(V);
|
|
303
310
|
});
|
|
304
|
-
let
|
|
305
|
-
|
|
311
|
+
let H = getQueryParameter(CONSTANT.MICRO_TOKEN);
|
|
312
|
+
g.authHandler && H && await g.authHandler(H);
|
|
306
313
|
} else ConsoleUtil.warn("当前环境非微应用环境");
|
|
307
314
|
};
|
|
308
|
-
function patchIframeHistory(
|
|
309
|
-
let
|
|
310
|
-
|
|
311
|
-
let
|
|
312
|
-
...
|
|
313
|
-
...
|
|
315
|
+
function patchIframeHistory(g, B) {
|
|
316
|
+
let V = B.history, H = V.pushState, U = V.replaceState;
|
|
317
|
+
V.pushState = function(B, U, W) {
|
|
318
|
+
let G = {
|
|
319
|
+
...V.state || {},
|
|
320
|
+
...B,
|
|
314
321
|
_patchedByParent: !0
|
|
315
322
|
};
|
|
316
|
-
|
|
317
|
-
},
|
|
318
|
-
let
|
|
319
|
-
...
|
|
320
|
-
...
|
|
323
|
+
H.call(V, G, U, W), syncUrlToWindow(g);
|
|
324
|
+
}, V.replaceState = function(g, B, H) {
|
|
325
|
+
let W = {
|
|
326
|
+
...V.state || {},
|
|
327
|
+
...g,
|
|
321
328
|
_patchedByParent: !0
|
|
322
329
|
};
|
|
323
|
-
|
|
330
|
+
U.call(V, W, B, H);
|
|
324
331
|
};
|
|
325
332
|
}
|
|
326
|
-
var syncAppInfo = async (
|
|
333
|
+
var syncAppInfo = async (g) => await ipcSyncAppInfo(g).then((g) => {
|
|
327
334
|
window[CONSTANT.MICRO_APP] = {
|
|
328
335
|
microVersion: version,
|
|
329
|
-
...
|
|
336
|
+
...g
|
|
330
337
|
};
|
|
331
338
|
});
|
|
332
|
-
async function syncUrlToWindow(
|
|
333
|
-
ConsoleUtil.trace("syncUrlToWindow",
|
|
334
|
-
let { name:
|
|
335
|
-
ConsoleUtil.trace("curUrl",
|
|
339
|
+
async function syncUrlToWindow(g) {
|
|
340
|
+
ConsoleUtil.trace("syncUrlToWindow", g, window[CONSTANT.MICRO_APP]), window[CONSTANT.MICRO_APP] || syncAppInfo(g);
|
|
341
|
+
let { name: B, sync: V, mainLocation: H } = window[CONSTANT.MICRO_APP], U = window.location.pathname + window.location.search + window.location.hash;
|
|
342
|
+
ConsoleUtil.trace("curUrl", U), ConsoleUtil.trace("child", "「$xt/micro/sync」", "send", B, U), window.parent.postMessage({
|
|
336
343
|
cmd: "$xt/micro/sync",
|
|
337
344
|
data: {
|
|
338
|
-
name:
|
|
339
|
-
url: window.encodeURIComponent(
|
|
345
|
+
name: B,
|
|
346
|
+
url: window.encodeURIComponent(U)
|
|
340
347
|
}
|
|
341
|
-
},
|
|
348
|
+
}, H.origin);
|
|
342
349
|
}
|
|
343
350
|
var _apps = {}, defaultAppOption = {
|
|
344
351
|
url: "",
|
|
@@ -346,78 +353,78 @@ var _apps = {}, defaultAppOption = {
|
|
|
346
353
|
sync: !0,
|
|
347
354
|
preload: !1
|
|
348
355
|
};
|
|
349
|
-
const setupApp = (
|
|
350
|
-
if (!
|
|
351
|
-
ConsoleUtil.error("app url is required",
|
|
356
|
+
const setupApp = (g) => {
|
|
357
|
+
if (!g.url || g.url === "" || g.url === "undefined") {
|
|
358
|
+
ConsoleUtil.error("app url is required", g);
|
|
352
359
|
return;
|
|
353
360
|
}
|
|
354
|
-
let
|
|
355
|
-
return
|
|
361
|
+
let B = Object.assign({}, defaultAppOption, g), V = _apps[g.name];
|
|
362
|
+
return V ? _apps[g.name] = {
|
|
363
|
+
...V,
|
|
364
|
+
...B
|
|
365
|
+
} : _apps[g.name] = {
|
|
356
366
|
...B,
|
|
357
|
-
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
var setApp = (h, z) => _apps[h.name] = {
|
|
364
|
-
...h,
|
|
365
|
-
...z
|
|
367
|
+
instance: new MicroApp(g)
|
|
368
|
+
}, _apps[g.name];
|
|
369
|
+
}, getApp = (g) => _apps[g];
|
|
370
|
+
var setApp = (g, B) => _apps[g.name] = {
|
|
371
|
+
...g,
|
|
372
|
+
...B
|
|
366
373
|
};
|
|
367
|
-
const startApp = (
|
|
368
|
-
let
|
|
369
|
-
|
|
370
|
-
}), stopApp = (
|
|
371
|
-
let
|
|
372
|
-
|
|
373
|
-
}, reloadApp = (
|
|
374
|
-
let
|
|
375
|
-
|
|
376
|
-
}, clearParentUrl = (
|
|
377
|
-
ConsoleUtil.trace("clearParentUrl",
|
|
378
|
-
let
|
|
379
|
-
ConsoleUtil.trace("url",
|
|
380
|
-
let
|
|
381
|
-
|
|
382
|
-
let
|
|
383
|
-
return window.history.replaceState({}, "",
|
|
384
|
-
}, isMicro = () => window[CONSTANT.IS_MICRO] === !0, isParent = () => window[CONSTANT.MICRO_TYPE] === "parent", createAppUrl = (
|
|
385
|
-
let
|
|
386
|
-
if (isMicro())
|
|
387
|
-
else if (!
|
|
374
|
+
const startApp = (g, B) => new Promise((V, H) => {
|
|
375
|
+
let U = getApp(g);
|
|
376
|
+
U ? (B && (U.instance.changeOptions(B), U = setApp(U, B)), U.preload ? U.instance.preloadRender(U.el) : U.instance.mount(U.el), V(U)) : H("app not found, please setup app first");
|
|
377
|
+
}), stopApp = (g) => {
|
|
378
|
+
let B = getApp(g);
|
|
379
|
+
B && B.instance.unmount();
|
|
380
|
+
}, reloadApp = (g, B = !1) => {
|
|
381
|
+
let V = getApp(g);
|
|
382
|
+
V && V.instance.reload(B);
|
|
383
|
+
}, clearParentUrl = (g) => {
|
|
384
|
+
ConsoleUtil.trace("clearParentUrl", g, CONSTANT.URL_PARAM_PREFIX + g);
|
|
385
|
+
let B = window.location.href.split("#");
|
|
386
|
+
ConsoleUtil.trace("url", B);
|
|
387
|
+
let V = new URL(B[0]);
|
|
388
|
+
V.searchParams.delete(CONSTANT.URL_PARAM_PREFIX + g);
|
|
389
|
+
let H = B.length > 1 ? V.href + "#" + B[1] : V.href;
|
|
390
|
+
return window.history.replaceState({}, "", H), H;
|
|
391
|
+
}, isMicro = () => window[CONSTANT.IS_MICRO] === !0, isParent = () => window[CONSTANT.MICRO_TYPE] === "parent", createAppUrl = (g, B = window.__XT_MICRO_APP?.name) => {
|
|
392
|
+
let V = window.location;
|
|
393
|
+
if (isMicro()) V = window.__XT_MICRO_APP.mainLocation;
|
|
394
|
+
else if (!B) {
|
|
388
395
|
ConsoleUtil.error("在主应用中需要传入应用名称");
|
|
389
396
|
return;
|
|
390
397
|
}
|
|
391
|
-
return `${
|
|
398
|
+
return `${V.origin}${V.pathname}?__XT_MICRO_${B}=${encodeURIComponent(g)}${V.hash}`;
|
|
392
399
|
};
|
|
393
400
|
var XTMicroView_default = /* @__PURE__ */ defineComponent({
|
|
394
401
|
__name: "XTMicroView",
|
|
395
402
|
props: { appId: String },
|
|
396
|
-
setup(
|
|
397
|
-
let
|
|
398
|
-
|
|
399
|
-
el:
|
|
400
|
-
onUnload(
|
|
401
|
-
ConsoleUtil.trace("on unload",
|
|
403
|
+
setup(B) {
|
|
404
|
+
let K = B, q = ref(), J = () => {
|
|
405
|
+
K.appId && startApp(K.appId, {
|
|
406
|
+
el: q.value,
|
|
407
|
+
onUnload(g, B) {
|
|
408
|
+
ConsoleUtil.trace("on unload", B.app.name);
|
|
402
409
|
}
|
|
403
|
-
}).then((
|
|
404
|
-
ConsoleUtil.trace("app mounted",
|
|
410
|
+
}).then((g) => {
|
|
411
|
+
ConsoleUtil.trace("app mounted", K.appId);
|
|
405
412
|
});
|
|
406
413
|
};
|
|
407
|
-
return watch(() =>
|
|
408
|
-
|
|
414
|
+
return watch(() => K.appId, (g, B) => {
|
|
415
|
+
g !== B && B && stopApp(B), J();
|
|
409
416
|
}), onMounted(() => {
|
|
410
|
-
|
|
417
|
+
J();
|
|
411
418
|
}), onUnmounted(() => {
|
|
412
|
-
ConsoleUtil.trace("「micro」", "app unmount",
|
|
413
|
-
}), (
|
|
419
|
+
ConsoleUtil.trace("「micro」", "app unmount", K.appId), clearParentUrl(K.appId);
|
|
420
|
+
}), (B, H) => (openBlock(), createElementBlock("div", mergeProps({
|
|
414
421
|
ref_key: "refPanel",
|
|
415
|
-
ref:
|
|
416
|
-
},
|
|
422
|
+
ref: q
|
|
423
|
+
}, B.$attrs, { class: "xt-micro" }), null, 16));
|
|
417
424
|
}
|
|
418
425
|
});
|
|
419
426
|
window[CONSTANT.MICRO_VERSION] = version, window.micro_api = { debug: ConsoleUtil.setEnable };
|
|
420
|
-
var src_default = { install: (
|
|
421
|
-
|
|
427
|
+
var src_default = { install: (g, B = {}) => {
|
|
428
|
+
g.component("xt-micro-view", XTMicroView_default), mainMicroInit();
|
|
422
429
|
} };
|
|
423
430
|
export { XTMicroView_default as XTMicroView, clearParentUrl, createAppUrl, src_default as default, getApp, isMicro, isParent, microInit, reloadApp, setupApp, startApp, stopApp, utils_exports as utils };
|