@xingtukeji/micro 1.1.24 → 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.
Files changed (2) hide show
  1. package/dist/index.js +260 -253
  2. 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 = (h) => {
3
- let z = {};
4
- for (var B in h) __defProp(z, B, {
5
- get: h[B],
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 z;
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.23";
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(h, z = window.location.href) {
32
- h = h.replace(/[\[\]]/g, "\\$&");
33
- var B = (/* @__PURE__ */ RegExp("[?&]" + h + "(=([^&#]*)|&|#|$)")).exec(z);
34
- return B ? B[2] ? decodeURIComponent(B[2].replace(/\+/g, " ")).trim() : "" : null;
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(h, z, B) {
36
+ function getAbsolutePath(g, B, V) {
37
37
  try {
38
- return h && (B && h.startsWith("#") ? h : new URL(h, z).href);
38
+ return g && (V && g.startsWith("#") ? g : new URL(g, B).href);
39
39
  } catch {
40
- return h;
40
+ return g;
41
41
  }
42
42
  }
43
- function anchorElementGenerator(h) {
44
- let z = window.document.createElement("a");
45
- return z.href = h, z.href = z.href, z;
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(h) {
48
- let z = h.search || "";
49
- return [...new URLSearchParams(z).entries()].reduce((h, z) => (h[z[0]] = z[1], h), {});
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(h, z) {
52
- let B = null;
53
- return function(...V) {
54
- let H = this;
55
- B && clearTimeout(B), B = setTimeout(() => {
56
- h.apply(H, V);
57
- }, z);
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(h, z, B) {
61
- let [V, H] = h.split("#"), U = new URL(V), W = U.searchParams;
62
- return z = CONSTANT.URL_PARAM_PREFIX + z, W.has(z) ? W.set(z, B) : W.append(z, B), H && (U.hash = H), U.search = W.toString(), ConsoleUtil.trace("urlObj", U), U.origin + U.pathname + U.search + U.hash;
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 (h) => new Promise((z) => {
65
- window.addEventListener("message", (B) => {
66
- ConsoleUtil.trace("child ipcSyncAppInfo receive message", B.data), B.data.cmd === "$xt/micro/info" && (B.data.data.mainMicroVersion !== "1.1.23" && ConsoleUtil.warn(`子应用版本与主应用版本不一致,${h}子应用版本:${version},主应用版本:${B.data.data.mainMicroVersion}`), z(B.data.data));
67
- }), ConsoleUtil.trace("child ipcSyncAppInfo send message", h), window.parent.postMessage({
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: h
69
+ data: g
70
70
  }, "*");
71
71
  });
72
- var ConsoleUtil = class h {
72
+ var ConsoleUtil = class g {
73
73
  static STORAGE_KEY = "@xt/micro/console/enable";
74
74
  static get enable() {
75
- return localStorage.getItem(h.STORAGE_KEY) === "true";
75
+ return localStorage.getItem(g.STORAGE_KEY) === "true";
76
76
  }
77
- static setEnable(z) {
78
- localStorage.setItem(h.STORAGE_KEY, z.toString());
77
+ static setEnable(B) {
78
+ localStorage.setItem(g.STORAGE_KEY, B.toString());
79
79
  }
80
80
  static getEnable() {
81
- return h.enable;
81
+ return g.enable;
82
82
  }
83
83
  static getCallerInfo() {
84
84
  try {
85
- let h = /* @__PURE__ */ ((/* @__PURE__ */ Error()).stack || "").split("\n").slice(4);
86
- if (h.length === 0) return "unknown location";
87
- let z = h[0].trim(), B = z.match(/(?:http[s]?:\/\/.*?\/|)([^:]+):(\d+):(\d+)/);
88
- if (B) {
89
- let [, h, z] = B;
90
- return `[${h.split("/").slice(-2).join("/")}:${z}]`;
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 `[${z}]`;
92
+ return `[${B}]`;
93
93
  } catch {
94
94
  return "[unknown location]";
95
95
  }
96
96
  }
97
- static _log(z, ...B) {
98
- if (h.enable && typeof console[z] == "function") {
99
- let h = this.getCallerInfo();
100
- console[z](h, ...B);
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(...h) {
104
- this._log("log", ...h);
103
+ static log(...g) {
104
+ this._log("log", ...g);
105
105
  }
106
- static info(...h) {
107
- this._log("info", ...h);
106
+ static info(...g) {
107
+ this._log("info", ...g);
108
108
  }
109
- static warn(...h) {
110
- this._log("warn", ...h);
109
+ static warn(...g) {
110
+ this._log("warn", ...g);
111
111
  }
112
- static error(...h) {
113
- this._log("error", ...h);
112
+ static error(...g) {
113
+ this._log("error", ...g);
114
114
  }
115
- static debug(...h) {
116
- this._log("debug", ...h);
115
+ static debug(...g) {
116
+ this._log("debug", ...g);
117
117
  }
118
- static trace(...h) {
119
- this._log("trace", ...h);
118
+ static trace(...g) {
119
+ this._log("trace", ...g);
120
120
  }
121
- static table(h, z) {
122
- this.enable && console.table(h, z);
121
+ static table(g, B) {
122
+ this.enable && console.table(g, B);
123
123
  }
124
- static time(h) {
125
- this.enable && console.time(h);
124
+ static time(g) {
125
+ this.enable && console.time(g);
126
126
  }
127
- static timeEnd(h) {
128
- this.enable && console.timeEnd(h);
127
+ static timeEnd(g) {
128
+ this.enable && console.timeEnd(g);
129
129
  }
130
- }, _isMicro = ref(!1);
131
- Object.defineProperty(window, CONSTANT.IS_MICRO, {
132
- get: function() {
133
- return _isMicro.value;
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
- set: function(h) {
136
- _isMicro.value = h;
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 = (h) => `${CONSTANT.DOM_ID_PREFIX}${h}`, generateURL = (h, z = !1) => {
140
- function B(h) {
141
- return typeof h == "object" && !!h && !Array.isArray(h);
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 V = typeof h.params == "function" ? h.params() : B(h.params) ? h.params : {}, H = typeof h.token == "function" ? h.token() : B(h.token) ? h.token : "", U = new URL(h.url.indexOf("http") > -1 ? h.url : window.location.origin + h.url), W = getQueryParameter(CONSTANT.URL_PARAM_PREFIX + h.name);
144
- if (!z && W) {
145
- let z = getAbsolutePath(W, h.url, !0);
146
- if (z) {
147
- let B = new URL(z, h.url);
148
- B.searchParams.forEach((h, z) => {
149
- U.searchParams.set(z, h);
150
- }), U.hash = B.hash;
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(h.name);
159
+ clearParentUrl(g.name);
153
160
  }
154
- if (U.searchParams.set(CONSTANT.IS_MICRO, "true"), U.searchParams.set(CONSTANT.MICRO_NAME, h.name), H && U.searchParams.set(CONSTANT.MICRO_TOKEN, H), V) for (let h of Object.keys(V)) U.searchParams.set(h, V[h]);
155
- return clearParentUrl(h.name), U.href;
156
- }, generateLoading = (h) => {
157
- ConsoleUtil.log("generateLoading", h);
158
- let z = document.createElement("div");
159
- return z.id = generateId(h) + "-loading", z.innerHTML = "应用加载中...", z.style.textAlign = "center", z.style.lineHeight = "100px", z.style.fontSize = "20px", z.style.color = "#999", z.style.position = "absolute", z.style.top = "0", z.style.left = "0", z.style.right = "0", z.style.bottom = "0", z.style.zIndex = "999", z.style.background = "transparent", z.style["mix-blend-mode"] = "difference", z;
160
- }, removeLoading = (h) => {
161
- h.style.opacity = "0", setTimeout(() => {
162
- document.querySelectorAll("#" + h.id).forEach((h) => {
163
- h.remove();
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 = (h) => {
167
- let z = document.createElement("iframe");
168
- z.id = generateId(h.app.name), z.src = generateURL(h.app), z.style.width = "100%", z.style.height = "100%", z.style.border = "none", z.style.margin = "0", z.style.padding = "0", z.style.backgroundColor = "transparent", z.style["color-scheme"] = "unset", z.style.visibility = "hidden", z.addEventListener("load", (B) => (z.style.visibility = "visible", h.onLoad.call(h, B))), z.addEventListener("error", (z) => h.onError.call(h, z)), z.addEventListener("unload", (z) => h.onUnload.call(h, z));
169
- let B = new MutationObserver((V) => {
170
- ConsoleUtil.trace("监听 body 中子节点的变化2", h.panel), V.forEach((V) => {
171
- Array.from(V.removedNodes).includes(z) && (h.onUnload(new Event("unload")), B.disconnect());
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 B.observe(h.panel.parentNode, {
181
+ return V.observe(g.panel.parentNode, {
175
182
  childList: !0,
176
183
  subtree: !1
177
- }), z;
184
+ }), B;
178
185
  }, MicroApp = class {
179
186
  app;
180
187
  dom;
181
188
  panel;
182
189
  loading;
183
190
  loadingTimer;
184
- constructor(h) {
185
- return this.app = h, this.dom = void 0, this.panel = void 0, h.preload && this.preload(), this;
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(h, z = !1) {
188
- let B;
189
- if (h ? (B = typeof h == "string" ? document.querySelector(h) : h, B.innerHTML = "") : B = document.body, B) {
190
- this.panel = B;
191
- let h = !0, V = document.getElementById(generateId(this.app.name));
192
- if (V || (h = !1, V = generateIframe(this), this.dom = V), z ? V.style.display = "none" : V.style.removeProperty("display"), !z) {
193
- let h = generateLoading(this.app.name);
194
- this.loading = h, B.appendChild(h);
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, h ? "已加载迁移 dom" : "首次加载"), B.appendChild(V);
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 h = document.getElementById(generateId(this.app.name));
202
- if (h) this.app.preload ? (h.style.display = "none", document.body.appendChild(h)) : this.panel?.removeChild(h);
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(h) {
206
- this.mount(h);
212
+ preloadRender(g) {
213
+ this.mount(g);
207
214
  }
208
215
  preload() {
209
216
  this.mount(void 0, !0);
210
217
  }
211
- reload(h = !1) {
212
- let z = this.dom.src;
213
- h && (z = generateURL(this.app, !0), ConsoleUtil.trace("go home", z)), ConsoleUtil.trace("reload app", this.app, z);
214
- let B = new URL(z);
215
- B.searchParams.set("__t", Date.now().toString()), this.dom.style.visibility = "hidden", this.dom.src = B.toString();
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(h) {
218
- ConsoleUtil.trace("changeOptions", this.app, h), this.app = {
224
+ changeOptions(g) {
225
+ ConsoleUtil.trace("changeOptions", this.app, g), this.app = {
219
226
  ...this.app,
220
- ...h
227
+ ...g
221
228
  };
222
229
  }
223
- onLoad(h) {
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(h, this);
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(h) {
227
- ConsoleUtil.trace("app onUnload", this.app.name), clearParentUrl(this.app.name), this.app.onUnload && this.app.onUnload(h, this);
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(h) {
230
- ConsoleUtil.error("app onError", this.app.name), this.app.onError && this.app.onError(h, this);
236
+ onError(g) {
237
+ ConsoleUtil.error("app onError", this.app.name), this.app.onError && this.app.onError(g, this);
231
238
  }
232
- }, _postMicro = (h) => {
233
- h.dom?.contentWindow?.postMessage({
234
- cmd: `$xt/micro/${h.app.name}`,
239
+ }, _postMicro = (g) => {
240
+ g.dom?.contentWindow?.postMessage({
241
+ cmd: `$xt/micro/${g.app.name}`,
235
242
  data: {
236
- name: h.app.name,
237
- url: h.app.url,
238
- preload: h.app.preload,
239
- sync: h.app.sync || !0
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", (h) => {
245
- if (h.data.cmd === "$xt/micro/sync") {
246
- ConsoleUtil.trace("父页面接受子应用同步路由信息", h.data);
247
- let z = [
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
- ], B = new URL(window.decodeURIComponent(h.data.data.url), h.origin);
252
- for (let h of z) B.searchParams.delete(h);
253
- ConsoleUtil.trace("tempUrl", B);
254
- let V = clearParentUrl(h.data.data.name), H = new URL(V);
255
- H.searchParams.forEach((h, z) => {
256
- z.startsWith(CONSTANT.URL_PARAM_PREFIX) && H.searchParams.delete(z);
257
- }), ConsoleUtil.trace("mainURL.href", H.href);
258
- let U = addOrReplaceUrlParam(H.href, h.data.data.name, B.href);
259
- ConsoleUtil.trace("curUrl", U), window.history.replaceState(null, "", U);
260
- } else if (h.data.cmd === "$xt/micro/info") {
261
- ConsoleUtil.trace("parent 「$xt/micro/info」 receive message", h.data);
262
- let z = getApp(h.data.data);
263
- h.source.postMessage({
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: z.name,
279
- url: z.url,
280
- params: z.params,
281
- sync: z.sync,
282
- preload: z.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: h.origin });
291
+ }, { targetOrigin: g.origin });
285
292
  }
286
293
  });
287
- }, microInit = async (h = {}) => {
294
+ }, microInit = async (g = {}) => {
288
295
  window[CONSTANT.MICRO_TYPE] = "child";
289
- let z = getQueryParameter(CONSTANT.IS_MICRO), B = getQueryParameter(CONSTANT.MICRO_NAME);
290
- if (ConsoleUtil.trace("child microInit", z, B), z && (window[CONSTANT.IS_MICRO] = z === "true", B && await syncAppInfo(B)), window[CONSTANT.IS_MICRO]) {
291
- function z(h) {
292
- var z = document.styleSheets[0];
293
- if (!z) {
294
- var B = document.createElement("style");
295
- document.head.appendChild(B), z = B.sheet;
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
- z.insertRule(h, z.cssRules.length);
304
+ B.insertRule(g, B.cssRules.length);
298
305
  }
299
- window[CONSTANT.IS_MICRO] && h.hideHeaderCssSelector && (Array.isArray(h.hideHeaderCssSelector) ? h.hideHeaderCssSelector.forEach((h) => {
300
- z(`${h} { display: none !important; }`);
301
- }) : z(`${h.hideHeaderCssSelector} { display: none !important; }`)), await patchIframeHistory(B, window), window.addEventListener("message", async (h) => {
302
- h.data.cmd === `$xt/micro/${B}` && syncUrlToWindow(B);
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 V = getQueryParameter(CONSTANT.MICRO_TOKEN);
305
- h.authHandler && V && await h.authHandler(V);
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(h, z) {
309
- let B = z.history, V = B.pushState, H = B.replaceState;
310
- B.pushState = function(z, H, U) {
311
- let W = {
312
- ...B.state || {},
313
- ...z,
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
- V.call(B, W, H, U), syncUrlToWindow(h);
317
- }, B.replaceState = function(h, z, V) {
318
- let U = {
319
- ...B.state || {},
320
- ...h,
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
- H.call(B, U, z, V);
330
+ U.call(V, W, B, H);
324
331
  };
325
332
  }
326
- var syncAppInfo = async (h) => await ipcSyncAppInfo(h).then((h) => {
333
+ var syncAppInfo = async (g) => await ipcSyncAppInfo(g).then((g) => {
327
334
  window[CONSTANT.MICRO_APP] = {
328
335
  microVersion: version,
329
- ...h
336
+ ...g
330
337
  };
331
338
  });
332
- async function syncUrlToWindow(h) {
333
- ConsoleUtil.trace("syncUrlToWindow", h, window[CONSTANT.MICRO_APP]), window[CONSTANT.MICRO_APP] || syncAppInfo(h);
334
- let { name: z, sync: B, mainLocation: V } = window[CONSTANT.MICRO_APP], H = window.location.pathname + window.location.search + window.location.hash;
335
- ConsoleUtil.trace("curUrl", H), ConsoleUtil.trace("child", "「$xt/micro/sync」", "send", z, H), window.parent.postMessage({
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: z,
339
- url: window.encodeURIComponent(H)
345
+ name: B,
346
+ url: window.encodeURIComponent(U)
340
347
  }
341
- }, V.origin);
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 = (h) => {
350
- if (!h.url || h.url === "" || h.url === "undefined") {
351
- ConsoleUtil.error("app url is required", h);
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 z = Object.assign({}, defaultAppOption, h), B = _apps[h.name];
355
- return B ? _apps[h.name] = {
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
- ...z
358
- } : _apps[h.name] = {
359
- ...z,
360
- instance: new MicroApp(h)
361
- }, _apps[h.name];
362
- }, getApp = (h) => _apps[h];
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 = (h, z) => new Promise((B, V) => {
368
- let H = getApp(h);
369
- H ? (z && (H.instance.changeOptions(z), H = setApp(H, z)), H.preload ? H.instance.preloadRender(H.el) : H.instance.mount(H.el), B(H)) : V("app not found, please setup app first");
370
- }), stopApp = (h) => {
371
- let z = getApp(h);
372
- z && z.instance.unmount();
373
- }, reloadApp = (h, z = !1) => {
374
- let B = getApp(h);
375
- B && B.instance.reload(z);
376
- }, clearParentUrl = (h) => {
377
- ConsoleUtil.trace("clearParentUrl", h, CONSTANT.URL_PARAM_PREFIX + h);
378
- let z = window.location.href.split("#");
379
- ConsoleUtil.trace("url", z);
380
- let B = new URL(z[0]);
381
- B.searchParams.delete(CONSTANT.URL_PARAM_PREFIX + h);
382
- let V = z.length > 1 ? B.href + "#" + z[1] : B.href;
383
- return window.history.replaceState({}, "", V), V;
384
- }, isMicro = () => window[CONSTANT.IS_MICRO] === !0, isParent = () => window[CONSTANT.MICRO_TYPE] === "parent", createAppUrl = (h, z = window.__XT_MICRO_APP?.name) => {
385
- let B = window.location;
386
- if (isMicro()) B = window.__XT_MICRO_APP.mainLocation;
387
- else if (!z) {
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 `${B.origin}${B.pathname}?__XT_MICRO_${z}=${encodeURIComponent(h)}${B.hash}`;
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(z) {
397
- let G = z, K = ref(), q = () => {
398
- G.appId && startApp(G.appId, {
399
- el: K.value,
400
- onUnload(h, z) {
401
- ConsoleUtil.trace("on unload", z.app.name);
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((h) => {
404
- ConsoleUtil.trace("app mounted", G.appId);
410
+ }).then((g) => {
411
+ ConsoleUtil.trace("app mounted", K.appId);
405
412
  });
406
413
  };
407
- return watch(() => G.appId, (h, z) => {
408
- h !== z && z && stopApp(z), q();
414
+ return watch(() => K.appId, (g, B) => {
415
+ g !== B && B && stopApp(B), J();
409
416
  }), onMounted(() => {
410
- q();
417
+ J();
411
418
  }), onUnmounted(() => {
412
- ConsoleUtil.trace("「micro」", "app unmount", G.appId), clearParentUrl(G.appId);
413
- }), (z, V) => (openBlock(), createElementBlock("div", mergeProps({
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: K
416
- }, z.$attrs, { class: "xt-micro" }), null, 16));
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: (h, z = {}) => {
421
- h.component("xt-micro-view", XTMicroView_default), mainMicroInit();
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xingtukeji/micro",
3
- "version": "1.1.24",
3
+ "version": "1.1.25",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "main": "dist/index.js",