@blueking/bk-weweb 0.0.30 → 0.0.31
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/base-app/collect-source.esm.js +12 -2246
- package/dist/base-app/collect-source.esm.js.map +1 -1
- package/dist/index.esm.js +12 -2319
- package/dist/index.esm.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,2264 +1,30 @@
|
|
|
1
|
-
// src/context/cache.ts
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
return currentRunningApp;
|
|
5
|
-
}
|
|
6
|
-
function setCurrentRunningApp(appInstance) {
|
|
7
|
-
currentRunningApp = appInstance;
|
|
8
|
-
}
|
|
9
|
-
var windowNativeFuncMap = /* @__PURE__ */ new Map();
|
|
10
|
-
var globalContextCode = `const { ${[
|
|
11
|
-
"Array",
|
|
12
|
-
"ArrayBuffer",
|
|
13
|
-
"Boolean",
|
|
14
|
-
"constructor",
|
|
15
|
-
"DataView",
|
|
16
|
-
"Date",
|
|
17
|
-
"decodeURI",
|
|
18
|
-
"decodeURIComponent",
|
|
19
|
-
"encodeURI",
|
|
20
|
-
"encodeURIComponent",
|
|
21
|
-
"Error",
|
|
22
|
-
"escape",
|
|
23
|
-
"eval",
|
|
24
|
-
"EvalError",
|
|
25
|
-
"Float32Array",
|
|
26
|
-
"Float64Array",
|
|
27
|
-
"Function",
|
|
28
|
-
"hasOwnProperty",
|
|
29
|
-
"Infinity",
|
|
30
|
-
"Int16Array",
|
|
31
|
-
"Int32Array",
|
|
32
|
-
"Int8Array",
|
|
33
|
-
"isFinite",
|
|
34
|
-
"isNaN",
|
|
35
|
-
"isPrototypeOf",
|
|
36
|
-
"JSON",
|
|
37
|
-
"Map",
|
|
38
|
-
"Math",
|
|
39
|
-
"NaN",
|
|
40
|
-
"Number",
|
|
41
|
-
"Object",
|
|
42
|
-
"parseFloat",
|
|
43
|
-
"parseInt",
|
|
44
|
-
"Promise",
|
|
45
|
-
"propertyIsEnumerable",
|
|
46
|
-
"Proxy",
|
|
47
|
-
"RangeError",
|
|
48
|
-
"ReferenceError",
|
|
49
|
-
"Reflect",
|
|
50
|
-
"RegExp",
|
|
51
|
-
"Set",
|
|
52
|
-
"String",
|
|
53
|
-
"Symbol",
|
|
54
|
-
"SyntaxError",
|
|
55
|
-
"toLocaleString",
|
|
56
|
-
"toString",
|
|
57
|
-
"TypeError",
|
|
58
|
-
"Uint16Array",
|
|
59
|
-
"Uint32Array",
|
|
60
|
-
"Uint8Array",
|
|
61
|
-
"Uint8ClampedArray",
|
|
62
|
-
"undefined",
|
|
63
|
-
"unescape",
|
|
64
|
-
"URIError",
|
|
65
|
-
"valueOf",
|
|
66
|
-
"WeakMap",
|
|
67
|
-
"WeakSet"
|
|
68
|
-
].join(",")} }= this;`;
|
|
69
|
-
var getGlobalContextCode = () => {
|
|
70
|
-
return globalContextCode;
|
|
71
|
-
};
|
|
72
|
-
var collectNativeWindowFunc = () => {
|
|
73
|
-
const keyList = Object.getOwnPropertyNames(window);
|
|
74
|
-
for (const key of keyList) {
|
|
75
|
-
if (!windowNativeFuncMap.has(key) && key.match(/^[A-Z]/) && typeof window[key] === "function" && window[key].toString().includes("[native code]")) {
|
|
76
|
-
windowNativeFuncMap.set(key, true);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
collectNativeWindowFunc();
|
|
81
|
-
|
|
82
|
-
// src/typings/source.ts
|
|
83
|
-
var CSS_ATTRIBUTE_KEY = "id";
|
|
84
|
-
|
|
85
|
-
// src/typings/sandbox.ts
|
|
86
|
-
var WINDOW_WHITE_LIST = [
|
|
87
|
-
"System",
|
|
88
|
-
// SystemJS
|
|
89
|
-
"__cjsWrapper",
|
|
90
|
-
// SystemJS CommonJS wrapper
|
|
91
|
-
false ? "__REACT_DEVTOOLS_GLOBAL_HOOK__" : ""
|
|
92
|
-
];
|
|
93
|
-
var BK_WEWEB_INJECT_KEY_LIST = [
|
|
94
|
-
"__POWERED_BY_BK_WEWEB__",
|
|
95
|
-
"__BK_WEWEB_APP_KEY__",
|
|
96
|
-
"__BK_WEWEB_DATA__"
|
|
97
|
-
];
|
|
98
|
-
var WINDOW_ALIAS_LIST = ["window", "self", "globalThis"];
|
|
99
|
-
var BK_WEWEB_LOCATION_KEY_LIST = ["location", "history"];
|
|
100
|
-
var COMMON_MICRO_APP_WINDOE_KEY_MAP = {
|
|
101
|
-
__bk_pop_manager: true,
|
|
102
|
-
__bk_zIndex_manager: true,
|
|
103
|
-
i18n: true
|
|
104
|
-
};
|
|
105
|
-
var DEV_MICRO_APP_WINDOE_KEY_MAP = false ? {
|
|
106
|
-
__DEV__: true,
|
|
107
|
-
__VUE_DEVTOOLS_GLOBAL_HOOK__: true,
|
|
108
|
-
__VUE_DEVTOOLS_HOOK_REPLAY__: true,
|
|
109
|
-
__VUE_DEVTOOLS_PLUGINS__: true,
|
|
110
|
-
__VUE_I18N_FULL_INSTALL__: true,
|
|
111
|
-
__VUE_I18N_LEGACY_API__: true,
|
|
112
|
-
__VUE_OPTIONS_API__: true,
|
|
113
|
-
"__core-js_shared__": true,
|
|
114
|
-
webpackChunkapm: true,
|
|
115
|
-
webpackChunkpc: true,
|
|
116
|
-
webpackChunktrace: true,
|
|
117
|
-
webpackJsonp: true,
|
|
118
|
-
...COMMON_MICRO_APP_WINDOE_KEY_MAP
|
|
119
|
-
} : COMMON_MICRO_APP_WINDOE_KEY_MAP;
|
|
120
|
-
|
|
121
|
-
// src/utils/common.ts
|
|
122
|
-
var nextTask = (cb) => Promise.resolve().then(cb);
|
|
123
|
-
function addUrlProtocol(url) {
|
|
124
|
-
if (url.startsWith("//")) return `${location.protocol}${url}`;
|
|
125
|
-
if (!url.startsWith("http")) return `${location.protocol}//${url}`;
|
|
126
|
-
return url;
|
|
127
|
-
}
|
|
128
|
-
function fillUpPath(path, baseURI) {
|
|
129
|
-
if (!path || /^((((ht|f)tps?)|file):)?\/\//.test(path) || /^(data|blob):/.test(path)) return path;
|
|
130
|
-
const { origin, pathname } = new URL(addUrlProtocol(baseURI));
|
|
131
|
-
return new URL(path, `${origin}${pathname}`.replace(/\.(\w+)$/, "/")).toString();
|
|
132
|
-
}
|
|
133
|
-
function randomUrl() {
|
|
134
|
-
return `inline-${random(16)}`;
|
|
135
|
-
}
|
|
136
|
-
var requestIdleCallback = window.requestIdleCallback || ((cb) => {
|
|
137
|
-
const start = Date.now();
|
|
138
|
-
return setTimeout(() => {
|
|
139
|
-
cb({
|
|
140
|
-
didTimeout: false,
|
|
141
|
-
timeRemaining() {
|
|
142
|
-
return Math.max(0, 50 - (Date.now() - start));
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
}, 1);
|
|
146
|
-
});
|
|
147
|
-
var cancelIdleCallback = window.cancelIdleCallback || ((id) => {
|
|
148
|
-
clearTimeout(id);
|
|
149
|
-
});
|
|
150
|
-
var random = (n, str = "abcdefghijklmnopqrstuvwxyz0123456789") => {
|
|
151
|
-
let result = "";
|
|
152
|
-
for (let i = 0; i < n; i++) {
|
|
153
|
-
result += str[Number.parseInt((Math.random() * str.length).toString(), 10)];
|
|
154
|
-
}
|
|
155
|
-
return result;
|
|
156
|
-
};
|
|
157
|
-
var isJsonpUrl = (url) => {
|
|
158
|
-
if (!url) return false;
|
|
159
|
-
if (url.match(/\.js$/)) return false;
|
|
160
|
-
const { pathname } = new URL(addUrlProtocol(url));
|
|
161
|
-
return !pathname.match(/\.js$/);
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
// src/context/document.ts
|
|
165
|
-
var SPECIAL_ELEMENT_TAG = ["body", "html", "head"];
|
|
166
|
-
var createProxyDocument = (rawDocument, app) => {
|
|
167
|
-
const fakeDocument = {};
|
|
168
|
-
function shadowRootInsertAdjacentHTML(where, domString) {
|
|
169
|
-
const temporaryContainer = document.createElement("div");
|
|
170
|
-
temporaryContainer.innerHTML = domString;
|
|
171
|
-
const elements = Array.from(temporaryContainer.childNodes);
|
|
172
|
-
const shadow = app.container;
|
|
173
|
-
switch (where) {
|
|
174
|
-
case "beforebegin":
|
|
175
|
-
elements.forEach((el) => shadow.host.parentNode?.insertBefore(el, shadow.host));
|
|
176
|
-
break;
|
|
177
|
-
case "afterbegin":
|
|
178
|
-
elements.reverse().forEach((el) => shadow.insertBefore(el, shadow.firstChild));
|
|
179
|
-
break;
|
|
180
|
-
case "beforeend":
|
|
181
|
-
elements.forEach((el) => shadow.appendChild(el));
|
|
182
|
-
break;
|
|
183
|
-
case "afterend":
|
|
184
|
-
elements.forEach((el) => shadow.host.parentNode?.insertBefore(el, shadow.host.nextSibling));
|
|
185
|
-
break;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
const proxyBody = new Proxy(
|
|
189
|
-
{},
|
|
190
|
-
{
|
|
191
|
-
get(_, key) {
|
|
192
|
-
if (app.container instanceof ShadowRoot) {
|
|
193
|
-
if (key === "insertAdjacentHTML") {
|
|
194
|
-
return shadowRootInsertAdjacentHTML.bind(app.container);
|
|
195
|
-
}
|
|
196
|
-
const value2 = app.container[key];
|
|
197
|
-
if (typeof value2 === "function") {
|
|
198
|
-
return value2.bind(app.container);
|
|
199
|
-
}
|
|
200
|
-
if (value2 !== void 0) {
|
|
201
|
-
return value2;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
const value = Reflect.get(rawDocument.body, key);
|
|
205
|
-
return typeof value === "function" ? value.bind(rawDocument.body) : value;
|
|
206
|
-
},
|
|
207
|
-
set(_, key, value) {
|
|
208
|
-
if (app.container instanceof ShadowRoot) {
|
|
209
|
-
app.container[key] = value;
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
212
|
-
Reflect.set(rawDocument.body, key, value);
|
|
213
|
-
return true;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
);
|
|
217
|
-
function createElement(tagName, options) {
|
|
218
|
-
const element = rawDocument.createElement(tagName, options);
|
|
219
|
-
element.__BK_WEWEB_APP_KEY__ = app.appCacheKey;
|
|
220
|
-
return element;
|
|
221
|
-
}
|
|
222
|
-
function querySelectorNew(selectors) {
|
|
223
|
-
if (selectors === proxyBody) {
|
|
224
|
-
return app.container instanceof ShadowRoot ? app.container : rawDocument.body;
|
|
225
|
-
}
|
|
226
|
-
if (SPECIAL_ELEMENT_TAG.includes(selectors)) {
|
|
227
|
-
if (app?.container instanceof ShadowRoot) {
|
|
228
|
-
return app?.container;
|
|
229
|
-
}
|
|
230
|
-
return rawDocument.querySelector.call(this, selectors);
|
|
231
|
-
}
|
|
232
|
-
try {
|
|
233
|
-
return app?.container?.querySelector(selectors) ?? null;
|
|
234
|
-
} catch {
|
|
235
|
-
return null;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
function querySelectorAllNew(selectors) {
|
|
239
|
-
if (SPECIAL_ELEMENT_TAG.includes(selectors)) {
|
|
240
|
-
if (app?.container instanceof ShadowRoot) {
|
|
241
|
-
return app?.container;
|
|
242
|
-
}
|
|
243
|
-
return rawDocument.querySelector(selectors);
|
|
244
|
-
}
|
|
245
|
-
return app?.container?.querySelectorAll(selectors) ?? [];
|
|
246
|
-
}
|
|
247
|
-
function getElementByIdNew(key) {
|
|
248
|
-
return querySelectorNew.call(rawDocument, `#${key}`);
|
|
249
|
-
}
|
|
250
|
-
function getElementsByClassName(key) {
|
|
251
|
-
return querySelectorAllNew(`.${key}`);
|
|
252
|
-
}
|
|
253
|
-
function getElementsByTagName(key) {
|
|
254
|
-
if (SPECIAL_ELEMENT_TAG.includes(key) || !app?.showSourceCode && key.toLocaleLowerCase() === "script") {
|
|
255
|
-
return rawDocument.getElementsByTagName(key);
|
|
256
|
-
}
|
|
257
|
-
return querySelectorAllNew(key);
|
|
258
|
-
}
|
|
259
|
-
function getElementsByNameNew(key) {
|
|
260
|
-
return querySelectorAllNew(`[name=${key}]`);
|
|
261
|
-
}
|
|
262
|
-
return new Proxy(fakeDocument, {
|
|
263
|
-
get(_, key) {
|
|
264
|
-
if (key === "createElement") {
|
|
265
|
-
return createElement.bind(rawDocument);
|
|
266
|
-
}
|
|
267
|
-
if (key === "querySelector") {
|
|
268
|
-
return querySelectorNew.bind(rawDocument);
|
|
269
|
-
}
|
|
270
|
-
if (key === "querySelectorAll") {
|
|
271
|
-
return querySelectorAllNew.bind(rawDocument);
|
|
272
|
-
}
|
|
273
|
-
if (key === "getElementById") {
|
|
274
|
-
return getElementByIdNew.bind(rawDocument);
|
|
275
|
-
}
|
|
276
|
-
if (key === "getElementsByClassName") {
|
|
277
|
-
return getElementsByClassName.bind(rawDocument);
|
|
278
|
-
}
|
|
279
|
-
if (key === "getElementsByTagName") {
|
|
280
|
-
return getElementsByTagName.bind(rawDocument);
|
|
281
|
-
}
|
|
282
|
-
if (key === "getElementsByName") {
|
|
283
|
-
return getElementsByNameNew.bind(rawDocument);
|
|
284
|
-
}
|
|
285
|
-
if (key === "body") {
|
|
286
|
-
return proxyBody;
|
|
287
|
-
}
|
|
288
|
-
const result = Reflect.get(rawDocument, key);
|
|
289
|
-
if (typeof result === "function") {
|
|
290
|
-
return result.bind(rawDocument);
|
|
291
|
-
}
|
|
292
|
-
return result;
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
// src/context/event.ts
|
|
298
|
-
function rewriteDocumentAndBodyEvent() {
|
|
299
|
-
const { addEventListener, removeEventListener } = window.document;
|
|
300
|
-
const { addEventListener: bodyAddEventListener, removeEventListener: bodyRemoveEventListener } = window.document.body;
|
|
301
|
-
const documentListenerMap = /* @__PURE__ */ new Map();
|
|
302
|
-
document.addEventListener = function(type, listener, options) {
|
|
303
|
-
const app = getCurrentRunningApp();
|
|
304
|
-
if (app?.keepAlive) {
|
|
305
|
-
const listeners = documentListenerMap.get(type) || [];
|
|
306
|
-
documentListenerMap.set(type, [...listeners, listener]);
|
|
307
|
-
}
|
|
308
|
-
addEventListener.call(app?.container instanceof ShadowRoot ? app.container : this, type, listener, options);
|
|
309
|
-
};
|
|
310
|
-
document.body.addEventListener = document.addEventListener;
|
|
311
|
-
document.removeEventListener = function(type, listener, options) {
|
|
312
|
-
const app = getCurrentRunningApp();
|
|
313
|
-
if (app?.keepAlive) {
|
|
314
|
-
const listeners = documentListenerMap.get(type) || [];
|
|
315
|
-
if (listeners.length && listeners.some((l) => l === listener)) {
|
|
316
|
-
listeners.splice(listeners.indexOf(listener), 1);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
removeEventListener.call(app?.container instanceof ShadowRoot ? app.container : this, type, listener, options);
|
|
320
|
-
};
|
|
321
|
-
document.body.removeEventListener = document.removeEventListener;
|
|
322
|
-
function resetDocumentAndBodyEvent() {
|
|
323
|
-
const app = getCurrentRunningApp();
|
|
324
|
-
if (app?.keepAlive && documentListenerMap.values()) {
|
|
325
|
-
Array.from(documentListenerMap.entries()).forEach(([type, listeners]) => {
|
|
326
|
-
listeners?.forEach((listener) => {
|
|
327
|
-
document.removeEventListener.call(document, type, listener);
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
document.addEventListener = addEventListener;
|
|
332
|
-
document.body.addEventListener = bodyAddEventListener;
|
|
333
|
-
document.removeEventListener = removeEventListener;
|
|
334
|
-
document.body.removeEventListener = bodyRemoveEventListener;
|
|
335
|
-
documentListenerMap.clear();
|
|
336
|
-
}
|
|
337
|
-
return {
|
|
338
|
-
resetDocumentAndBodyEvent
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// src/context/function.ts
|
|
343
|
-
var CLASS_REGEX = /^class\b/;
|
|
344
|
-
var COMMON_CONTRUCT_FU_REGEX = /^function\b\s[A-Z].*/;
|
|
345
|
-
var ConstructFunCacheMap = /* @__PURE__ */ new WeakMap();
|
|
346
|
-
function isConstructFun(fn) {
|
|
347
|
-
if (fn.prototype?.constructor === fn && Object.getOwnPropertyNames(fn.prototype).length > 1) {
|
|
348
|
-
return true;
|
|
349
|
-
}
|
|
350
|
-
if (ConstructFunCacheMap.has(fn)) {
|
|
351
|
-
return ConstructFunCacheMap.get(fn);
|
|
352
|
-
}
|
|
353
|
-
const constructable = COMMON_CONTRUCT_FU_REGEX.test(fn.toString()) || CLASS_REGEX.test(fn.toString());
|
|
354
|
-
ConstructFunCacheMap.set(fn, constructable);
|
|
355
|
-
return constructable;
|
|
356
|
-
}
|
|
357
|
-
var functionBoundedValueMap = /* @__PURE__ */ new WeakMap();
|
|
358
|
-
function bindFunctionToRawWindow(rawWindow, value) {
|
|
359
|
-
if (functionBoundedValueMap.has(value)) {
|
|
360
|
-
return functionBoundedValueMap.get(value);
|
|
361
|
-
}
|
|
362
|
-
if (typeof value === "function" && !isConstructFun(value)) {
|
|
363
|
-
const boundValue = Function.prototype.bind.call(value, rawWindow);
|
|
364
|
-
for (const key in value) {
|
|
365
|
-
boundValue[key] = value[key];
|
|
366
|
-
}
|
|
367
|
-
if (value.hasOwnProperty("prototype") && !boundValue.hasOwnProperty("prototype")) {
|
|
368
|
-
Object.defineProperty(boundValue, "prototype", { enumerable: false, value: value.prototype, writable: true });
|
|
369
|
-
}
|
|
370
|
-
if (typeof value.toString === "function") {
|
|
371
|
-
const valueHasInstanceToString = value.hasOwnProperty("toString") && !boundValue.hasOwnProperty("toString");
|
|
372
|
-
const boundValueHasPrototypeToString = boundValue.toString === Function.prototype.toString;
|
|
373
|
-
if (valueHasInstanceToString || boundValueHasPrototypeToString) {
|
|
374
|
-
const originToStringDescriptor = Object.getOwnPropertyDescriptor(
|
|
375
|
-
valueHasInstanceToString ? value : Function.prototype,
|
|
376
|
-
"toString"
|
|
377
|
-
);
|
|
378
|
-
Object.defineProperty(boundValue, "toString", {
|
|
379
|
-
...originToStringDescriptor,
|
|
380
|
-
...originToStringDescriptor?.get ? null : { value: () => value.toString() }
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
functionBoundedValueMap.set(value, boundValue);
|
|
385
|
-
return boundValue;
|
|
386
|
-
}
|
|
387
|
-
return value;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
// src/context/window.ts
|
|
391
|
-
function rewriteWindowFunction(fakeWindow) {
|
|
392
|
-
const windowEventLisenerMap = /* @__PURE__ */ new Map();
|
|
393
|
-
const intervalTimerList = [];
|
|
394
|
-
const rawWindow = window;
|
|
395
|
-
const { addEventListener, clearInterval: clearInterval2, removeEventListener, setInterval: setInterval2 } = window;
|
|
396
|
-
fakeWindow.addEventListener = (type, listener, options) => {
|
|
397
|
-
windowEventLisenerMap.set(type, [...windowEventLisenerMap.get(type) || [], listener]);
|
|
398
|
-
addEventListener.call(rawWindow, type, listener, options);
|
|
399
|
-
};
|
|
400
|
-
fakeWindow.removeEventListener = (type, listener, options) => {
|
|
401
|
-
const listenerList = windowEventLisenerMap.get(type);
|
|
402
|
-
if (listenerList?.length) {
|
|
403
|
-
const index = listenerList.indexOf(listener);
|
|
404
|
-
index > -1 && listenerList.splice(index, 1);
|
|
405
|
-
}
|
|
406
|
-
removeEventListener.call(rawWindow, type, listener, options);
|
|
407
|
-
};
|
|
408
|
-
fakeWindow.setInterval = (handler, timeout, ...args) => {
|
|
409
|
-
const timer = setInterval2.call(rawWindow, handler, timeout, ...args);
|
|
410
|
-
intervalTimerList.push(timer);
|
|
411
|
-
return timer;
|
|
412
|
-
};
|
|
413
|
-
fakeWindow.clearInterval = (timer) => {
|
|
414
|
-
const index = intervalTimerList.indexOf(timer);
|
|
415
|
-
index > -1 && intervalTimerList.splice(index, 1);
|
|
416
|
-
clearInterval2.call(rawWindow, timer);
|
|
417
|
-
};
|
|
418
|
-
function resetWindowFunction() {
|
|
419
|
-
if (windowEventLisenerMap.size) {
|
|
420
|
-
windowEventLisenerMap.forEach((listenerList, type) => {
|
|
421
|
-
listenerList.forEach((listener) => removeEventListener.call(rawWindow, type, listener));
|
|
422
|
-
});
|
|
423
|
-
windowEventLisenerMap.clear();
|
|
424
|
-
}
|
|
425
|
-
if (intervalTimerList.length) {
|
|
426
|
-
intervalTimerList.forEach((timer) => {
|
|
427
|
-
clearInterval2.call(rawWindow, timer);
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
return {
|
|
432
|
-
resetWindowFunction
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
// src/context/sandbox.ts
|
|
437
|
-
var SandBox = class {
|
|
438
|
-
constructor(app) {
|
|
439
|
-
this.app = app;
|
|
440
|
-
const windowDescriptorSet = /* @__PURE__ */ new Set();
|
|
441
|
-
const rawWindow = window;
|
|
442
|
-
this.rawWindow = rawWindow;
|
|
443
|
-
this.rawDocument = createProxyDocument(document, app);
|
|
444
|
-
const fakeWindow = /* @__PURE__ */ Object.create({});
|
|
445
|
-
fakeWindow.__BK_WEWEB_APP_KEY__ = app.appCacheKey;
|
|
446
|
-
fakeWindow.__POWERED_BY_BK_WEWEB__ = true;
|
|
447
|
-
fakeWindow.rawDocument = document;
|
|
448
|
-
fakeWindow.rawWindow = rawWindow;
|
|
449
|
-
fakeWindow.__proto__ = Window;
|
|
450
|
-
this.fakeWindow = fakeWindow;
|
|
451
|
-
const { resetWindowFunction } = rewriteWindowFunction(this.fakeWindow);
|
|
452
|
-
this.resetWindowFunction = resetWindowFunction;
|
|
453
|
-
this.windowSymbolKey = `__${(app.name || app.appCacheKey).replace(/(-|,|:|~|'|")/gim, "_")}_${random(
|
|
454
|
-
10
|
|
455
|
-
)}__`;
|
|
456
|
-
this.proxyWindow = new Proxy(this.fakeWindow, {
|
|
457
|
-
// Object.defineProperty(window, key, Descriptor)
|
|
458
|
-
defineProperty: (target, key, value) => {
|
|
459
|
-
if (windowDescriptorSet.has(key)) {
|
|
460
|
-
return Reflect.defineProperty(rawWindow, key, value);
|
|
461
|
-
}
|
|
462
|
-
return Reflect.defineProperty(target, key, value);
|
|
463
|
-
},
|
|
464
|
-
deleteProperty: (target, key) => {
|
|
465
|
-
if (Object.hasOwn(target, key)) {
|
|
466
|
-
this.sameRawWindowKeySet.has(key) && this.sameRawWindowKeySet.delete(key);
|
|
467
|
-
this.inRawWindowKeySet.has(key) && Reflect.deleteProperty(rawWindow, key);
|
|
468
|
-
return Reflect.deleteProperty(target, key);
|
|
469
|
-
}
|
|
470
|
-
return true;
|
|
471
|
-
},
|
|
472
|
-
get: (target, key) => {
|
|
473
|
-
if (key === Symbol.unscopables || windowNativeFuncMap.has(key)) return rawWindow[key];
|
|
474
|
-
if (DEV_MICRO_APP_WINDOE_KEY_MAP[key]) return this.fakeWindow[key];
|
|
475
|
-
if (WINDOW_ALIAS_LIST.includes(key)) return this.proxyWindow;
|
|
476
|
-
if (key === "document") {
|
|
477
|
-
app.registerRunningApp();
|
|
478
|
-
return this.rawDocument;
|
|
479
|
-
}
|
|
480
|
-
if (key === "eval") {
|
|
481
|
-
app.registerRunningApp();
|
|
482
|
-
return eval;
|
|
483
|
-
}
|
|
484
|
-
if (BK_WEWEB_LOCATION_KEY_LIST.includes(key) && this.app instanceof MicroAppModel && this.app.iframe && this.app.scopeLocation) {
|
|
485
|
-
return this.app.iframe.contentWindow?.[key];
|
|
486
|
-
}
|
|
487
|
-
if (key === "hasOwnProperty")
|
|
488
|
-
return (key2) => Object.hasOwn(this.fakeWindow, key2) || Object.hasOwn(rawWindow, key2);
|
|
489
|
-
if (key === "top" || key === "parent") {
|
|
490
|
-
if (rawWindow === rawWindow.parent) {
|
|
491
|
-
return this.proxyWindow;
|
|
492
|
-
}
|
|
493
|
-
return Reflect.get(rawWindow, key);
|
|
494
|
-
}
|
|
495
|
-
if (key === "getComputedStyle") {
|
|
496
|
-
return (element, pseudoElt) => {
|
|
497
|
-
if (element instanceof Element) {
|
|
498
|
-
return rawWindow.getComputedStyle(element, pseudoElt);
|
|
499
|
-
}
|
|
500
|
-
return rawWindow.getComputedStyle(document.body, pseudoElt);
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
if (Reflect.has(target, key) || BK_WEWEB_INJECT_KEY_LIST.includes(key)) return Reflect.get(target, key);
|
|
504
|
-
const rawValue = Reflect.get(rawWindow, key);
|
|
505
|
-
return bindFunctionToRawWindow(rawWindow, rawValue);
|
|
506
|
-
},
|
|
507
|
-
getOwnPropertyDescriptor: (target, key) => {
|
|
508
|
-
if (Object.hasOwn(target, key)) {
|
|
509
|
-
return Object.getOwnPropertyDescriptor(target, key);
|
|
510
|
-
}
|
|
511
|
-
if (Object.hasOwn(rawWindow, key)) {
|
|
512
|
-
windowDescriptorSet.add(key);
|
|
513
|
-
const descriptor = Object.getOwnPropertyDescriptor(rawWindow, key);
|
|
514
|
-
if (descriptor && !descriptor.configurable) {
|
|
515
|
-
descriptor.configurable = true;
|
|
516
|
-
}
|
|
517
|
-
return descriptor;
|
|
518
|
-
}
|
|
519
|
-
return void 0;
|
|
520
|
-
},
|
|
521
|
-
has: (target, key) => windowNativeFuncMap.has(key) || key in target || key in rawWindow,
|
|
522
|
-
// Object.getOwnPropertyNames(window)
|
|
523
|
-
ownKeys: (target) => Array.from(new Set(Reflect.ownKeys(rawWindow).concat(Reflect.ownKeys(target)))),
|
|
524
|
-
set: (target, key, value) => {
|
|
525
|
-
if (this.active) {
|
|
526
|
-
if (BK_WEWEB_LOCATION_KEY_LIST.includes(key) && this.app instanceof MicroAppModel && this.app.iframe && this.app.scopeLocation) {
|
|
527
|
-
return Reflect.set(this.app.iframe.contentWindow, key, value);
|
|
528
|
-
}
|
|
529
|
-
if (key === "location") {
|
|
530
|
-
Reflect.set(rawWindow, key, value);
|
|
531
|
-
} else if (!Object.hasOwn(target, key) && Object.hasOwn(rawWindow, key) && !BK_WEWEB_INJECT_KEY_LIST.includes(key)) {
|
|
532
|
-
const descriptor = Object.getOwnPropertyDescriptor(rawWindow, key);
|
|
533
|
-
const { configurable, enumerable, writable } = descriptor;
|
|
534
|
-
if (writable) {
|
|
535
|
-
Object.defineProperty(target, key, {
|
|
536
|
-
configurable,
|
|
537
|
-
enumerable,
|
|
538
|
-
value,
|
|
539
|
-
writable
|
|
540
|
-
});
|
|
541
|
-
this.sameRawWindowKeySet.add(key);
|
|
542
|
-
}
|
|
543
|
-
} else {
|
|
544
|
-
Reflect.set(target, key, value);
|
|
545
|
-
this.sameRawWindowKeySet.add(key);
|
|
546
|
-
}
|
|
547
|
-
if (WINDOW_WHITE_LIST.includes(key) && !Reflect.has(rawWindow, key) && !BK_WEWEB_INJECT_KEY_LIST.includes(key)) {
|
|
548
|
-
Reflect.set(rawWindow, key, value);
|
|
549
|
-
this.inRawWindowKeySet.add(key);
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
return true;
|
|
553
|
-
}
|
|
554
|
-
});
|
|
555
|
-
rawWindow[this.windowSymbolKey] = this.proxyWindow;
|
|
556
|
-
}
|
|
557
|
-
active = false;
|
|
558
|
-
inRawWindowKeySet = /* @__PURE__ */ new Set();
|
|
559
|
-
resetDocumentAndBodyEvent;
|
|
560
|
-
resetWindowFunction;
|
|
561
|
-
sameRawWindowKeySet = /* @__PURE__ */ new Set();
|
|
562
|
-
fakeWindow;
|
|
563
|
-
proxyDocument;
|
|
564
|
-
proxyWindow;
|
|
565
|
-
rawDocument;
|
|
566
|
-
rawWindow;
|
|
567
|
-
windowSymbolKey;
|
|
568
|
-
/**
|
|
569
|
-
*
|
|
570
|
-
* @param data data for sandbox
|
|
571
|
-
* @description active hook for sandbox
|
|
572
|
-
*/
|
|
573
|
-
activated(data) {
|
|
574
|
-
if (!this.active) {
|
|
575
|
-
this.active = true;
|
|
576
|
-
this.rawDocument = createProxyDocument(document, this.app);
|
|
577
|
-
this.fakeWindow.__BK_WEWEB_DATA__ = data ?? {};
|
|
578
|
-
const { resetDocumentAndBodyEvent } = rewriteDocumentAndBodyEvent();
|
|
579
|
-
this.resetDocumentAndBodyEvent = resetDocumentAndBodyEvent;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
*
|
|
584
|
-
* @description decativated hook for sandbox
|
|
585
|
-
*/
|
|
586
|
-
deactivated() {
|
|
587
|
-
if (!this.active) return;
|
|
588
|
-
this.active = false;
|
|
589
|
-
this.resetWindowFunction();
|
|
590
|
-
for (const key of this.inRawWindowKeySet) {
|
|
591
|
-
Reflect.deleteProperty(window, key);
|
|
592
|
-
}
|
|
593
|
-
this.inRawWindowKeySet.clear();
|
|
594
|
-
this.resetDocumentAndBodyEvent?.();
|
|
595
|
-
}
|
|
596
|
-
};
|
|
597
|
-
|
|
598
|
-
// src/utils/custom.ts
|
|
599
|
-
var ELEMENT_TARGET_NAME = ["currentTarget", "srcElement", "target"];
|
|
600
|
-
function defineEventSourceElement(element, eventName = "custom") {
|
|
601
|
-
return Object.defineProperties(
|
|
602
|
-
new CustomEvent(eventName),
|
|
603
|
-
ELEMENT_TARGET_NAME.reduce((props, name) => {
|
|
604
|
-
props[name] = {
|
|
605
|
-
get() {
|
|
606
|
-
return element;
|
|
607
|
-
}
|
|
608
|
-
};
|
|
609
|
-
return props;
|
|
610
|
-
}, {})
|
|
611
|
-
);
|
|
612
|
-
}
|
|
613
|
-
function dispatchLinkOrScriptLoad(element) {
|
|
614
|
-
const event = defineEventSourceElement(element, "load");
|
|
615
|
-
if (typeof element.onload === "function") {
|
|
616
|
-
element.onload(event);
|
|
617
|
-
return;
|
|
618
|
-
}
|
|
619
|
-
element.dispatchEvent(event);
|
|
620
|
-
}
|
|
621
|
-
function dispatchLinkOrScriptError(element) {
|
|
622
|
-
const event = defineEventSourceElement(element, "error");
|
|
623
|
-
if (typeof element.onerror === "function") {
|
|
624
|
-
element.onerror(event);
|
|
625
|
-
return;
|
|
626
|
-
}
|
|
627
|
-
element.dispatchEvent(event);
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
// src/utils/element.ts
|
|
631
|
-
var { appendChild: bodyAppendChild } = HTMLBodyElement.prototype;
|
|
632
|
-
function resetNewElement(parent, child, app) {
|
|
633
|
-
if (child instanceof HTMLStyleElement) {
|
|
634
|
-
if (child.hasAttribute("exclude")) {
|
|
635
|
-
return document.createComment("\u3010bk-weweb\u3011style with exclude attribute is ignored");
|
|
636
|
-
}
|
|
637
|
-
if (child.textContent) {
|
|
638
|
-
if (!(app.container instanceof ShadowRoot) && appCache.getBaseAppStyle(child.textContent)) {
|
|
639
|
-
return document.createComment("\u3010bk-weweb\u3011style is effective in base app");
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
if (!child.hasAttribute("ignore")) {
|
|
643
|
-
const styleInstance = new Style({
|
|
644
|
-
code: child.textContent || "",
|
|
645
|
-
fromHtml: false,
|
|
646
|
-
url: ""
|
|
647
|
-
});
|
|
648
|
-
app.source?.setStyle(randomUrl(), styleInstance);
|
|
649
|
-
styleInstance.scopedStyleCSS(app, child);
|
|
650
|
-
}
|
|
651
|
-
return child;
|
|
652
|
-
}
|
|
653
|
-
if (child instanceof HTMLLinkElement) {
|
|
654
|
-
const result = app.source?.collectLink(child, parent, true);
|
|
655
|
-
if (!result) return child;
|
|
656
|
-
if (result.style) {
|
|
657
|
-
result.style.scopedLinkCSS(app, child);
|
|
658
|
-
}
|
|
659
|
-
if (result.replace !== child) {
|
|
660
|
-
return result.replace;
|
|
661
|
-
}
|
|
662
|
-
return child;
|
|
663
|
-
}
|
|
664
|
-
if (child instanceof HTMLScriptElement) {
|
|
665
|
-
const replaceInfo = app.source.collectScript(child, parent, true);
|
|
666
|
-
if (!replaceInfo) {
|
|
667
|
-
return child;
|
|
668
|
-
}
|
|
669
|
-
if (replaceInfo.script) {
|
|
670
|
-
replaceInfo.script.executeCode(app);
|
|
671
|
-
}
|
|
672
|
-
if (replaceInfo.replace !== child) {
|
|
673
|
-
return replaceInfo.replace;
|
|
674
|
-
}
|
|
675
|
-
if (app.scopeJs && !child.getAttribute("src") && !child.textContent) {
|
|
676
|
-
const observer = new MutationObserver(() => {
|
|
677
|
-
if (child.getAttribute("src")) {
|
|
678
|
-
observer.disconnect();
|
|
679
|
-
const scriptInfo = app.source.collectScript(child, parent, true);
|
|
680
|
-
if (scriptInfo?.replace) {
|
|
681
|
-
bodyAppendChild.call(app.container, scriptInfo.replace);
|
|
682
|
-
}
|
|
683
|
-
if (isJsonpUrl(child.getAttribute("src"))) {
|
|
684
|
-
app.container?.append(child);
|
|
685
|
-
return;
|
|
686
|
-
}
|
|
687
|
-
if (scriptInfo?.script) {
|
|
688
|
-
scriptInfo.script.executeCode(app);
|
|
689
|
-
}
|
|
690
|
-
child.remove();
|
|
691
|
-
} else if (child.textContent) {
|
|
692
|
-
observer.disconnect();
|
|
693
|
-
const scriptInstance = new Script({
|
|
694
|
-
async: false,
|
|
695
|
-
code: child.textContent,
|
|
696
|
-
defer: child.type === "module",
|
|
697
|
-
fromHtml: false,
|
|
698
|
-
isModule: child.type === "module"
|
|
699
|
-
});
|
|
700
|
-
app.source.scripts.set(randomUrl(), scriptInstance);
|
|
701
|
-
try {
|
|
702
|
-
scriptInstance.executeCode(app);
|
|
703
|
-
} catch (e) {
|
|
704
|
-
console.error(e);
|
|
705
|
-
} finally {
|
|
706
|
-
!scriptInstance.isModule && dispatchLinkOrScriptLoad(child);
|
|
707
|
-
child.remove();
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
});
|
|
711
|
-
observer.observe(child, { attributeFilter: ["src"], childList: true, subtree: false });
|
|
712
|
-
return document.createComment("\u3010bk-weweb\u3011dynamic script or module");
|
|
713
|
-
}
|
|
714
|
-
return child;
|
|
715
|
-
}
|
|
716
|
-
return child;
|
|
717
|
-
}
|
|
718
|
-
function isSpecialElement(node) {
|
|
719
|
-
return node instanceof HTMLScriptElement || node instanceof HTMLStyleElement || node instanceof HTMLLinkElement;
|
|
720
|
-
}
|
|
721
|
-
function elementAppendHandler(parent, newChild, rawMethod) {
|
|
722
|
-
if (newChild.__BK_WEWEB_APP_KEY__) {
|
|
723
|
-
const app = appCache.getApp(newChild.__BK_WEWEB_APP_KEY__);
|
|
724
|
-
if (app?.container) {
|
|
725
|
-
const targetChild = resetNewElement(parent, newChild, app);
|
|
726
|
-
const needKeepAlive = isSpecialElement(newChild) && !!app.keepAlive && !(app.container instanceof ShadowRoot);
|
|
727
|
-
const container = needKeepAlive ? document.head : app?.container;
|
|
728
|
-
setMarkElement(targetChild, app, needKeepAlive);
|
|
729
|
-
return rawMethod.call(container, targetChild);
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
return rawMethod.call(parent, newChild);
|
|
733
|
-
}
|
|
734
|
-
function elementInsertHandler(parent, newChild, passiveChild, rawMethod) {
|
|
735
|
-
if (newChild.__BK_WEWEB_APP_KEY__) {
|
|
736
|
-
const app = appCache.getApp(newChild.__BK_WEWEB_APP_KEY__);
|
|
737
|
-
if (app?.container) {
|
|
738
|
-
const needKeepAlive = isSpecialElement(newChild) && app.keepAlive && !(app.container instanceof ShadowRoot);
|
|
739
|
-
const container = needKeepAlive ? document.head : app?.container;
|
|
740
|
-
const targetChild = resetNewElement(parent, newChild, app);
|
|
741
|
-
if (needKeepAlive) {
|
|
742
|
-
setMarkElement(targetChild, app, needKeepAlive);
|
|
743
|
-
}
|
|
744
|
-
if (passiveChild && !container.contains(passiveChild)) {
|
|
745
|
-
return bodyAppendChild.call(container, targetChild);
|
|
746
|
-
}
|
|
747
|
-
return rawMethod.call(container, targetChild, passiveChild);
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
return rawMethod.call(parent, newChild, passiveChild);
|
|
751
|
-
}
|
|
752
|
-
function setMarkElement(element, app, keepAlive) {
|
|
753
|
-
if (keepAlive && app) {
|
|
754
|
-
element.__KEEP_ALIVE__ = app.appCacheKey;
|
|
755
|
-
element.setAttribute("data-from", app.name);
|
|
756
|
-
element.setAttribute("data-keep-alive", "true");
|
|
757
|
-
}
|
|
758
|
-
element.setAttribute?.("powered-by", "bk-weweb");
|
|
759
|
-
return element;
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
// src/context/element.ts
|
|
763
|
-
var { setAttribute } = Element.prototype;
|
|
764
|
-
var {
|
|
765
|
-
append,
|
|
766
|
-
appendChild: bodyAppendChild2,
|
|
767
|
-
insertBefore: bodyInsertBefore,
|
|
768
|
-
removeChild: bodyRemoveChild
|
|
769
|
-
} = HTMLBodyElement.prototype;
|
|
770
|
-
var {
|
|
771
|
-
appendChild: headAppendChild,
|
|
772
|
-
insertBefore: headInsertBefore,
|
|
773
|
-
removeChild: headRemoveChild
|
|
774
|
-
} = HTMLHeadElement.prototype;
|
|
775
|
-
var rawHead = document.head;
|
|
776
|
-
var hasRewrite = false;
|
|
777
|
-
function rewriteBodyAndHeaderMethods() {
|
|
778
|
-
if (hasRewrite) return;
|
|
779
|
-
hasRewrite = true;
|
|
780
|
-
Element.prototype.setAttribute = function(key, value) {
|
|
781
|
-
const tagName = this.tagName.toLocaleUpperCase();
|
|
782
|
-
if ((["src", "srcset"].includes(key) && ["IMG", "SCRIPT"].includes(tagName) || key === "href" && ["LINK"].includes(tagName)) && this.__BK_WEWEB_APP_KEY__ && appCache.getApp(this.__BK_WEWEB_APP_KEY__ || "")) {
|
|
783
|
-
setAttribute.call(this, key, fillUpPath(value, appCache.getApp(this.__BK_WEWEB_APP_KEY__).url));
|
|
784
|
-
} else {
|
|
785
|
-
setAttribute.call(this, key, value);
|
|
786
|
-
}
|
|
787
|
-
};
|
|
788
|
-
HTMLBodyElement.prototype.appendChild = function appendChildNew(newChild) {
|
|
789
|
-
if (newChild.__KEEP_ALIVE__ && isSpecialElement(newChild)) return headAppendChild.call(rawHead, newChild);
|
|
790
|
-
return elementAppendHandler(this, newChild, bodyAppendChild2);
|
|
791
|
-
};
|
|
792
|
-
HTMLBodyElement.prototype.append = function(...nodes) {
|
|
793
|
-
nodes.forEach((node) => {
|
|
794
|
-
if (node.__KEEP_ALIVE__ && isSpecialElement(node)) {
|
|
795
|
-
return headAppendChild.call(rawHead, node);
|
|
796
|
-
}
|
|
797
|
-
elementAppendHandler(this, node, bodyAppendChild2);
|
|
798
|
-
});
|
|
799
|
-
};
|
|
800
|
-
HTMLHeadElement.prototype.appendChild = HTMLBodyElement.prototype.appendChild;
|
|
801
|
-
HTMLHeadElement.prototype.insertBefore = function(newChild, refChild) {
|
|
802
|
-
return elementInsertHandler(this, newChild, refChild, headInsertBefore);
|
|
803
|
-
};
|
|
804
|
-
HTMLBodyElement.prototype.insertBefore = function(newChild, refChild) {
|
|
805
|
-
return elementInsertHandler(this, newChild, refChild, headInsertBefore);
|
|
806
|
-
};
|
|
807
|
-
HTMLBodyElement.prototype.removeChild = function removeChildNew(oldChild) {
|
|
808
|
-
const app = appCache.getApp(oldChild.__BK_WEWEB_APP_KEY__);
|
|
809
|
-
if (app?.container?.contains(oldChild)) {
|
|
810
|
-
const node = bodyRemoveChild.call(app.container, oldChild);
|
|
811
|
-
return node;
|
|
812
|
-
}
|
|
813
|
-
if (this.contains(oldChild)) {
|
|
814
|
-
return bodyRemoveChild.call(this, oldChild);
|
|
815
|
-
}
|
|
816
|
-
return oldChild;
|
|
817
|
-
};
|
|
818
|
-
HTMLHeadElement.prototype.removeChild = HTMLBodyElement.prototype.removeChild;
|
|
819
|
-
}
|
|
820
|
-
function resetBodyAndHeaderMethods() {
|
|
821
|
-
setCurrentRunningApp(null);
|
|
822
|
-
Element.prototype.setAttribute = setAttribute;
|
|
823
|
-
HTMLBodyElement.prototype.appendChild = bodyAppendChild2;
|
|
824
|
-
HTMLBodyElement.prototype.append = append;
|
|
825
|
-
HTMLBodyElement.prototype.removeChild = bodyRemoveChild;
|
|
826
|
-
HTMLBodyElement.prototype.insertBefore = bodyInsertBefore;
|
|
827
|
-
HTMLHeadElement.prototype.appendChild = headAppendChild;
|
|
828
|
-
HTMLHeadElement.prototype.insertBefore = headInsertBefore;
|
|
829
|
-
HTMLHeadElement.prototype.removeChild = headRemoveChild;
|
|
830
|
-
hasRewrite = false;
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
// src/lifecircle/before-load.ts
|
|
834
|
-
function beforeLoad() {
|
|
835
|
-
rewriteBodyAndHeaderMethods();
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
// src/lifecircle/activated.ts
|
|
839
|
-
function activated(appKey, container, callback) {
|
|
840
|
-
const app = appCache.getApp(appKey);
|
|
841
|
-
if (app?.status === "DEACTIVATED" /* DEACTIVATED */ && app.keepAlive) {
|
|
842
|
-
nextTask(() => {
|
|
843
|
-
beforeLoad();
|
|
844
|
-
app.activated(container, callback);
|
|
845
|
-
});
|
|
846
|
-
} else {
|
|
847
|
-
app && nextTask(() => {
|
|
848
|
-
beforeLoad();
|
|
849
|
-
app.mount(container, callback);
|
|
850
|
-
});
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
// src/lifecircle/deactivated.ts
|
|
855
|
-
function deactivated(appKey) {
|
|
856
|
-
const app = appCache.getApp(appKey);
|
|
857
|
-
if (app && ["ACTIVATED" /* ACTIVATED */, "MOUNTED" /* MOUNTED */].includes(app.status)) {
|
|
858
|
-
app.keepAlive ? app.deactivated() : app.unmount();
|
|
859
|
-
}
|
|
860
|
-
if (!appCache.hasActiveApp) {
|
|
861
|
-
resetBodyAndHeaderMethods();
|
|
862
|
-
}
|
|
863
|
-
}
|
|
864
|
-
|
|
865
|
-
// src/lifecircle/load.ts
|
|
866
|
-
async function load(props) {
|
|
867
|
-
beforeLoad();
|
|
868
|
-
if (props.mode === "js" /* INSTANCE */) return await loadInstance(props);
|
|
869
|
-
return await loadApp(props);
|
|
870
|
-
}
|
|
871
|
-
async function loadApp(props) {
|
|
872
|
-
beforeLoad();
|
|
873
|
-
let instance = appCache.getApp(props.id);
|
|
874
|
-
if (!instance) {
|
|
875
|
-
instance = new MicroAppModel(props);
|
|
876
|
-
appCache.setApp(instance);
|
|
877
|
-
}
|
|
878
|
-
await instance.start();
|
|
879
|
-
return instance;
|
|
880
|
-
}
|
|
881
|
-
function loadInstance(props) {
|
|
882
|
-
beforeLoad();
|
|
883
|
-
return new Promise((resolve) => {
|
|
884
|
-
let instance = appCache.getApp(props.id);
|
|
885
|
-
if (!instance) {
|
|
886
|
-
instance = new MicroInstanceModel(props);
|
|
887
|
-
appCache.setApp(instance);
|
|
888
|
-
instance.start().then(() => resolve(instance));
|
|
889
|
-
} else if (["MOUNTING" /* MOUNTING */, "UNSET" /* UNSET */].includes(instance.status)) {
|
|
890
|
-
const timer = setInterval(() => {
|
|
891
|
-
if (["ERROR" /* ERROR */, "MOUNTED" /* MOUNTED */].includes(instance.status)) {
|
|
892
|
-
resolve(instance);
|
|
893
|
-
clearInterval(timer);
|
|
894
|
-
}
|
|
895
|
-
}, 300);
|
|
896
|
-
} else {
|
|
897
|
-
resolve(instance);
|
|
898
|
-
}
|
|
899
|
-
});
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
// src/lifecircle/unmount.ts
|
|
903
|
-
function unmount(appKey) {
|
|
904
|
-
const app = appCache.getApp(appKey);
|
|
905
|
-
if (app && app.status !== "UNMOUNT" /* UNMOUNT */) {
|
|
906
|
-
app.unmount();
|
|
907
|
-
}
|
|
908
|
-
if (!appCache.hasActiveApp) {
|
|
909
|
-
resetBodyAndHeaderMethods();
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
// src/component/web-compnent.ts
|
|
914
|
-
var BkWewebElement = class extends HTMLElement {
|
|
915
|
-
appUrl = "";
|
|
916
|
-
connected = false;
|
|
917
|
-
static get observedAttributes() {
|
|
918
|
-
return ["url" /* url */];
|
|
919
|
-
}
|
|
920
|
-
getBooleanAttr(name) {
|
|
921
|
-
return this.hasAttribute(name) || this.hasAttribute(name.replace(/([A-Z])/g, "-$1").toLocaleLowerCase()) ? this.getAttribute(name) !== "false" : void 0;
|
|
922
|
-
}
|
|
923
|
-
async handleAttributeChanged() {
|
|
924
|
-
if (!this.appKey) return;
|
|
925
|
-
if (this.getBooleanAttr("setShodowDom" /* setShodowDom */)) {
|
|
926
|
-
this.attachShadow({ mode: "open" });
|
|
927
|
-
}
|
|
928
|
-
const app = appCache.getApp(this.appKey);
|
|
929
|
-
if (app && app.url === this.appUrl && (app.isPreLoad || app.status === "UNMOUNT" /* UNMOUNT */)) {
|
|
930
|
-
activated(this.appKey, this.shadowRoot ?? this);
|
|
931
|
-
return;
|
|
932
|
-
}
|
|
933
|
-
await load(this.appProps);
|
|
934
|
-
}
|
|
935
|
-
attributeChangedCallback(attr, _oldVal, newVal) {
|
|
936
|
-
if (attr !== "url" /* url */ || this[attr] === newVal || !this.connected) return;
|
|
937
|
-
this.appUrl = newVal;
|
|
938
|
-
const cacheApp = appCache.getApp(this.appKey);
|
|
939
|
-
(this.connected || cacheApp) && this.handleAttributeChanged();
|
|
940
|
-
}
|
|
941
|
-
async connectedCallback() {
|
|
942
|
-
if (this.getBooleanAttr("setShodowDom" /* setShodowDom */) && !this.shadowRoot) {
|
|
943
|
-
this.attachShadow({ delegatesFocus: false, mode: "open" });
|
|
944
|
-
}
|
|
945
|
-
await load(this.appProps);
|
|
946
|
-
activated(this.appKey, this.shadowRoot ?? this);
|
|
947
|
-
this.connected = true;
|
|
948
|
-
}
|
|
949
|
-
disconnectedCallback() {
|
|
950
|
-
this.connected = false;
|
|
951
|
-
if (this.appProps.keepAlive) {
|
|
952
|
-
deactivated(this.appKey);
|
|
953
|
-
} else unmount(this.appKey);
|
|
954
|
-
}
|
|
955
|
-
get appData() {
|
|
956
|
-
if (this.hasAttribute("data" /* data */)) {
|
|
957
|
-
try {
|
|
958
|
-
return JSON.parse(this.getAttribute("data" /* data */));
|
|
959
|
-
} catch {
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
return {};
|
|
963
|
-
}
|
|
964
|
-
// 考虑到js模式下 需要js bundle的复用性 需用户设置id属性 如果单实例下则可以不用配置
|
|
965
|
-
get appKey() {
|
|
966
|
-
return this.getAttribute("id" /* id */) || this.getAttribute("url" /* url */);
|
|
967
|
-
}
|
|
968
|
-
get appProps() {
|
|
969
|
-
if (this.getAttribute("mode") === "js" /* INSTANCE */) {
|
|
970
|
-
return {
|
|
971
|
-
container: this.shadowRoot ?? this,
|
|
972
|
-
data: this.appData,
|
|
973
|
-
id: this.appKey,
|
|
974
|
-
keepAlive: this.getBooleanAttr("keepAlive" /* keepAlive */),
|
|
975
|
-
mode: "js" /* INSTANCE */,
|
|
976
|
-
scopeCss: this.getBooleanAttr("scopeCss" /* scopeCss */) && !this.getBooleanAttr("setShodowDom" /* setShodowDom */),
|
|
977
|
-
scopeJs: this.getBooleanAttr("scopeJs" /* scopeJs */),
|
|
978
|
-
showSourceCode: this.getBooleanAttr("showSourceCode" /* showSourceCode */),
|
|
979
|
-
url: this.getAttribute("url" /* url */)
|
|
980
|
-
};
|
|
981
|
-
}
|
|
982
|
-
return {
|
|
983
|
-
container: this.shadowRoot ?? this,
|
|
984
|
-
data: this.appData,
|
|
985
|
-
id: this.appKey,
|
|
986
|
-
keepAlive: this.getBooleanAttr("keepAlive" /* keepAlive */),
|
|
987
|
-
mode: "app" /* APP */,
|
|
988
|
-
scopeCss: !this.getBooleanAttr("setShodowDom" /* setShodowDom */),
|
|
989
|
-
scopeJs: !this.getBooleanAttr("scopeJs" /* scopeJs */),
|
|
990
|
-
scopeLocation: this.getBooleanAttr("scopeLocation" /* scopeLocation */),
|
|
991
|
-
showSourceCode: this.getBooleanAttr("showSourceCode" /* showSourceCode */),
|
|
992
|
-
url: this.getAttribute("url" /* url */)
|
|
993
|
-
};
|
|
994
|
-
}
|
|
995
|
-
};
|
|
996
|
-
|
|
997
|
-
// src/utils/load-source.ts
|
|
998
|
-
async function collectSource(soruceList) {
|
|
999
|
-
let source = [];
|
|
1000
|
-
if (typeof soruceList === "function") {
|
|
1001
|
-
source = await soruceList();
|
|
1002
|
-
} else {
|
|
1003
|
-
source = soruceList || [];
|
|
1004
|
-
}
|
|
1005
|
-
if (!source.length) return {};
|
|
1006
|
-
const collectScript = /* @__PURE__ */ new Map();
|
|
1007
|
-
const collectStyle = /* @__PURE__ */ new Map();
|
|
1008
|
-
source.forEach((str) => {
|
|
1009
|
-
try {
|
|
1010
|
-
const url = new URL(str);
|
|
1011
|
-
if (url.pathname.match(/\.js$/)) {
|
|
1012
|
-
const script = appCache.getCacheScript(str);
|
|
1013
|
-
collectScript.set(
|
|
1014
|
-
str,
|
|
1015
|
-
new Script({
|
|
1016
|
-
async: false,
|
|
1017
|
-
code: script?.code || "",
|
|
1018
|
-
defer: false,
|
|
1019
|
-
fromHtml: false,
|
|
1020
|
-
initial: true,
|
|
1021
|
-
isModule: false,
|
|
1022
|
-
url: str
|
|
1023
|
-
})
|
|
1024
|
-
);
|
|
1025
|
-
} else if (url.pathname.match(/\.css$/)) {
|
|
1026
|
-
const style = appCache.getCacheStyle(str);
|
|
1027
|
-
collectStyle.set(
|
|
1028
|
-
str,
|
|
1029
|
-
new Style({
|
|
1030
|
-
code: style?.code || "",
|
|
1031
|
-
fromHtml: true,
|
|
1032
|
-
initial: true,
|
|
1033
|
-
prefetch: false,
|
|
1034
|
-
preload: false,
|
|
1035
|
-
url: str
|
|
1036
|
-
})
|
|
1037
|
-
);
|
|
1038
|
-
}
|
|
1039
|
-
} catch {
|
|
1040
|
-
console.error(`\u3010bk-weweb\u3011: ${str} is invalid URL`);
|
|
1041
|
-
}
|
|
1042
|
-
});
|
|
1043
|
-
return {
|
|
1044
|
-
collectScript,
|
|
1045
|
-
collectStyle
|
|
1046
|
-
};
|
|
1047
|
-
}
|
|
1048
|
-
|
|
1049
|
-
// src/index.ts
|
|
1050
|
-
var CUSTOM_ELEMENT_TAG = "bk-weweb";
|
|
1051
|
-
var WeWeb = class {
|
|
1052
|
-
fetchSource;
|
|
1053
|
-
webcomponentTag = CUSTOM_ELEMENT_TAG;
|
|
1054
|
-
constructor() {
|
|
1055
|
-
if (!window.customElements.get(CUSTOM_ELEMENT_TAG)) {
|
|
1056
|
-
window.customElements.define(CUSTOM_ELEMENT_TAG, BkWewebElement);
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
// 设置自定义dom标签名
|
|
1060
|
-
setWebComponentTag() {
|
|
1061
|
-
if (!window.customElements.get(this.webcomponentTag)) {
|
|
1062
|
-
window.customElements.define(this.webcomponentTag, BkWewebElement);
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
// todo set some global start props
|
|
1066
|
-
start(option) {
|
|
1067
|
-
if (option?.collectBaseSource) {
|
|
1068
|
-
collectBaseSource();
|
|
1069
|
-
}
|
|
1070
|
-
if (typeof option?.fetchSource === "function") {
|
|
1071
|
-
this.fetchSource = option.fetchSource;
|
|
1072
|
-
}
|
|
1073
|
-
this.webcomponentTag = option?.webcomponentTag || CUSTOM_ELEMENT_TAG;
|
|
1074
|
-
this.setWebComponentTag();
|
|
1075
|
-
}
|
|
1076
|
-
};
|
|
1077
|
-
var weWeb = new WeWeb();
|
|
1078
|
-
var src_default = weWeb;
|
|
1079
|
-
|
|
1080
|
-
// src/utils/fetch.ts
|
|
1081
|
-
function fetchSource(url, options = {}, app) {
|
|
1082
|
-
if (typeof app?.fetchSource === "function") {
|
|
1083
|
-
return app.fetchSource(url, options).catch(() => "");
|
|
1084
|
-
}
|
|
1085
|
-
if (src_default.fetchSource) {
|
|
1086
|
-
return src_default.fetchSource(url, options);
|
|
1087
|
-
}
|
|
1088
|
-
return window.fetch(url, options).then((res) => res.text());
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
// src/entry/style.ts
|
|
1092
|
-
var Style = class {
|
|
1093
|
-
code = "";
|
|
1094
|
-
fromHtml;
|
|
1095
|
-
initial;
|
|
1096
|
-
prefetch = false;
|
|
1097
|
-
preload = false;
|
|
1098
|
-
scoped;
|
|
1099
|
-
scopedCode = "";
|
|
1100
|
-
url;
|
|
1101
|
-
constructor({ code, fromHtml, initial, prefetch, preload, url }) {
|
|
1102
|
-
this.scoped = false;
|
|
1103
|
-
this.code = code;
|
|
1104
|
-
this.prefetch = prefetch ?? false;
|
|
1105
|
-
this.preload = preload ?? false;
|
|
1106
|
-
this.url = url;
|
|
1107
|
-
this.fromHtml = fromHtml;
|
|
1108
|
-
this.initial = initial ?? false;
|
|
1109
|
-
}
|
|
1110
|
-
/**
|
|
1111
|
-
* @param styleElement 样式node
|
|
1112
|
-
* @param app 应用实例
|
|
1113
|
-
*/
|
|
1114
|
-
commonScoped(styleElement, app) {
|
|
1115
|
-
if (app.scopeCss && !(app.container instanceof ShadowRoot)) {
|
|
1116
|
-
const cssStyleSheet = new CSSStyleSheet({ disabled: true });
|
|
1117
|
-
cssStyleSheet.replaceSync(styleElement.textContent || this.code);
|
|
1118
|
-
const rules = Array.from(cssStyleSheet?.cssRules ?? []);
|
|
1119
|
-
const cssPrefix = `#${app.name}`;
|
|
1120
|
-
const scopedCss = this.scopeRule(rules, cssPrefix);
|
|
1121
|
-
const cssText = this.resetUrlHost(scopedCss, app.url, this.url);
|
|
1122
|
-
styleElement.textContent = cssText;
|
|
1123
|
-
this.scopedCode = cssText;
|
|
1124
|
-
} else {
|
|
1125
|
-
const cssText = this.resetUrlHost(styleElement.textContent || this.code || "", app.url, this.url);
|
|
1126
|
-
if (cssText && app.container instanceof ShadowRoot) {
|
|
1127
|
-
let fontContent = "";
|
|
1128
|
-
cssText.match(/@font-face\s*\{[^}]+\}/g)?.forEach((fontFace) => {
|
|
1129
|
-
fontContent += `${fontFace}
|
|
1130
|
-
`;
|
|
1131
|
-
});
|
|
1132
|
-
const rawDocument = app.sandBox?.rawDocument;
|
|
1133
|
-
if (rawDocument && fontContent) {
|
|
1134
|
-
const fontStyle = rawDocument.createElement("style");
|
|
1135
|
-
fontStyle.setAttribute("type", "text/css");
|
|
1136
|
-
fontStyle.setAttribute("powered-by", "bk-weweb");
|
|
1137
|
-
fontStyle.textContent = fontContent;
|
|
1138
|
-
rawDocument?.head?.append(fontStyle);
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
styleElement.textContent = cssText;
|
|
1142
|
-
}
|
|
1143
|
-
this.scoped = true;
|
|
1144
|
-
}
|
|
1145
|
-
createStyleElement() {
|
|
1146
|
-
const styleElement = document.createElement("style");
|
|
1147
|
-
if (styleElement.__BK_WEWEB_APP_KEY__) {
|
|
1148
|
-
styleElement.__BK_WEWEB_APP_KEY__ = void 0;
|
|
1149
|
-
}
|
|
1150
|
-
return styleElement;
|
|
1151
|
-
}
|
|
1152
|
-
/**
|
|
1153
|
-
* @param app 应用实例
|
|
1154
|
-
* @returns 返回执行后的style标签
|
|
1155
|
-
*/
|
|
1156
|
-
async executeCode(app) {
|
|
1157
|
-
app.registerRunningApp();
|
|
1158
|
-
let styleElement = this.createStyleElement();
|
|
1159
|
-
styleElement.setAttribute("type", "text/css");
|
|
1160
|
-
styleElement.textContent = this.code;
|
|
1161
|
-
try {
|
|
1162
|
-
if (!this.code) await this.getCode(app);
|
|
1163
|
-
styleElement = this.scopedStyleCSS(app, styleElement);
|
|
1164
|
-
this.scoped = true;
|
|
1165
|
-
} catch (e) {
|
|
1166
|
-
console.error("scoped style error", e);
|
|
1167
|
-
}
|
|
1168
|
-
return styleElement;
|
|
1169
|
-
}
|
|
1170
|
-
async getCode(app) {
|
|
1171
|
-
if (this.code.length || !this.url) {
|
|
1172
|
-
return this.code;
|
|
1173
|
-
}
|
|
1174
|
-
let code = "";
|
|
1175
|
-
if (app?.source?.styles?.has(this.url)) {
|
|
1176
|
-
code = app.source.styles.get(this.url)?.code || "";
|
|
1177
|
-
}
|
|
1178
|
-
if (!code && appCache.getCacheStyle(this.url)) {
|
|
1179
|
-
const style = appCache.getCacheStyle(this.url);
|
|
1180
|
-
code = style?.code || "";
|
|
1181
|
-
}
|
|
1182
|
-
if (!code) {
|
|
1183
|
-
code = await fetchSource(this.url, {}, app).catch(() => "");
|
|
1184
|
-
}
|
|
1185
|
-
this.code = code;
|
|
1186
|
-
return code;
|
|
1187
|
-
}
|
|
1188
|
-
// 主应用已生效的样式 不再应用在子应用
|
|
1189
|
-
linkedBaseStyle(styleElement, app) {
|
|
1190
|
-
if (!(app.container instanceof ShadowRoot) && styleElement.textContent && appCache.getBaseAppStyle(styleElement.textContent)) {
|
|
1191
|
-
styleElement.textContent = "";
|
|
1192
|
-
styleElement.innerHTML = "";
|
|
1193
|
-
styleElement.setAttribute("linked-from-base", "true");
|
|
1194
|
-
return true;
|
|
1195
|
-
}
|
|
1196
|
-
return false;
|
|
1197
|
-
}
|
|
1198
|
-
resetPackRule(rule, prefix, packName) {
|
|
1199
|
-
const result = this.scopeRule(Array.from(rule.cssRules), prefix);
|
|
1200
|
-
return `@${packName} ${rule.conditionText} {${result}}`;
|
|
1201
|
-
}
|
|
1202
|
-
resetUrlHost(cssText, uri, linkPath) {
|
|
1203
|
-
let baseURI = uri;
|
|
1204
|
-
return cssText.replace(/url\(["']?([^)"']+)["']?\)/gm, (text, $1) => {
|
|
1205
|
-
if (/^(data|blob):/.test($1) || /^(https?:)?\/\//.test($1)) {
|
|
1206
|
-
return text;
|
|
1207
|
-
}
|
|
1208
|
-
if (/^((\.\.?\/)|[^/])/.test($1) && linkPath) {
|
|
1209
|
-
const pathArr = linkPath.split("/");
|
|
1210
|
-
pathArr.pop();
|
|
1211
|
-
baseURI = addUrlProtocol(`${pathArr.join("/")}/`);
|
|
1212
|
-
}
|
|
1213
|
-
return `url("${fillUpPath($1, baseURI)}")`;
|
|
1214
|
-
});
|
|
1215
|
-
}
|
|
1216
|
-
scopeRule(rules, cssPrefix) {
|
|
1217
|
-
let result = "";
|
|
1218
|
-
for (const rule of rules) {
|
|
1219
|
-
switch (rule.type) {
|
|
1220
|
-
case 1 /* STYLE_RULE */:
|
|
1221
|
-
result += this.scopeStyleRule(rule, cssPrefix);
|
|
1222
|
-
break;
|
|
1223
|
-
case 4 /* MEDIA_RULE */:
|
|
1224
|
-
result += this.resetPackRule(rule, cssPrefix, "media");
|
|
1225
|
-
break;
|
|
1226
|
-
case 12 /* SUPPORTS_RULE */:
|
|
1227
|
-
result += this.resetPackRule(rule, cssPrefix, "supports");
|
|
1228
|
-
break;
|
|
1229
|
-
default:
|
|
1230
|
-
result += rule.cssText;
|
|
1231
|
-
break;
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
return result.replace(/^\s+/, "");
|
|
1235
|
-
}
|
|
1236
|
-
scopeStyleRule(rule, prefix) {
|
|
1237
|
-
const { cssText, selectorText } = rule;
|
|
1238
|
-
if (/^((html[\s>~,]+body)|(html|body|:root))$/.test(selectorText)) {
|
|
1239
|
-
return cssText.replace(/^((html[\s>~,]+body)|(html|body|:root))/, prefix);
|
|
1240
|
-
}
|
|
1241
|
-
if (selectorText === "*") {
|
|
1242
|
-
return cssText.replace("*", `${prefix} *`);
|
|
1243
|
-
}
|
|
1244
|
-
const builtInRootSelectorRE = /(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;
|
|
1245
|
-
return cssText.replace(
|
|
1246
|
-
/^[\s\S]+{/,
|
|
1247
|
-
(selectors) => selectors.replace(/(^|,)([^,]+)/g, (all, $1, $2) => {
|
|
1248
|
-
if (builtInRootSelectorRE.test($2)) {
|
|
1249
|
-
return all.replace(builtInRootSelectorRE, prefix);
|
|
1250
|
-
}
|
|
1251
|
-
return `${$1} ${prefix} ${$2.replace(/^\s*/, "")}`;
|
|
1252
|
-
})
|
|
1253
|
-
);
|
|
1254
|
-
}
|
|
1255
|
-
scopedLinkCSS(app, linkElement) {
|
|
1256
|
-
const styleElement = this.createStyleElement();
|
|
1257
|
-
styleElement.setAttribute("type", "text/css");
|
|
1258
|
-
const needKeepAlive = !!app.keepAlive && !(app.container instanceof ShadowRoot);
|
|
1259
|
-
setMarkElement(styleElement, app, needKeepAlive);
|
|
1260
|
-
const container = needKeepAlive ? document.head : app.container;
|
|
1261
|
-
try {
|
|
1262
|
-
if (this.code) {
|
|
1263
|
-
this.commonScoped(styleElement, app);
|
|
1264
|
-
container?.prepend(styleElement);
|
|
1265
|
-
linkElement && dispatchLinkOrScriptLoad(linkElement);
|
|
1266
|
-
} else if (linkElement.getAttribute("href")) {
|
|
1267
|
-
this.url = fillUpPath(linkElement.getAttribute("href"), app.url);
|
|
1268
|
-
this.getCode(app).then(() => {
|
|
1269
|
-
this.scopedStyleCSS(app, styleElement);
|
|
1270
|
-
linkElement.remove();
|
|
1271
|
-
container?.prepend(styleElement);
|
|
1272
|
-
linkElement && dispatchLinkOrScriptLoad(linkElement);
|
|
1273
|
-
this.scoped = true;
|
|
1274
|
-
});
|
|
1275
|
-
} else {
|
|
1276
|
-
const observer = new MutationObserver(() => {
|
|
1277
|
-
if (!linkElement.href) return;
|
|
1278
|
-
observer.disconnect();
|
|
1279
|
-
this.url = fillUpPath(linkElement.getAttribute("href"), app.url);
|
|
1280
|
-
this.getCode(app).then(() => {
|
|
1281
|
-
this.scopedStyleCSS(app, styleElement);
|
|
1282
|
-
linkElement.remove();
|
|
1283
|
-
container?.prepend(styleElement);
|
|
1284
|
-
linkElement && dispatchLinkOrScriptLoad(linkElement);
|
|
1285
|
-
this.scoped = true;
|
|
1286
|
-
});
|
|
1287
|
-
});
|
|
1288
|
-
observer.observe(linkElement, { attributeFilter: ["href"], childList: false, subtree: false });
|
|
1289
|
-
}
|
|
1290
|
-
} catch {
|
|
1291
|
-
linkElement && dispatchLinkOrScriptError(linkElement);
|
|
1292
|
-
}
|
|
1293
|
-
return styleElement;
|
|
1294
|
-
}
|
|
1295
|
-
scopedStyleCSS(app, styleElement) {
|
|
1296
|
-
const needKeepAlive = !!app.keepAlive && !(app.container instanceof ShadowRoot);
|
|
1297
|
-
setMarkElement(styleElement, app, needKeepAlive);
|
|
1298
|
-
if (this.code || styleElement.textContent) {
|
|
1299
|
-
if (styleElement.textContent) {
|
|
1300
|
-
styleElement.textContent = "";
|
|
1301
|
-
styleElement.innerHTML = "";
|
|
1302
|
-
}
|
|
1303
|
-
if (this.linkedBaseStyle(styleElement, app)) return styleElement;
|
|
1304
|
-
this.commonScoped(styleElement, app);
|
|
1305
|
-
} else {
|
|
1306
|
-
const observer = new MutationObserver(() => {
|
|
1307
|
-
if (!(styleElement.textContent || styleElement.sheet?.cssRules?.length)) return;
|
|
1308
|
-
observer.disconnect();
|
|
1309
|
-
if (!this.linkedBaseStyle(styleElement, app)) {
|
|
1310
|
-
this.commonScoped(styleElement, app);
|
|
1311
|
-
}
|
|
1312
|
-
});
|
|
1313
|
-
observer.observe(styleElement, { attributes: false, characterData: true, childList: true, subtree: true });
|
|
1314
|
-
}
|
|
1315
|
-
this.url && styleElement.setAttribute("origin-src", this.url);
|
|
1316
|
-
return styleElement;
|
|
1317
|
-
}
|
|
1318
|
-
};
|
|
1319
|
-
async function executeAppStyles(app, container) {
|
|
1320
|
-
const styleList = Array.from(app.source.styles.values());
|
|
1321
|
-
const promiseList = [];
|
|
1322
|
-
for (const style of styleList) {
|
|
1323
|
-
promiseList.push(style.executeCode(app));
|
|
1324
|
-
}
|
|
1325
|
-
await Promise.all(promiseList).then((styleElementList) => {
|
|
1326
|
-
const parentElement = container || app.container;
|
|
1327
|
-
if (app.keepAlive && !(parentElement instanceof ShadowRoot)) {
|
|
1328
|
-
document.head.append(...styleElementList);
|
|
1329
|
-
} else {
|
|
1330
|
-
parentElement?.append(...styleElementList);
|
|
1331
|
-
}
|
|
1332
|
-
});
|
|
1333
|
-
}
|
|
1334
|
-
|
|
1335
|
-
// src/mode/instance.ts
|
|
1336
|
-
var MicroInstanceModel = class {
|
|
1337
|
-
state = "UNSET" /* UNSET */;
|
|
1338
|
-
// 当前实例状态
|
|
1339
|
-
appCacheKey;
|
|
1340
|
-
// 缓存key
|
|
1341
|
-
container;
|
|
1342
|
-
// 容器
|
|
1343
|
-
data;
|
|
1344
|
-
// 数据
|
|
1345
|
-
initSource;
|
|
1346
|
-
// 初始资源
|
|
1347
|
-
isPreLoad = false;
|
|
1348
|
-
// 是否预加载
|
|
1349
|
-
keepAlive;
|
|
1350
|
-
// 是否缓存
|
|
1351
|
-
name;
|
|
1352
|
-
// 名称
|
|
1353
|
-
sandBox;
|
|
1354
|
-
// 沙箱
|
|
1355
|
-
scopeCss = true;
|
|
1356
|
-
// 是否隔离样式
|
|
1357
|
-
scopeJs = false;
|
|
1358
|
-
// 是否隔离js
|
|
1359
|
-
showSourceCode = true;
|
|
1360
|
-
// 是否显示源码
|
|
1361
|
-
source;
|
|
1362
|
-
// 入口资源
|
|
1363
|
-
url;
|
|
1364
|
-
// url
|
|
1365
|
-
fetchSource;
|
|
1366
|
-
constructor(props) {
|
|
1367
|
-
this.name = props.id !== props.url ? props.id : random(5);
|
|
1368
|
-
this.appCacheKey = props.id || this.name;
|
|
1369
|
-
this.url = props.url;
|
|
1370
|
-
this.container = props.container ?? void 0;
|
|
1371
|
-
this.scopeJs = props.scopeJs ?? true;
|
|
1372
|
-
this.showSourceCode = props.showSourceCode ?? true;
|
|
1373
|
-
this.scopeCss = props.scopeCss ?? true;
|
|
1374
|
-
this.keepAlive = props.keepAlive ?? false;
|
|
1375
|
-
this.data = props.data ?? {};
|
|
1376
|
-
this.initSource = props.initSource ?? [];
|
|
1377
|
-
if (this.scopeJs) {
|
|
1378
|
-
this.sandBox = new SandBox(this);
|
|
1379
|
-
}
|
|
1380
|
-
this.fetchSource = props.fetchSource;
|
|
1381
|
-
}
|
|
1382
|
-
activated(container, callback) {
|
|
1383
|
-
this.isPreLoad = false;
|
|
1384
|
-
this.state = "ACTIVATED" /* ACTIVATED */;
|
|
1385
|
-
if (this.container && container) {
|
|
1386
|
-
if (container instanceof Element) container?.setAttribute(CSS_ATTRIBUTE_KEY, this.name);
|
|
1387
|
-
const fragment = document.createDocumentFragment();
|
|
1388
|
-
for (const node of Array.from(this.container.childNodes)) {
|
|
1389
|
-
fragment.appendChild(node);
|
|
1390
|
-
}
|
|
1391
|
-
container.appendChild(fragment);
|
|
1392
|
-
this.container = container;
|
|
1393
|
-
this.sandBox?.activated();
|
|
1394
|
-
const scriptInfo = this.source?.getScript(this.url);
|
|
1395
|
-
callback?.(this, scriptInfo?.exportInstance);
|
|
1396
|
-
}
|
|
1397
|
-
}
|
|
1398
|
-
deactivated() {
|
|
1399
|
-
this.state = "DEACTIVATED" /* DEACTIVATED */;
|
|
1400
|
-
this.sandBox?.deactivated();
|
|
1401
|
-
}
|
|
1402
|
-
mount(container, callback) {
|
|
1403
|
-
this.isPreLoad = false;
|
|
1404
|
-
this.container = container ?? this.container;
|
|
1405
|
-
this.state = "MOUNTING" /* MOUNTING */;
|
|
1406
|
-
if (this.container instanceof HTMLElement) {
|
|
1407
|
-
this.container?.setAttribute(CSS_ATTRIBUTE_KEY, this.name);
|
|
1408
|
-
}
|
|
1409
|
-
this.container.innerHTML = "";
|
|
1410
|
-
const instanceWrap = document.createElement("div");
|
|
1411
|
-
const wrapId = `${this.name}-wrapper`;
|
|
1412
|
-
instanceWrap.setAttribute("id", wrapId);
|
|
1413
|
-
if (this.source?.styles.size) {
|
|
1414
|
-
executeAppStyles(this, this.container);
|
|
1415
|
-
}
|
|
1416
|
-
this.container.appendChild(instanceWrap);
|
|
1417
|
-
this.sandBox?.activated();
|
|
1418
|
-
execAppScripts(this).finally(() => {
|
|
1419
|
-
this.state = "MOUNTED" /* MOUNTED */;
|
|
1420
|
-
const scriptInfo = this.source?.getScript(this.url);
|
|
1421
|
-
if (typeof scriptInfo?.exportInstance?.render === "function") {
|
|
1422
|
-
scriptInfo.exportInstance.render(instanceWrap, this.data);
|
|
1423
|
-
}
|
|
1424
|
-
callback?.(this, scriptInfo?.exportInstance);
|
|
1425
|
-
});
|
|
1426
|
-
}
|
|
1427
|
-
onError() {
|
|
1428
|
-
this.state = "ERROR" /* ERROR */;
|
|
1429
|
-
}
|
|
1430
|
-
onMount() {
|
|
1431
|
-
if (this.isPreLoad) return;
|
|
1432
|
-
this.state = "LOADED" /* LOADED */;
|
|
1433
|
-
this.mount();
|
|
1434
|
-
}
|
|
1435
|
-
registerRunningApp() {
|
|
1436
|
-
setCurrentRunningApp(this);
|
|
1437
|
-
Promise.resolve().then(() => setCurrentRunningApp(null));
|
|
1438
|
-
}
|
|
1439
|
-
async start() {
|
|
1440
|
-
if (!this.source || ["ERROR" /* ERROR */, "UNSET" /* UNSET */].includes(this.status)) {
|
|
1441
|
-
this.source = new EntrySource(this.url);
|
|
1442
|
-
await this.source.importEntry(this);
|
|
1443
|
-
}
|
|
1444
|
-
}
|
|
1445
|
-
unmount(needDestroy) {
|
|
1446
|
-
this.state = "UNMOUNT" /* UNMOUNT */;
|
|
1447
|
-
this.sandBox?.deactivated();
|
|
1448
|
-
needDestroy && appCache.deleteApp(this.url);
|
|
1449
|
-
this.container.innerHTML = "";
|
|
1450
|
-
this.container = void 0;
|
|
1451
|
-
}
|
|
1452
|
-
set status(v) {
|
|
1453
|
-
this.state = v;
|
|
1454
|
-
}
|
|
1455
|
-
get status() {
|
|
1456
|
-
return this.state;
|
|
1457
|
-
}
|
|
1458
|
-
};
|
|
1459
|
-
|
|
1460
|
-
// src/entry/script.ts
|
|
1461
|
-
var firstGlobalProp;
|
|
1462
|
-
var secondGlobalProp;
|
|
1463
|
-
var lastGlobalProp;
|
|
1464
|
-
var Script = class {
|
|
1465
|
-
async = false;
|
|
1466
|
-
code = "";
|
|
1467
|
-
defer = false;
|
|
1468
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1469
|
-
exportInstance;
|
|
1470
|
-
fromHtml;
|
|
1471
|
-
initial;
|
|
1472
|
-
isModule = false;
|
|
1473
|
-
scoped;
|
|
1474
|
-
url;
|
|
1475
|
-
constructor({ async, code, defer, fromHtml, initial, isModule, url }) {
|
|
1476
|
-
this.code = code;
|
|
1477
|
-
this.async = async;
|
|
1478
|
-
this.defer = defer;
|
|
1479
|
-
this.isModule = isModule;
|
|
1480
|
-
this.url = url;
|
|
1481
|
-
this.scoped = false;
|
|
1482
|
-
this.fromHtml = fromHtml ?? false;
|
|
1483
|
-
this.initial = initial ?? false;
|
|
1484
|
-
}
|
|
1485
|
-
/**
|
|
1486
|
-
* @param app 应用
|
|
1487
|
-
* @param needReplaceScriptElement 是否需要替换script标签
|
|
1488
|
-
* @returns 返回执行后的script标签或注释
|
|
1489
|
-
*/
|
|
1490
|
-
async executeCode(app, needReplaceScriptElement = false) {
|
|
1491
|
-
try {
|
|
1492
|
-
if (!this.code) await this.getCode(app);
|
|
1493
|
-
if (app instanceof MicroInstanceModel) {
|
|
1494
|
-
const globalWindow = app.scopeJs ? app.sandBox?.proxyWindow || window : window;
|
|
1495
|
-
noteGlobalProps(globalWindow);
|
|
1496
|
-
}
|
|
1497
|
-
let scopedCode = this.code;
|
|
1498
|
-
scopedCode = this.transformCode(app);
|
|
1499
|
-
if (app.showSourceCode || this.isModule) {
|
|
1500
|
-
const scriptElement = document.createElement("script");
|
|
1501
|
-
if (scriptElement.__BK_WEWEB_APP_KEY__) {
|
|
1502
|
-
scriptElement.__BK_WEWEB_APP_KEY__ = void 0;
|
|
1503
|
-
}
|
|
1504
|
-
app.registerRunningApp();
|
|
1505
|
-
this.executeSourceScript(scriptElement, scopedCode);
|
|
1506
|
-
if (needReplaceScriptElement) return scriptElement;
|
|
1507
|
-
const needKeepAlive = !!app.keepAlive && !(app.container instanceof ShadowRoot);
|
|
1508
|
-
const container = needKeepAlive ? document.head : app.container;
|
|
1509
|
-
setMarkElement(scriptElement, app, needKeepAlive);
|
|
1510
|
-
container.appendChild(scriptElement);
|
|
1511
|
-
} else {
|
|
1512
|
-
this.executeMemoryScript(app, scopedCode);
|
|
1513
|
-
if (needReplaceScriptElement) return document.createComment("\u3010bk-weweb\u3011dynamic script");
|
|
1514
|
-
}
|
|
1515
|
-
if (app instanceof MicroInstanceModel) {
|
|
1516
|
-
const globalWindow = app.scopeJs ? app.sandBox?.proxyWindow || window : window;
|
|
1517
|
-
const exportProp = getGlobalProp(globalWindow);
|
|
1518
|
-
if (exportProp) {
|
|
1519
|
-
this.exportInstance = globalWindow[exportProp];
|
|
1520
|
-
if (!app.scopeJs) {
|
|
1521
|
-
delete globalWindow[exportProp];
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1525
|
-
} catch (e) {
|
|
1526
|
-
console.error("execute script code error", e);
|
|
1527
|
-
}
|
|
1528
|
-
return;
|
|
1529
|
-
}
|
|
1530
|
-
// 内存脚本执行
|
|
1531
|
-
executeMemoryScript(app, scopedCode) {
|
|
1532
|
-
try {
|
|
1533
|
-
const isScopedLocation = app instanceof MicroAppModel && app.scopeLocation;
|
|
1534
|
-
app.registerRunningApp();
|
|
1535
|
-
new Function("window", "location", "history", scopedCode)(
|
|
1536
|
-
app.sandBox.proxyWindow,
|
|
1537
|
-
isScopedLocation ? app.iframe.contentWindow.location : window.location,
|
|
1538
|
-
isScopedLocation ? app.iframe.contentWindow.history : window.history
|
|
1539
|
-
);
|
|
1540
|
-
} catch (e) {
|
|
1541
|
-
console.error(e);
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
// 脚本标签执行
|
|
1545
|
-
executeSourceScript(scriptElement, scopedCode) {
|
|
1546
|
-
if (this.isModule) {
|
|
1547
|
-
scriptElement.src = `${this.url}?key=${Date.now()}`;
|
|
1548
|
-
scriptElement.setAttribute("type", "module");
|
|
1549
|
-
} else {
|
|
1550
|
-
scriptElement.textContent = scopedCode;
|
|
1551
|
-
}
|
|
1552
|
-
this.url && scriptElement.setAttribute("origin-src", this.url);
|
|
1553
|
-
}
|
|
1554
|
-
// 获取脚本内容
|
|
1555
|
-
async getCode(app) {
|
|
1556
|
-
if (this.code.length || !this.url) {
|
|
1557
|
-
return this.code;
|
|
1558
|
-
}
|
|
1559
|
-
let code = "";
|
|
1560
|
-
if (app?.source?.getScript(this.url)) {
|
|
1561
|
-
code = app.source.getScript(this.url)?.code || "";
|
|
1562
|
-
}
|
|
1563
|
-
if (!code && appCache.getCacheScript(this.url)) {
|
|
1564
|
-
code = appCache.getCacheScript(this.url)?.code || "";
|
|
1565
|
-
}
|
|
1566
|
-
if (!code) {
|
|
1567
|
-
code = await fetchSource(this.url, {}, app).catch((e) => {
|
|
1568
|
-
console.error(`fetch script ${this.url} error`, e);
|
|
1569
|
-
return "";
|
|
1570
|
-
});
|
|
1571
|
-
}
|
|
1572
|
-
code = code.replace(/^"use\sstrict";$/gim, "");
|
|
1573
|
-
this.code = code;
|
|
1574
|
-
return code;
|
|
1575
|
-
}
|
|
1576
|
-
setCode(code) {
|
|
1577
|
-
this.code = code;
|
|
1578
|
-
}
|
|
1579
|
-
// 转换脚本内容
|
|
1580
|
-
transformCode(app) {
|
|
1581
|
-
const sourceMapUrl = this.url ? `//# sourceURL=${this.url}
|
|
1582
|
-
` : "";
|
|
1583
|
-
if (app.sandBox) {
|
|
1584
|
-
if (this.isModule) {
|
|
1585
|
-
return ` with(window.${app.sandBox.windowSymbolKey}){
|
|
1
|
+
var Ae=null;function k(){return Ae}function M(r){Ae=r}var N=new Map,ke=`const { ${["Array","ArrayBuffer","Boolean","constructor","DataView","Date","decodeURI","decodeURIComponent","encodeURI","encodeURIComponent","Error","escape","eval","EvalError","Float32Array","Float64Array","Function","hasOwnProperty","Infinity","Int16Array","Int32Array","Int8Array","isFinite","isNaN","isPrototypeOf","JSON","Map","Math","NaN","Number","Object","parseFloat","parseInt","Promise","propertyIsEnumerable","Proxy","RangeError","ReferenceError","Reflect","RegExp","Set","String","Symbol","SyntaxError","toLocaleString","toString","TypeError","Uint16Array","Uint32Array","Uint8Array","Uint8ClampedArray","undefined","unescape","URIError","valueOf","WeakMap","WeakSet"].join(",")} }= this;`,Z=()=>ke,$e=()=>{let r=Object.getOwnPropertyNames(window);for(let e of r)!N.has(e)&&e.match(/^[A-Z]/)&&typeof window[e]=="function"&&window[e].toString().includes("[native code]")&&N.set(e,!0)};$e();var I="id";var Ee=["System","__cjsWrapper",""],$=["__POWERED_BY_BK_WEWEB__","__BK_WEWEB_APP_KEY__","__BK_WEWEB_DATA__"],ge=["window","self","globalThis"],X=["location","history"],Fe={__bk_pop_manager:!0,__bk_zIndex_manager:!0,i18n:!0},be=Fe;var F=r=>Promise.resolve().then(r);function T(r){return r.startsWith("//")?`${location.protocol}${r}`:r.startsWith("http")?r:`${location.protocol}//${r}`}function h(r,e){if(!r||/^((((ht|f)tps?)|file):)?\/\//.test(r)||/^(data|blob):/.test(r))return r;let{origin:t,pathname:o}=new URL(T(e));return new URL(r,`${t}${o}`.replace(/\.(\w+)$/,"/")).toString()}function x(){return`inline-${W(16)}`}var je=window.requestIdleCallback||(r=>{let e=Date.now();return setTimeout(()=>{r({didTimeout:!1,timeRemaining(){return Math.max(0,50-(Date.now()-e))}})},1)}),yt=window.cancelIdleCallback||(r=>{clearTimeout(r)}),W=(r,e="abcdefghijklmnopqrstuvwxyz0123456789")=>{let t="";for(let o=0;o<r;o++)t+=e[Number.parseInt((Math.random()*e.length).toString(),10)];return t},j=r=>{if(!r||r.match(/\.js$/))return!1;let{pathname:e}=new URL(T(r));return!e.match(/\.js$/)};var Q=["body","html","head"],ee=(r,e)=>{let t={};function o(d,f){let m=document.createElement("div");m.innerHTML=f;let b=Array.from(m.childNodes),B=e.container;switch(d){case"beforebegin":b.forEach(P=>B.host.parentNode?.insertBefore(P,B.host));break;case"afterbegin":b.reverse().forEach(P=>B.insertBefore(P,B.firstChild));break;case"beforeend":b.forEach(P=>B.appendChild(P));break;case"afterend":b.forEach(P=>B.host.parentNode?.insertBefore(P,B.host.nextSibling));break}}let n=new Proxy({},{get(d,f){if(e.container instanceof ShadowRoot){if(f==="insertAdjacentHTML")return o.bind(e.container);let b=e.container[f];if(typeof b=="function")return b.bind(e.container);if(b!==void 0)return b}let m=Reflect.get(r.body,f);return typeof m=="function"?m.bind(r.body):m},set(d,f,m){return e.container instanceof ShadowRoot?(e.container[f]=m,!0):(Reflect.set(r.body,f,m),!0)}});function i(d,f){let m=r.createElement(d,f);return m.__BK_WEWEB_APP_KEY__=e.appCacheKey,m}function c(d){if(d===n)return e.container instanceof ShadowRoot?e.container:r.body;if(Q.includes(d))return e?.container instanceof ShadowRoot?e?.container:r.querySelector.call(this,d);try{return e?.container?.querySelector(d)??null}catch{return null}}function s(d){return Q.includes(d)?e?.container instanceof ShadowRoot?e?.container:r.querySelector(d):e?.container?.querySelectorAll(d)??[]}function u(d){return c.call(r,`#${d}`)}function l(d){return s(`.${d}`)}function p(d){return Q.includes(d)||!e?.showSourceCode&&d.toLocaleLowerCase()==="script"?r.getElementsByTagName(d):s(d)}function S(d){return s(`[name=${d}]`)}return new Proxy(t,{get(d,f){if(f==="createElement")return i.bind(r);if(f==="querySelector")return c.bind(r);if(f==="querySelectorAll")return s.bind(r);if(f==="getElementById")return u.bind(r);if(f==="getElementsByClassName")return l.bind(r);if(f==="getElementsByTagName")return p.bind(r);if(f==="getElementsByName")return S.bind(r);if(f==="body")return n;let m=Reflect.get(r,f);return typeof m=="function"?m.bind(r):m}})};function _e(){let{addEventListener:r,removeEventListener:e}=window.document,{addEventListener:t,removeEventListener:o}=window.document.body,n=new Map;document.addEventListener=function(c,s,u){let l=k();if(l?.keepAlive){let p=n.get(c)||[];n.set(c,[...p,s])}r.call(l?.container instanceof ShadowRoot?l.container:this,c,s,u)},document.body.addEventListener=document.addEventListener,document.removeEventListener=function(c,s,u){let l=k();if(l?.keepAlive){let p=n.get(c)||[];p.length&&p.some(S=>S===s)&&p.splice(p.indexOf(s),1)}e.call(l?.container instanceof ShadowRoot?l.container:this,c,s,u)},document.body.removeEventListener=document.removeEventListener;function i(){k()?.keepAlive&&n.values()&&Array.from(n.entries()).forEach(([s,u])=>{u?.forEach(l=>{document.removeEventListener.call(document,s,l)})}),document.addEventListener=r,document.body.addEventListener=t,document.removeEventListener=e,document.body.removeEventListener=o,n.clear()}return{resetDocumentAndBodyEvent:i}}var Ye=/^class\b/,Je=/^function\b\s[A-Z].*/,te=new WeakMap;function Ve(r){if(r.prototype?.constructor===r&&Object.getOwnPropertyNames(r.prototype).length>1)return!0;if(te.has(r))return te.get(r);let e=Je.test(r.toString())||Ye.test(r.toString());return te.set(r,e),e}var oe=new WeakMap;function re(r,e){if(oe.has(e))return oe.get(e);if(typeof e=="function"&&!Ve(e)){let t=Function.prototype.bind.call(e,r);for(let o in e)t[o]=e[o];if(e.hasOwnProperty("prototype")&&!t.hasOwnProperty("prototype")&&Object.defineProperty(t,"prototype",{enumerable:!1,value:e.prototype,writable:!0}),typeof e.toString=="function"){let o=e.hasOwnProperty("toString")&&!t.hasOwnProperty("toString"),n=t.toString===Function.prototype.toString;if(o||n){let i=Object.getOwnPropertyDescriptor(o?e:Function.prototype,"toString");Object.defineProperty(t,"toString",{...i,...i?.get?null:{value:()=>e.toString()}})}}return oe.set(e,t),t}return e}function Ce(r){let e=new Map,t=[],o=window,{addEventListener:n,clearInterval:i,removeEventListener:c,setInterval:s}=window;r.addEventListener=(l,p,S)=>{e.set(l,[...e.get(l)||[],p]),n.call(o,l,p,S)},r.removeEventListener=(l,p,S)=>{let d=e.get(l);if(d?.length){let f=d.indexOf(p);f>-1&&d.splice(f,1)}c.call(o,l,p,S)},r.setInterval=(l,p,...S)=>{let d=s.call(o,l,p,...S);return t.push(d),d},r.clearInterval=l=>{let p=t.indexOf(l);p>-1&&t.splice(p,1),i.call(o,l)};function u(){e.size&&(e.forEach((l,p)=>{l.forEach(S=>c.call(o,p,S))}),e.clear()),t.length&&t.forEach(l=>{i.call(o,l)})}return{resetWindowFunction:u}}var R=class{constructor(e){this.app=e;let t=new Set,o=window;this.rawWindow=o,this.rawDocument=ee(document,e);let n=Object.create({});n.__BK_WEWEB_APP_KEY__=e.appCacheKey,n.__POWERED_BY_BK_WEWEB__=!0,n.rawDocument=document,n.rawWindow=o,n.__proto__=Window,this.fakeWindow=n;let{resetWindowFunction:i}=Ce(this.fakeWindow);this.resetWindowFunction=i,this.windowSymbolKey=`__${(e.name||e.appCacheKey).replace(/(-|,|:|~|'|")/gim,"_")}_${W(10)}__`,this.proxyWindow=new Proxy(this.fakeWindow,{defineProperty:(c,s,u)=>t.has(s)?Reflect.defineProperty(o,s,u):Reflect.defineProperty(c,s,u),deleteProperty:(c,s)=>Object.hasOwn(c,s)?(this.sameRawWindowKeySet.has(s)&&this.sameRawWindowKeySet.delete(s),this.inRawWindowKeySet.has(s)&&Reflect.deleteProperty(o,s),Reflect.deleteProperty(c,s)):!0,get:(c,s)=>{if(s===Symbol.unscopables||N.has(s))return o[s];if(be[s])return this.fakeWindow[s];if(ge.includes(s))return this.proxyWindow;if(s==="document")return e.registerRunningApp(),this.rawDocument;if(s==="eval")return e.registerRunningApp(),eval;if(X.includes(s)&&this.app instanceof A&&this.app.iframe&&this.app.scopeLocation)return this.app.iframe.contentWindow?.[s];if(s==="hasOwnProperty")return l=>Object.hasOwn(this.fakeWindow,l)||Object.hasOwn(o,l);if(s==="top"||s==="parent")return o===o.parent?this.proxyWindow:Reflect.get(o,s);if(s==="getComputedStyle")return(l,p)=>l instanceof Element?o.getComputedStyle(l,p):o.getComputedStyle(document.body,p);if(Reflect.has(c,s)||$.includes(s))return Reflect.get(c,s);let u=Reflect.get(o,s);return re(o,u)},getOwnPropertyDescriptor:(c,s)=>{if(Object.hasOwn(c,s))return Object.getOwnPropertyDescriptor(c,s);if(Object.hasOwn(o,s)){t.add(s);let u=Object.getOwnPropertyDescriptor(o,s);return u&&!u.configurable&&(u.configurable=!0),u}},has:(c,s)=>N.has(s)||s in c||s in o,ownKeys:c=>Array.from(new Set(Reflect.ownKeys(o).concat(Reflect.ownKeys(c)))),set:(c,s,u)=>{if(this.active){if(X.includes(s)&&this.app instanceof A&&this.app.iframe&&this.app.scopeLocation)return Reflect.set(this.app.iframe.contentWindow,s,u);if(s==="location")Reflect.set(o,s,u);else if(!Object.hasOwn(c,s)&&Object.hasOwn(o,s)&&!$.includes(s)){let l=Object.getOwnPropertyDescriptor(o,s),{configurable:p,enumerable:S,writable:d}=l;d&&(Object.defineProperty(c,s,{configurable:p,enumerable:S,value:u,writable:d}),this.sameRawWindowKeySet.add(s))}else Reflect.set(c,s,u),this.sameRawWindowKeySet.add(s);Ee.includes(s)&&!Reflect.has(o,s)&&!$.includes(s)&&(Reflect.set(o,s,u),this.inRawWindowKeySet.add(s))}return!0}}),o[this.windowSymbolKey]=this.proxyWindow}active=!1;inRawWindowKeySet=new Set;resetDocumentAndBodyEvent;resetWindowFunction;sameRawWindowKeySet=new Set;fakeWindow;proxyDocument;proxyWindow;rawDocument;rawWindow;windowSymbolKey;activated(e){if(!this.active){this.active=!0,this.rawDocument=ee(document,this.app),this.fakeWindow.__BK_WEWEB_DATA__=e??{};let{resetDocumentAndBodyEvent:t}=_e();this.resetDocumentAndBodyEvent=t}}deactivated(){if(this.active){this.active=!1,this.resetWindowFunction();for(let e of this.inRawWindowKeySet)Reflect.deleteProperty(window,e);this.inRawWindowKeySet.clear(),this.resetDocumentAndBodyEvent?.()}}};var Ge=["currentTarget","srcElement","target"];function Me(r,e="custom"){return Object.defineProperties(new CustomEvent(e),Ge.reduce((t,o)=>(t[o]={get(){return r}},t),{}))}function _(r){let e=Me(r,"load");if(typeof r.onload=="function"){r.onload(e);return}r.dispatchEvent(e)}function K(r){let e=Me(r,"error");if(typeof r.onerror=="function"){r.onerror(e);return}r.dispatchEvent(e)}var{appendChild:Te}=HTMLBodyElement.prototype;function xe(r,e,t){if(e instanceof HTMLStyleElement){if(e.hasAttribute("exclude"))return document.createComment("\u3010bk-weweb\u3011style with exclude attribute is ignored");if(e.textContent&&!(t.container instanceof ShadowRoot)&&a.getBaseAppStyle(e.textContent))return document.createComment("\u3010bk-weweb\u3011style is effective in base app");if(!e.hasAttribute("ignore")){let o=new w({code:e.textContent||"",fromHtml:!1,url:""});t.source?.setStyle(x(),o),o.scopedStyleCSS(t,e)}return e}if(e instanceof HTMLLinkElement){let o=t.source?.collectLink(e,r,!0);return o?(o.style&&o.style.scopedLinkCSS(t,e),o.replace!==e?o.replace:e):e}if(e instanceof HTMLScriptElement){let o=t.source.collectScript(e,r,!0);if(!o)return e;if(o.script&&o.script.executeCode(t),o.replace!==e)return o.replace;if(t.scopeJs&&!e.getAttribute("src")&&!e.textContent){let n=new MutationObserver(()=>{if(e.getAttribute("src")){n.disconnect();let i=t.source.collectScript(e,r,!0);if(i?.replace&&Te.call(t.container,i.replace),j(e.getAttribute("src"))){t.container?.append(e);return}i?.script&&i.script.executeCode(t),e.remove()}else if(e.textContent){n.disconnect();let i=new y({async:!1,code:e.textContent,defer:e.type==="module",fromHtml:!1,isModule:e.type==="module"});t.source.scripts.set(x(),i);try{i.executeCode(t)}catch(c){console.error(c)}finally{!i.isModule&&_(e),e.remove()}}});return n.observe(e,{attributeFilter:["src"],childList:!0,subtree:!1}),document.createComment("\u3010bk-weweb\u3011dynamic script or module")}return e}return e}function D(r){return r instanceof HTMLScriptElement||r instanceof HTMLStyleElement||r instanceof HTMLLinkElement}function ne(r,e,t){if(e.__BK_WEWEB_APP_KEY__){let o=a.getApp(e.__BK_WEWEB_APP_KEY__);if(o?.container){let n=xe(r,e,o),i=D(e)&&!!o.keepAlive&&!(o.container instanceof ShadowRoot),c=i?document.head:o?.container;return E(n,o,i),t.call(c,n)}}return t.call(r,e)}function ie(r,e,t,o){if(e.__BK_WEWEB_APP_KEY__){let n=a.getApp(e.__BK_WEWEB_APP_KEY__);if(n?.container){let i=D(e)&&n.keepAlive&&!(n.container instanceof ShadowRoot),c=i?document.head:n?.container,s=xe(r,e,n);return i&&E(s,n,i),t&&!c.contains(t)?Te.call(c,s):o.call(c,s,t)}}return o.call(r,e,t)}function E(r,e,t){return t&&e&&(r.__KEEP_ALIVE__=e.appCacheKey,r.setAttribute("data-from",e.name),r.setAttribute("data-keep-alive","true")),r.setAttribute?.("powered-by","bk-weweb"),r}var{setAttribute:se}=Element.prototype,{append:qe,appendChild:ce,insertBefore:ze,removeChild:ae}=HTMLBodyElement.prototype,{appendChild:le,insertBefore:de,removeChild:Ze}=HTMLHeadElement.prototype,Le=document.head,ue=!1;function ve(){ue||(ue=!0,Element.prototype.setAttribute=function(r,e){let t=this.tagName.toLocaleUpperCase();(["src","srcset"].includes(r)&&["IMG","SCRIPT"].includes(t)||r==="href"&&["LINK"].includes(t))&&this.__BK_WEWEB_APP_KEY__&&a.getApp(this.__BK_WEWEB_APP_KEY__||"")?se.call(this,r,h(e,a.getApp(this.__BK_WEWEB_APP_KEY__).url)):se.call(this,r,e)},HTMLBodyElement.prototype.appendChild=function(e){return e.__KEEP_ALIVE__&&D(e)?le.call(Le,e):ne(this,e,ce)},HTMLBodyElement.prototype.append=function(...r){r.forEach(e=>{if(e.__KEEP_ALIVE__&&D(e))return le.call(Le,e);ne(this,e,ce)})},HTMLHeadElement.prototype.appendChild=HTMLBodyElement.prototype.appendChild,HTMLHeadElement.prototype.insertBefore=function(r,e){return ie(this,r,e,de)},HTMLBodyElement.prototype.insertBefore=function(r,e){return ie(this,r,e,de)},HTMLBodyElement.prototype.removeChild=function(e){let t=a.getApp(e.__BK_WEWEB_APP_KEY__);return t?.container?.contains(e)?ae.call(t.container,e):this.contains(e)?ae.call(this,e):e},HTMLHeadElement.prototype.removeChild=HTMLBodyElement.prototype.removeChild)}function Y(){M(null),Element.prototype.setAttribute=se,HTMLBodyElement.prototype.appendChild=ce,HTMLBodyElement.prototype.append=qe,HTMLBodyElement.prototype.removeChild=ae,HTMLBodyElement.prototype.insertBefore=ze,HTMLHeadElement.prototype.appendChild=le,HTMLHeadElement.prototype.insertBefore=de,HTMLHeadElement.prototype.removeChild=Ze,ue=!1}function L(){ve()}function pe(r,e,t){let o=a.getApp(r);o?.status==="DEACTIVATED"&&o.keepAlive?F(()=>{L(),o.activated(e,t)}):o&&F(()=>{L(),o.mount(e,t)})}function Be(r){let e=a.getApp(r);e&&["ACTIVATED","MOUNTED"].includes(e.status)&&(e.keepAlive?e.deactivated():e.unmount()),a.hasActiveApp||Y()}async function me(r){return L(),r.mode==="js"?await Ie(r):await Pe(r)}async function Pe(r){L();let e=a.getApp(r.id);return e||(e=new A(r),a.setApp(e)),await e.start(),e}function Ie(r){return L(),new Promise(e=>{let t=a.getApp(r.id);if(!t)t=new C(r),a.setApp(t),t.start().then(()=>e(t));else if(["MOUNTING","UNSET"].includes(t.status)){let o=setInterval(()=>{["ERROR","MOUNTED"].includes(t.status)&&(e(t),clearInterval(o))},300)}else e(t)})}function We(r){let e=a.getApp(r);e&&e.status!=="UNMOUNT"&&e.unmount(),a.hasActiveApp||Y()}var H=class extends HTMLElement{appUrl="";connected=!1;static get observedAttributes(){return["url"]}getBooleanAttr(e){return this.hasAttribute(e)||this.hasAttribute(e.replace(/([A-Z])/g,"-$1").toLocaleLowerCase())?this.getAttribute(e)!=="false":void 0}async handleAttributeChanged(){if(!this.appKey)return;this.getBooleanAttr("setShodowDom")&&this.attachShadow({mode:"open"});let e=a.getApp(this.appKey);if(e&&e.url===this.appUrl&&(e.isPreLoad||e.status==="UNMOUNT")){pe(this.appKey,this.shadowRoot??this);return}await me(this.appProps)}attributeChangedCallback(e,t,o){if(e!=="url"||this[e]===o||!this.connected)return;this.appUrl=o;let n=a.getApp(this.appKey);(this.connected||n)&&this.handleAttributeChanged()}async connectedCallback(){this.getBooleanAttr("setShodowDom")&&!this.shadowRoot&&this.attachShadow({delegatesFocus:!1,mode:"open"}),await me(this.appProps),pe(this.appKey,this.shadowRoot??this),this.connected=!0}disconnectedCallback(){this.connected=!1,this.appProps.keepAlive?Be(this.appKey):We(this.appKey)}get appData(){if(this.hasAttribute("data"))try{return JSON.parse(this.getAttribute("data"))}catch{}return{}}get appKey(){return this.getAttribute("id")||this.getAttribute("url")}get appProps(){return this.getAttribute("mode")==="js"?{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr("keepAlive"),mode:"js",scopeCss:this.getBooleanAttr("scopeCss")&&!this.getBooleanAttr("setShodowDom"),scopeJs:this.getBooleanAttr("scopeJs"),showSourceCode:this.getBooleanAttr("showSourceCode"),url:this.getAttribute("url")}:{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr("keepAlive"),mode:"app",scopeCss:!this.getBooleanAttr("setShodowDom"),scopeJs:!this.getBooleanAttr("scopeJs"),scopeLocation:this.getBooleanAttr("scopeLocation"),showSourceCode:this.getBooleanAttr("showSourceCode"),url:this.getAttribute("url")}}};async function Re(r){let e=[];if(typeof r=="function"?e=await r():e=r||[],!e.length)return{};let t=new Map,o=new Map;return e.forEach(n=>{try{let i=new URL(n);if(i.pathname.match(/\.js$/)){let c=a.getCacheScript(n);t.set(n,new y({async:!1,code:c?.code||"",defer:!1,fromHtml:!1,initial:!0,isModule:!1,url:n}))}else if(i.pathname.match(/\.css$/)){let c=a.getCacheStyle(n);o.set(n,new w({code:c?.code||"",fromHtml:!0,initial:!0,prefetch:!1,preload:!1,url:n}))}}catch{console.error(`\u3010bk-weweb\u3011: ${n} is invalid URL`)}}),{collectScript:t,collectStyle:o}}var J="bk-weweb",he=class{fetchSource;webcomponentTag=J;constructor(){window.customElements.get(J)||window.customElements.define(J,H)}setWebComponentTag(){window.customElements.get(this.webcomponentTag)||window.customElements.define(this.webcomponentTag,H)}start(e){e?.collectBaseSource&&Oe(),typeof e?.fetchSource=="function"&&(this.fetchSource=e.fetchSource),this.webcomponentTag=e?.webcomponentTag||J,this.setWebComponentTag()}},Xe=new he,ye=Xe;function g(r,e={},t){return typeof t?.fetchSource=="function"?t.fetchSource(r,e).catch(()=>""):ye.fetchSource?ye.fetchSource(r,e):window.fetch(r,e).then(o=>o.text())}var w=class{code="";fromHtml;initial;prefetch=!1;preload=!1;scoped;scopedCode="";url;constructor({code:e,fromHtml:t,initial:o,prefetch:n,preload:i,url:c}){this.scoped=!1,this.code=e,this.prefetch=n??!1,this.preload=i??!1,this.url=c,this.fromHtml=t,this.initial=o??!1}commonScoped(e,t){if(t.scopeCss&&!(t.container instanceof ShadowRoot)){let o=new CSSStyleSheet({disabled:!0});o.replaceSync(e.textContent||this.code);let n=Array.from(o?.cssRules??[]),i=`#${t.name}`,c=this.scopeRule(n,i),s=this.resetUrlHost(c,t.url,this.url);e.textContent=s,this.scopedCode=s}else{let o=this.resetUrlHost(e.textContent||this.code||"",t.url,this.url);if(o&&t.container instanceof ShadowRoot){let n="";o.match(/@font-face\s*\{[^}]+\}/g)?.forEach(c=>{n+=`${c}
|
|
2
|
+
`});let i=t.sandBox?.rawDocument;if(i&&n){let c=i.createElement("style");c.setAttribute("type","text/css"),c.setAttribute("powered-by","bk-weweb"),c.textContent=n,i?.head?.append(c)}}e.textContent=o}this.scoped=!0}createStyleElement(){let e=document.createElement("style");return e.__BK_WEWEB_APP_KEY__&&(e.__BK_WEWEB_APP_KEY__=void 0),e}async executeCode(e){e.registerRunningApp();let t=this.createStyleElement();t.setAttribute("type","text/css"),t.textContent=this.code;try{this.code||await this.getCode(e),t=this.scopedStyleCSS(e,t),this.scoped=!0}catch(o){console.error("scoped style error",o)}return t}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";return e?.source?.styles?.has(this.url)&&(t=e.source.styles.get(this.url)?.code||""),!t&&a.getCacheStyle(this.url)&&(t=a.getCacheStyle(this.url)?.code||""),t||(t=await g(this.url,{},e).catch(()=>"")),this.code=t,t}linkedBaseStyle(e,t){return!(t.container instanceof ShadowRoot)&&e.textContent&&a.getBaseAppStyle(e.textContent)?(e.textContent="",e.innerHTML="",e.setAttribute("linked-from-base","true"),!0):!1}resetPackRule(e,t,o){let n=this.scopeRule(Array.from(e.cssRules),t);return`@${o} ${e.conditionText} {${n}}`}resetUrlHost(e,t,o){let n=t;return e.replace(/url\(["']?([^)"']+)["']?\)/gm,(i,c)=>{if(/^(data|blob):/.test(c)||/^(https?:)?\/\//.test(c))return i;if(/^((\.\.?\/)|[^/])/.test(c)&&o){let s=o.split("/");s.pop(),n=T(`${s.join("/")}/`)}return`url("${h(c,n)}")`})}scopeRule(e,t){let o="";for(let n of e)switch(n.type){case 1:o+=this.scopeStyleRule(n,t);break;case 4:o+=this.resetPackRule(n,t,"media");break;case 12:o+=this.resetPackRule(n,t,"supports");break;default:o+=n.cssText;break}return o.replace(/^\s+/,"")}scopeStyleRule(e,t){let{cssText:o,selectorText:n}=e;if(/^((html[\s>~,]+body)|(html|body|:root))$/.test(n))return o.replace(/^((html[\s>~,]+body)|(html|body|:root))/,t);if(n==="*")return o.replace("*",`${t} *`);let i=/(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;return o.replace(/^[\s\S]+{/,c=>c.replace(/(^|,)([^,]+)/g,(s,u,l)=>i.test(l)?s.replace(i,t):`${u} ${t} ${l.replace(/^\s*/,"")}`))}scopedLinkCSS(e,t){let o=this.createStyleElement();o.setAttribute("type","text/css");let n=!!e.keepAlive&&!(e.container instanceof ShadowRoot);E(o,e,n);let i=n?document.head:e.container;try{if(this.code)this.commonScoped(o,e),i?.prepend(o),t&&_(t);else if(t.getAttribute("href"))this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then(()=>{this.scopedStyleCSS(e,o),t.remove(),i?.prepend(o),t&&_(t),this.scoped=!0});else{let c=new MutationObserver(()=>{t.href&&(c.disconnect(),this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then(()=>{this.scopedStyleCSS(e,o),t.remove(),i?.prepend(o),t&&_(t),this.scoped=!0}))});c.observe(t,{attributeFilter:["href"],childList:!1,subtree:!1})}}catch{t&&K(t)}return o}scopedStyleCSS(e,t){let o=!!e.keepAlive&&!(e.container instanceof ShadowRoot);if(E(t,e,o),this.code||t.textContent){if(t.textContent&&(t.textContent="",t.innerHTML=""),this.linkedBaseStyle(t,e))return t;this.commonScoped(t,e)}else{let n=new MutationObserver(()=>{(t.textContent||t.sheet?.cssRules?.length)&&(n.disconnect(),this.linkedBaseStyle(t,e)||this.commonScoped(t,e))});n.observe(t,{attributes:!1,characterData:!0,childList:!0,subtree:!0})}return this.url&&t.setAttribute("origin-src",this.url),t}};async function V(r,e){let t=Array.from(r.source.styles.values()),o=[];for(let n of t)o.push(n.executeCode(r));await Promise.all(o).then(n=>{let i=e||r.container;r.keepAlive&&!(i instanceof ShadowRoot)?document.head.append(...n):i?.append(...n)})}var C=class{state="UNSET";appCacheKey;container;data;initSource;isPreLoad=!1;keepAlive;name;sandBox;scopeCss=!0;scopeJs=!1;showSourceCode=!0;source;url;fetchSource;constructor(e){this.name=e.id!==e.url?e.id:W(5),this.appCacheKey=e.id||this.name,this.url=e.url,this.container=e.container??void 0,this.scopeJs=e.scopeJs??!0,this.showSourceCode=e.showSourceCode??!0,this.scopeCss=e.scopeCss??!0,this.keepAlive=e.keepAlive??!1,this.data=e.data??{},this.initSource=e.initSource??[],this.scopeJs&&(this.sandBox=new R(this)),this.fetchSource=e.fetchSource}activated(e,t){if(this.isPreLoad=!1,this.state="ACTIVATED",this.container&&e){e instanceof Element&&e?.setAttribute(I,this.name);let o=document.createDocumentFragment();for(let i of Array.from(this.container.childNodes))o.appendChild(i);e.appendChild(o),this.container=e,this.sandBox?.activated();let n=this.source?.getScript(this.url);t?.(this,n?.exportInstance)}}deactivated(){this.state="DEACTIVATED",this.sandBox?.deactivated()}mount(e,t){this.isPreLoad=!1,this.container=e??this.container,this.state="MOUNTING",this.container instanceof HTMLElement&&this.container?.setAttribute(I,this.name),this.container.innerHTML="";let o=document.createElement("div"),n=`${this.name}-wrapper`;o.setAttribute("id",n),this.source?.styles.size&&V(this,this.container),this.container.appendChild(o),this.sandBox?.activated(),G(this).finally(()=>{this.state="MOUNTED";let i=this.source?.getScript(this.url);typeof i?.exportInstance?.render=="function"&&i.exportInstance.render(o,this.data),t?.(this,i?.exportInstance)})}onError(){this.state="ERROR"}onMount(){this.isPreLoad||(this.state="LOADED",this.mount())}registerRunningApp(){M(this),Promise.resolve().then(()=>M(null))}async start(){(!this.source||["ERROR","UNSET"].includes(this.status))&&(this.source=new v(this.url),await this.source.importEntry(this))}unmount(e){this.state="UNMOUNT",this.sandBox?.deactivated(),e&&a.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}set status(e){this.state=e}get status(){return this.state}};var q,U,z,y=class{async=!1;code="";defer=!1;exportInstance;fromHtml;initial;isModule=!1;scoped;url;constructor({async:e,code:t,defer:o,fromHtml:n,initial:i,isModule:c,url:s}){this.code=t,this.async=e,this.defer=o,this.isModule=c,this.url=s,this.scoped=!1,this.fromHtml=n??!1,this.initial=i??!1}async executeCode(e,t=!1){try{if(this.code||await this.getCode(e),e instanceof C){let n=e.scopeJs&&e.sandBox?.proxyWindow||window;et(n)}let o=this.code;if(o=this.transformCode(e),e.showSourceCode||this.isModule){let n=document.createElement("script");if(n.__BK_WEWEB_APP_KEY__&&(n.__BK_WEWEB_APP_KEY__=void 0),e.registerRunningApp(),this.executeSourceScript(n,o),t)return n;let i=!!e.keepAlive&&!(e.container instanceof ShadowRoot),c=i?document.head:e.container;E(n,e,i),c.appendChild(n)}else if(this.executeMemoryScript(e,o),t)return document.createComment("\u3010bk-weweb\u3011dynamic script");if(e instanceof C){let n=e.scopeJs&&e.sandBox?.proxyWindow||window,i=Qe(n);i&&(this.exportInstance=n[i],e.scopeJs||delete n[i])}}catch(o){console.error("execute script code error",o)}}executeMemoryScript(e,t){try{let o=e instanceof A&&e.scopeLocation;e.registerRunningApp(),new Function("window","location","history",t)(e.sandBox.proxyWindow,o?e.iframe.contentWindow.location:window.location,o?e.iframe.contentWindow.history:window.history)}catch(o){console.error(o)}}executeSourceScript(e,t){this.isModule?(e.src=`${this.url}?key=${Date.now()}`,e.setAttribute("type","module")):e.textContent=t,this.url&&e.setAttribute("origin-src",this.url)}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";return e?.source?.getScript(this.url)&&(t=e.source.getScript(this.url)?.code||""),!t&&a.getCacheScript(this.url)&&(t=a.getCacheScript(this.url)?.code||""),t||(t=await g(this.url,{},e).catch(o=>(console.error(`fetch script ${this.url} error`,o),""))),t=t.replace(/^"use\sstrict";$/gim,""),this.code=t,t}setCode(e){this.code=e}transformCode(e){let t=this.url?`//# sourceURL=${this.url}
|
|
3
|
+
`:"";return e.sandBox?this.isModule?` with(window.${e.sandBox.windowSymbolKey}){
|
|
1586
4
|
;${this.code}
|
|
1587
5
|
|
|
1588
|
-
${
|
|
1589
|
-
}
|
|
1590
|
-
}
|
|
1591
|
-
if (app.showSourceCode) {
|
|
1592
|
-
return `;(function(window, self, globalThis){
|
|
6
|
+
${t}
|
|
7
|
+
}`:e.showSourceCode?`;(function(window, self, globalThis){
|
|
1593
8
|
with(window){
|
|
1594
|
-
${
|
|
9
|
+
${Z()}
|
|
1595
10
|
|
|
1596
11
|
${this.code}
|
|
1597
12
|
|
|
1598
|
-
${
|
|
13
|
+
${t}
|
|
1599
14
|
}
|
|
1600
|
-
}).call(window.${
|
|
1601
|
-
window.${
|
|
1602
|
-
}
|
|
1603
|
-
return `
|
|
15
|
+
}).call(window.${e.sandBox.windowSymbolKey},
|
|
16
|
+
window.${e.sandBox.windowSymbolKey}, window.${e.sandBox.windowSymbolKey}, window.${e.sandBox.windowSymbolKey});`:`
|
|
1604
17
|
with(window) {
|
|
1605
18
|
try {
|
|
1606
|
-
${
|
|
19
|
+
${Z()}
|
|
1607
20
|
|
|
1608
21
|
${this.code}
|
|
1609
22
|
|
|
1610
|
-
${
|
|
23
|
+
${t}
|
|
1611
24
|
}
|
|
1612
25
|
catch(e) {
|
|
1613
26
|
console.error(e)
|
|
1614
27
|
}
|
|
1615
28
|
}
|
|
1616
|
-
`;
|
|
1617
|
-
}
|
|
1618
|
-
return this.code;
|
|
1619
|
-
}
|
|
1620
|
-
};
|
|
1621
|
-
function shouldSkipProperty(global, p) {
|
|
1622
|
-
return (
|
|
1623
|
-
// biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
|
|
1624
|
-
!global.hasOwnProperty(p) || !Number.isNaN(p) && p < global.length || typeof navigator !== "undefined" && navigator.userAgent.indexOf("Trident") !== -1 && global[p] && typeof window !== "undefined" && global[p].parent === window
|
|
1625
|
-
);
|
|
1626
|
-
}
|
|
1627
|
-
function getGlobalProp(global, useFirstGlobalProp) {
|
|
1628
|
-
let cnt = 0;
|
|
1629
|
-
let foundLastProp;
|
|
1630
|
-
let result;
|
|
1631
|
-
for (const p in global) {
|
|
1632
|
-
if (shouldSkipProperty(global, p)) continue;
|
|
1633
|
-
if (cnt === 0 && p !== firstGlobalProp || cnt === 1 && p !== secondGlobalProp) return p;
|
|
1634
|
-
if (foundLastProp) {
|
|
1635
|
-
lastGlobalProp = p;
|
|
1636
|
-
result = useFirstGlobalProp && result || p;
|
|
1637
|
-
} else {
|
|
1638
|
-
foundLastProp = p === lastGlobalProp;
|
|
1639
|
-
}
|
|
1640
|
-
cnt += 1;
|
|
1641
|
-
}
|
|
1642
|
-
return result;
|
|
1643
|
-
}
|
|
1644
|
-
function noteGlobalProps(global) {
|
|
1645
|
-
secondGlobalProp = void 0;
|
|
1646
|
-
firstGlobalProp = secondGlobalProp;
|
|
1647
|
-
for (const p in global) {
|
|
1648
|
-
if (shouldSkipProperty(global, p)) continue;
|
|
1649
|
-
if (!firstGlobalProp) firstGlobalProp = p;
|
|
1650
|
-
else if (!secondGlobalProp) secondGlobalProp = p;
|
|
1651
|
-
lastGlobalProp = p;
|
|
1652
|
-
}
|
|
1653
|
-
return lastGlobalProp;
|
|
1654
|
-
}
|
|
1655
|
-
async function execAppScripts(app) {
|
|
1656
|
-
const appScriptList = Array.from(app.source.scripts.values()).filter((script) => script.fromHtml || script.initial);
|
|
1657
|
-
const commonList = appScriptList.filter((script) => !script.async && !script.defer || script.isModule);
|
|
1658
|
-
await Promise.all(commonList.map((script) => script.getCode(app)));
|
|
1659
|
-
await Promise.all(commonList.map((script) => script.executeCode(app)));
|
|
1660
|
-
const deferScriptList = [];
|
|
1661
|
-
const asyncScriptList = [];
|
|
1662
|
-
for (const script of appScriptList) {
|
|
1663
|
-
if (script.defer || script.async) {
|
|
1664
|
-
if (!script.code && script.defer) {
|
|
1665
|
-
deferScriptList.push(script.executeCode(app));
|
|
1666
|
-
} else asyncScriptList.push(script.executeCode(app));
|
|
1667
|
-
}
|
|
1668
|
-
}
|
|
1669
|
-
await Promise.all([...asyncScriptList, ...deferScriptList]).catch((e) => {
|
|
1670
|
-
console.error(e);
|
|
1671
|
-
});
|
|
1672
|
-
}
|
|
1673
|
-
|
|
1674
|
-
// src/mode/app.ts
|
|
1675
|
-
var BLANK_ORIGN = "about:blank";
|
|
1676
|
-
var MicroAppModel = class {
|
|
1677
|
-
state = "UNSET" /* UNSET */;
|
|
1678
|
-
// 状态
|
|
1679
|
-
container;
|
|
1680
|
-
// 容器
|
|
1681
|
-
data;
|
|
1682
|
-
// 数据
|
|
1683
|
-
iframe = null;
|
|
1684
|
-
// scoped iframe
|
|
1685
|
-
initSource;
|
|
1686
|
-
// 初始资源
|
|
1687
|
-
isModuleApp = false;
|
|
1688
|
-
// 是否预加载
|
|
1689
|
-
isPreLoad = false;
|
|
1690
|
-
// 是否缓存
|
|
1691
|
-
keepAlive;
|
|
1692
|
-
mode = "app" /* APP */;
|
|
1693
|
-
// 名称
|
|
1694
|
-
name;
|
|
1695
|
-
// 沙箱
|
|
1696
|
-
sandBox;
|
|
1697
|
-
// 是否隔离样式
|
|
1698
|
-
scopeCss;
|
|
1699
|
-
// 是否隔离js
|
|
1700
|
-
scopeJs;
|
|
1701
|
-
// 是否隔离location
|
|
1702
|
-
scopeLocation;
|
|
1703
|
-
// 是否显示源码
|
|
1704
|
-
showSourceCode;
|
|
1705
|
-
// 入口资源
|
|
1706
|
-
source;
|
|
1707
|
-
// url
|
|
1708
|
-
url;
|
|
1709
|
-
// 是否是esm应用
|
|
1710
|
-
constructor(props) {
|
|
1711
|
-
this.name = props.id !== props.url ? props.id : random(5);
|
|
1712
|
-
this.mode = props.mode ?? "app" /* APP */;
|
|
1713
|
-
this.container = props.container ?? void 0;
|
|
1714
|
-
this.showSourceCode = props.showSourceCode ?? false;
|
|
1715
|
-
this.url = props.url;
|
|
1716
|
-
this.data = props.data || {};
|
|
1717
|
-
this.scopeJs = props.scopeJs ?? true;
|
|
1718
|
-
this.scopeCss = props.scopeCss ?? true;
|
|
1719
|
-
this.scopeLocation = props.scopeLocation ?? false;
|
|
1720
|
-
this.isPreLoad = props.isPreLoad ?? false;
|
|
1721
|
-
this.keepAlive = props.keepAlive ?? false;
|
|
1722
|
-
this.initSource = props.initSource ?? [];
|
|
1723
|
-
if (this.scopeJs) {
|
|
1724
|
-
this.sandBox = new SandBox(this);
|
|
1725
|
-
}
|
|
1726
|
-
if (this.container instanceof HTMLElement) {
|
|
1727
|
-
this.container.setAttribute(CSS_ATTRIBUTE_KEY, this.name);
|
|
1728
|
-
}
|
|
1729
|
-
}
|
|
1730
|
-
// 激活
|
|
1731
|
-
activated(container, callback) {
|
|
1732
|
-
this.isPreLoad = false;
|
|
1733
|
-
this.state = "ACTIVATED" /* ACTIVATED */;
|
|
1734
|
-
const app = this;
|
|
1735
|
-
if (container && this.container) {
|
|
1736
|
-
if (container instanceof Element) container.setAttribute(CSS_ATTRIBUTE_KEY, this.name);
|
|
1737
|
-
const fragment = document.createDocumentFragment();
|
|
1738
|
-
const list = Array.from(this.container.childNodes);
|
|
1739
|
-
for (const node of list) {
|
|
1740
|
-
node.__BK_WEWEB_APP_KEY__ = this.appCacheKey;
|
|
1741
|
-
Object.defineProperties(node, {
|
|
1742
|
-
ownerDocument: {
|
|
1743
|
-
get() {
|
|
1744
|
-
return app.sandBox?.rawDocument;
|
|
1745
|
-
}
|
|
1746
|
-
}
|
|
1747
|
-
});
|
|
1748
|
-
fragment.appendChild(node);
|
|
1749
|
-
}
|
|
1750
|
-
container.innerHTML = "";
|
|
1751
|
-
container.appendChild(fragment);
|
|
1752
|
-
this.container = container;
|
|
1753
|
-
this.initShadowRootContainer();
|
|
1754
|
-
this.sandBox?.activated(this.data);
|
|
1755
|
-
callback?.(this);
|
|
1756
|
-
}
|
|
1757
|
-
}
|
|
1758
|
-
// 创建隔离iframe
|
|
1759
|
-
createIframe() {
|
|
1760
|
-
return new Promise((resolve) => {
|
|
1761
|
-
const iframe = document.createElement("iframe");
|
|
1762
|
-
const url = new URL(addUrlProtocol(this.url));
|
|
1763
|
-
const isChrome = navigator.userAgent.indexOf("Chrome") > -1;
|
|
1764
|
-
iframe.setAttribute(
|
|
1765
|
-
"src",
|
|
1766
|
-
`${isChrome ? BLANK_ORIGN : location.origin}${url.pathname || "/"}${url.search}${url.hash}`
|
|
1767
|
-
);
|
|
1768
|
-
iframe.style.cssText = "display: none;";
|
|
1769
|
-
document.body.appendChild(iframe);
|
|
1770
|
-
if (isChrome) {
|
|
1771
|
-
setTimeout(() => resolve(iframe), 0);
|
|
1772
|
-
} else {
|
|
1773
|
-
const interval = setInterval(() => {
|
|
1774
|
-
if (iframe.contentWindow && iframe.contentWindow.location.href !== BLANK_ORIGN) {
|
|
1775
|
-
iframe.contentWindow.stop();
|
|
1776
|
-
iframe.contentDocument.body.parentElement.innerHTML = "<head></head><body></body>";
|
|
1777
|
-
clearInterval(interval);
|
|
1778
|
-
resolve(iframe);
|
|
1779
|
-
}
|
|
1780
|
-
}, 0);
|
|
1781
|
-
}
|
|
1782
|
-
});
|
|
1783
|
-
}
|
|
1784
|
-
deactivated() {
|
|
1785
|
-
this.state = "DEACTIVATED" /* DEACTIVATED */;
|
|
1786
|
-
this.sandBox?.deactivated();
|
|
1787
|
-
}
|
|
1788
|
-
initShadowRootContainer() {
|
|
1789
|
-
if (this.container instanceof ShadowRoot) {
|
|
1790
|
-
Object.defineProperties(this.container, {
|
|
1791
|
-
getBoundingClientRect: {
|
|
1792
|
-
get() {
|
|
1793
|
-
return this.host.getBoundingClientRect;
|
|
1794
|
-
}
|
|
1795
|
-
}
|
|
1796
|
-
});
|
|
1797
|
-
}
|
|
1798
|
-
}
|
|
1799
|
-
mount(container, callback) {
|
|
1800
|
-
this.isPreLoad = false;
|
|
1801
|
-
this.container = container ?? this.container;
|
|
1802
|
-
this.initShadowRootContainer();
|
|
1803
|
-
this.state = "MOUNTING" /* MOUNTING */;
|
|
1804
|
-
const app = this;
|
|
1805
|
-
if (this.container) {
|
|
1806
|
-
if (this.container instanceof Element) this.container.setAttribute(CSS_ATTRIBUTE_KEY, this.name);
|
|
1807
|
-
const clonedNode = this.source.html.cloneNode(true);
|
|
1808
|
-
const fragment = document.createDocumentFragment();
|
|
1809
|
-
for (const node of Array.from(clonedNode.childNodes)) {
|
|
1810
|
-
node.__BK_WEWEB_APP_KEY__ = this.appCacheKey;
|
|
1811
|
-
Object.defineProperties(node, {
|
|
1812
|
-
ownerDocument: {
|
|
1813
|
-
get() {
|
|
1814
|
-
return app.sandBox?.rawDocument;
|
|
1815
|
-
}
|
|
1816
|
-
}
|
|
1817
|
-
});
|
|
1818
|
-
fragment.appendChild(node);
|
|
1819
|
-
}
|
|
1820
|
-
this.container.innerHTML = "";
|
|
1821
|
-
this.container.appendChild(fragment);
|
|
1822
|
-
this.sandBox?.activated(this.data);
|
|
1823
|
-
execAppScripts(this).finally(() => {
|
|
1824
|
-
this.state = "MOUNTED" /* MOUNTED */;
|
|
1825
|
-
callback?.(this);
|
|
1826
|
-
});
|
|
1827
|
-
}
|
|
1828
|
-
}
|
|
1829
|
-
onError() {
|
|
1830
|
-
this.state = "ERROR" /* ERROR */;
|
|
1831
|
-
}
|
|
1832
|
-
onMount() {
|
|
1833
|
-
if (this.isPreLoad) return;
|
|
1834
|
-
this.state = "LOADED" /* LOADED */;
|
|
1835
|
-
this.mount();
|
|
1836
|
-
}
|
|
1837
|
-
registerRunningApp() {
|
|
1838
|
-
setCurrentRunningApp(this);
|
|
1839
|
-
Promise.resolve().then(() => setCurrentRunningApp(null));
|
|
1840
|
-
}
|
|
1841
|
-
async start() {
|
|
1842
|
-
if (!this.source || ["ERROR" /* ERROR */, "UNSET" /* UNSET */].includes(this.status)) {
|
|
1843
|
-
this.state = "LOADING" /* LOADING */;
|
|
1844
|
-
if (this.scopeLocation || this.isModuleApp) {
|
|
1845
|
-
const iframe = await this.createIframe();
|
|
1846
|
-
this.iframe = iframe;
|
|
1847
|
-
}
|
|
1848
|
-
this.source = new EntrySource(this.url);
|
|
1849
|
-
await this.source.importEntry(this);
|
|
1850
|
-
}
|
|
1851
|
-
}
|
|
1852
|
-
unmount(needDestroy = false) {
|
|
1853
|
-
this.state = "UNMOUNT" /* UNMOUNT */;
|
|
1854
|
-
this.sandBox?.deactivated();
|
|
1855
|
-
needDestroy && appCache.deleteApp(this.url);
|
|
1856
|
-
this.container.innerHTML = "";
|
|
1857
|
-
this.container = void 0;
|
|
1858
|
-
}
|
|
1859
|
-
get appCacheKey() {
|
|
1860
|
-
return this.url;
|
|
1861
|
-
}
|
|
1862
|
-
get status() {
|
|
1863
|
-
return this.state;
|
|
1864
|
-
}
|
|
1865
|
-
set status(v) {
|
|
1866
|
-
this.state = v;
|
|
1867
|
-
}
|
|
1868
|
-
};
|
|
1869
|
-
|
|
1870
|
-
// src/entry/entry.ts
|
|
1871
|
-
var SCRIPT_TYPE_NAMES = [
|
|
1872
|
-
"text/javascript",
|
|
1873
|
-
"text/ecmascript",
|
|
1874
|
-
"application/javascript",
|
|
1875
|
-
"application/ecmascript",
|
|
1876
|
-
"module"
|
|
1877
|
-
];
|
|
1878
|
-
var EntrySource = class {
|
|
1879
|
-
constructor(url) {
|
|
1880
|
-
this.url = url;
|
|
1881
|
-
this.scripts = /* @__PURE__ */ new Map();
|
|
1882
|
-
this.styles = /* @__PURE__ */ new Map();
|
|
1883
|
-
}
|
|
1884
|
-
html = null;
|
|
1885
|
-
rawHtml;
|
|
1886
|
-
scripts;
|
|
1887
|
-
styles;
|
|
1888
|
-
collectLink(link, parent, needReplaceELement = false) {
|
|
1889
|
-
if (link.hasAttribute("exclude")) {
|
|
1890
|
-
return { replace: document.createComment("\u3010bk-weweb\u3011style with exclude attribute is ignored") };
|
|
1891
|
-
}
|
|
1892
|
-
if (link.hasAttribute("ignore")) {
|
|
1893
|
-
return { replace: link };
|
|
1894
|
-
}
|
|
1895
|
-
const rel = link.getAttribute("rel");
|
|
1896
|
-
let href = link.getAttribute("href");
|
|
1897
|
-
let replaceElement;
|
|
1898
|
-
if (rel === "stylesheet" && href) {
|
|
1899
|
-
href = fillUpPath(href, this.url);
|
|
1900
|
-
replaceElement = document.createComment(`\u3010bk-weweb\u3011style with href=${href}`);
|
|
1901
|
-
let styleInstance = this.getStyle(href);
|
|
1902
|
-
if (!styleInstance) {
|
|
1903
|
-
styleInstance = new Style({
|
|
1904
|
-
code: "",
|
|
1905
|
-
fromHtml: !needReplaceELement,
|
|
1906
|
-
prefetch: !!link.getAttribute("prefetch"),
|
|
1907
|
-
preload: !!link.getAttribute("preload"),
|
|
1908
|
-
url: href
|
|
1909
|
-
});
|
|
1910
|
-
this.styles.set(href, styleInstance);
|
|
1911
|
-
}
|
|
1912
|
-
!needReplaceELement && parent.replaceChild(replaceElement, link);
|
|
1913
|
-
return { replace: replaceElement, style: styleInstance };
|
|
1914
|
-
}
|
|
1915
|
-
if (rel && ["apple-touch-icon", "icon", "prefetch", "preload", "prerender"].includes(rel)) {
|
|
1916
|
-
replaceElement = document.createComment(`\u3010bk-weweb\u3011style with rel=${rel}${href ? ` & href=${href}` : ""}`);
|
|
1917
|
-
!needReplaceELement && parent.removeChild(link);
|
|
1918
|
-
return { replace: replaceElement };
|
|
1919
|
-
}
|
|
1920
|
-
if (href) {
|
|
1921
|
-
link.setAttribute("href", fillUpPath(href, this.url));
|
|
1922
|
-
}
|
|
1923
|
-
return { replace: link };
|
|
1924
|
-
}
|
|
1925
|
-
collectScript(script, parent, needReplaceELement = false) {
|
|
1926
|
-
if (script.hasAttribute("ignore") || script.type !== "module" && isJsonpUrl(script.getAttribute("src")) || script.hasAttribute("type") && !SCRIPT_TYPE_NAMES.includes(script.type)) {
|
|
1927
|
-
return;
|
|
1928
|
-
}
|
|
1929
|
-
let replaceElement = null;
|
|
1930
|
-
if (script.hasAttribute("exclude")) {
|
|
1931
|
-
replaceElement = document.createComment("\u3010bk-weweb\u3011script element with exclude attribute is removed");
|
|
1932
|
-
!needReplaceELement && parent.replaceChild(replaceElement, script);
|
|
1933
|
-
return { replace: replaceElement };
|
|
1934
|
-
}
|
|
1935
|
-
let src = script.getAttribute("src");
|
|
1936
|
-
if (src) {
|
|
1937
|
-
src = fillUpPath(src, this.url);
|
|
1938
|
-
let scriptInstance = this.getScript(src);
|
|
1939
|
-
if (!scriptInstance) {
|
|
1940
|
-
scriptInstance = new Script({
|
|
1941
|
-
async: script.hasAttribute("async"),
|
|
1942
|
-
code: "",
|
|
1943
|
-
defer: script.defer || script.type === "module",
|
|
1944
|
-
fromHtml: !needReplaceELement,
|
|
1945
|
-
isModule: script.type === "module",
|
|
1946
|
-
url: src
|
|
1947
|
-
});
|
|
1948
|
-
this.scripts.set(src, scriptInstance);
|
|
1949
|
-
}
|
|
1950
|
-
replaceElement = document.createComment(`\u3010bk-weweb\u3011script with src='${src}'`);
|
|
1951
|
-
!needReplaceELement && parent.replaceChild(replaceElement, script);
|
|
1952
|
-
return { replace: replaceElement, script: scriptInstance };
|
|
1953
|
-
}
|
|
1954
|
-
if (script.textContent) {
|
|
1955
|
-
const nonceStr = randomUrl();
|
|
1956
|
-
const scriptInstance = new Script({
|
|
1957
|
-
async: false,
|
|
1958
|
-
// code: script.textContent.replace(/var\s+(\w+)=/gm, `window.$1 = `),
|
|
1959
|
-
code: script.textContent,
|
|
1960
|
-
defer: script.type === "module",
|
|
1961
|
-
fromHtml: !needReplaceELement,
|
|
1962
|
-
initial: false,
|
|
1963
|
-
isModule: script.type === "module",
|
|
1964
|
-
url: nonceStr
|
|
1965
|
-
});
|
|
1966
|
-
this.scripts.set(nonceStr, scriptInstance);
|
|
1967
|
-
replaceElement = document.createComment("\u3010bk-weweb\u3011script with text content");
|
|
1968
|
-
!needReplaceELement && parent.replaceChild(replaceElement, script);
|
|
1969
|
-
return {
|
|
1970
|
-
replace: replaceElement,
|
|
1971
|
-
script: scriptInstance
|
|
1972
|
-
};
|
|
1973
|
-
}
|
|
1974
|
-
return { replace: script };
|
|
1975
|
-
}
|
|
1976
|
-
collectScriptAndStyle(parent) {
|
|
1977
|
-
const links = Array.from(parent.querySelectorAll("link"));
|
|
1978
|
-
for (const link of links || []) {
|
|
1979
|
-
this.collectLink(link, link.parentElement);
|
|
1980
|
-
}
|
|
1981
|
-
const styles = Array.from(parent.querySelectorAll("style"));
|
|
1982
|
-
for (const style of styles || []) {
|
|
1983
|
-
if (!style.hasAttribute("exclude") && !style.hasAttribute("ignore")) {
|
|
1984
|
-
this.styles.set(
|
|
1985
|
-
randomUrl(),
|
|
1986
|
-
new Style({
|
|
1987
|
-
code: style.textContent || "",
|
|
1988
|
-
fromHtml: true,
|
|
1989
|
-
url: ""
|
|
1990
|
-
})
|
|
1991
|
-
);
|
|
1992
|
-
style.remove();
|
|
1993
|
-
}
|
|
1994
|
-
}
|
|
1995
|
-
const scripts = Array.from(parent.querySelectorAll("script"));
|
|
1996
|
-
for (const script of scripts) {
|
|
1997
|
-
this.collectScript(script, script.parentElement);
|
|
1998
|
-
}
|
|
1999
|
-
const metas = Array.from(parent.querySelectorAll("meta"));
|
|
2000
|
-
for (const meta of metas) {
|
|
2001
|
-
meta.parentElement.removeChild(meta);
|
|
2002
|
-
}
|
|
2003
|
-
const imgs = Array.from(parent.querySelectorAll("img"));
|
|
2004
|
-
for (const img of imgs) {
|
|
2005
|
-
if (img.hasAttribute("src")) {
|
|
2006
|
-
img.setAttribute("src", fillUpPath(img.getAttribute("src"), this.url));
|
|
2007
|
-
}
|
|
2008
|
-
}
|
|
2009
|
-
}
|
|
2010
|
-
getScript(url) {
|
|
2011
|
-
return this.scripts.get(url);
|
|
2012
|
-
}
|
|
2013
|
-
getStyle(urlOrCode) {
|
|
2014
|
-
return this.styles.get(urlOrCode) || Array.from(this.styles.values()).find((style) => style.code === urlOrCode);
|
|
2015
|
-
}
|
|
2016
|
-
async importEntry(app) {
|
|
2017
|
-
if (app.initSource?.length) {
|
|
2018
|
-
const { collectScript, collectStyle } = await collectSource(app.initSource);
|
|
2019
|
-
if (collectScript) {
|
|
2020
|
-
this.scripts = collectScript;
|
|
2021
|
-
}
|
|
2022
|
-
if (collectStyle) {
|
|
2023
|
-
this.styles = collectStyle;
|
|
2024
|
-
}
|
|
2025
|
-
}
|
|
2026
|
-
if (app instanceof MicroAppModel) await this.importHtmlEntry(app);
|
|
2027
|
-
else if (app instanceof MicroInstanceModel) await this.importInstanceEntry(app);
|
|
2028
|
-
}
|
|
2029
|
-
async importHtmlEntry(app) {
|
|
2030
|
-
let htmlStr = appCache.getCacheHtml(this.url);
|
|
2031
|
-
if (!htmlStr) {
|
|
2032
|
-
htmlStr = await fetchSource(addUrlProtocol(this.url), { cache: "no-cache" }, app);
|
|
2033
|
-
if (!htmlStr) {
|
|
2034
|
-
console.error("load app entry error, place check");
|
|
2035
|
-
return Promise.reject();
|
|
2036
|
-
}
|
|
2037
|
-
}
|
|
2038
|
-
this.rawHtml = htmlStr;
|
|
2039
|
-
const wrapElement = document.createElement("div");
|
|
2040
|
-
if (wrapElement.__BK_WEWEB_APP_KEY__) {
|
|
2041
|
-
wrapElement.__BK_WEWEB_APP_KEY__ = void 0;
|
|
2042
|
-
}
|
|
2043
|
-
wrapElement.innerHTML = htmlStr.replace(/<\/?head>/gim, "").replace(/<\/?body>/i, "");
|
|
2044
|
-
this.collectScriptAndStyle(wrapElement);
|
|
2045
|
-
await executeAppStyles(app, wrapElement);
|
|
2046
|
-
this.html = wrapElement;
|
|
2047
|
-
}
|
|
2048
|
-
async importInstanceEntry(app) {
|
|
2049
|
-
let jsStr = appCache.getCacheScript(this.url)?.code;
|
|
2050
|
-
if (!jsStr) {
|
|
2051
|
-
jsStr = await fetchSource(this.url, { cache: "no-cache" }, app);
|
|
2052
|
-
}
|
|
2053
|
-
if (!jsStr) {
|
|
2054
|
-
console.error("load app entry error, place check");
|
|
2055
|
-
return Promise.reject();
|
|
2056
|
-
}
|
|
2057
|
-
this.scripts.set(
|
|
2058
|
-
this.url,
|
|
2059
|
-
new Script({
|
|
2060
|
-
async: false,
|
|
2061
|
-
code: jsStr,
|
|
2062
|
-
defer: false,
|
|
2063
|
-
fromHtml: true,
|
|
2064
|
-
isModule: !!this.url.match(/\.ts$/),
|
|
2065
|
-
url: this.url
|
|
2066
|
-
})
|
|
2067
|
-
);
|
|
2068
|
-
}
|
|
2069
|
-
setScript(url, script) {
|
|
2070
|
-
this.scripts.set(url, script instanceof Script ? script : new Script(script));
|
|
2071
|
-
}
|
|
2072
|
-
setStyle(url, style) {
|
|
2073
|
-
this.styles.set(url, style);
|
|
2074
|
-
}
|
|
2075
|
-
};
|
|
2076
|
-
|
|
2077
|
-
// src/cache/app-cache.ts
|
|
2078
|
-
var AppCache = class {
|
|
2079
|
-
// todo 主应用共享资源包
|
|
2080
|
-
baseSource;
|
|
2081
|
-
cache;
|
|
2082
|
-
constructor() {
|
|
2083
|
-
this.cache = /* @__PURE__ */ new Map();
|
|
2084
|
-
this.baseSource = new EntrySource(location.href);
|
|
2085
|
-
}
|
|
2086
|
-
// 删除缓存
|
|
2087
|
-
deleteApp(url) {
|
|
2088
|
-
this.cache.delete(url);
|
|
2089
|
-
}
|
|
2090
|
-
// 获取缓存app
|
|
2091
|
-
getApp(name) {
|
|
2092
|
-
if (!name) return void 0;
|
|
2093
|
-
const app = this.cache.get(name);
|
|
2094
|
-
if (app) return app;
|
|
2095
|
-
return Array.from(this.cache.values()).find((item) => item.name === name);
|
|
2096
|
-
}
|
|
2097
|
-
// 获取缓存app
|
|
2098
|
-
getBaseAppStyle(urlOrCode) {
|
|
2099
|
-
return this.baseSource.getStyle(urlOrCode);
|
|
2100
|
-
}
|
|
2101
|
-
getCacheHtml(url) {
|
|
2102
|
-
const list = Array.from(this.cache.values());
|
|
2103
|
-
const app = list.find((item) => item.url === url);
|
|
2104
|
-
if (app) return app.source?.rawHtml || "";
|
|
2105
|
-
return "";
|
|
2106
|
-
}
|
|
2107
|
-
getCacheScript(url) {
|
|
2108
|
-
let script = this.baseSource.getScript(url);
|
|
2109
|
-
if (script) return;
|
|
2110
|
-
const list = Array.from(this.cache.values());
|
|
2111
|
-
list.some((app) => {
|
|
2112
|
-
script = app.source?.getScript(url);
|
|
2113
|
-
return !!script;
|
|
2114
|
-
});
|
|
2115
|
-
return script;
|
|
2116
|
-
}
|
|
2117
|
-
getCacheStyle(url) {
|
|
2118
|
-
let style = this.baseSource.getStyle(url);
|
|
2119
|
-
if (style) return;
|
|
2120
|
-
const list = Array.from(this.cache.values());
|
|
2121
|
-
list.some((app) => {
|
|
2122
|
-
style = app.source?.getStyle(url);
|
|
2123
|
-
return !!style;
|
|
2124
|
-
});
|
|
2125
|
-
return style;
|
|
2126
|
-
}
|
|
2127
|
-
setApp(app) {
|
|
2128
|
-
this.cache.set(app.appCacheKey, app);
|
|
2129
|
-
}
|
|
2130
|
-
setBaseAppScript(url, script) {
|
|
2131
|
-
this.baseSource.setScript(url, script);
|
|
2132
|
-
}
|
|
2133
|
-
setBaseAppStyle(url, style) {
|
|
2134
|
-
this.baseSource.setStyle(url, style);
|
|
2135
|
-
}
|
|
2136
|
-
get hasActiveApp() {
|
|
2137
|
-
return Array.from(this.cache.values()).some((app) => app.status !== "UNMOUNT" /* UNMOUNT */);
|
|
2138
|
-
}
|
|
2139
|
-
};
|
|
2140
|
-
var appCache = new AppCache();
|
|
2141
|
-
window.__getAppOrInstance__ = (id) => {
|
|
2142
|
-
if (!id) return appCache;
|
|
2143
|
-
return appCache.getApp(id);
|
|
2144
|
-
};
|
|
2145
|
-
|
|
2146
|
-
// src/base-app/element.ts
|
|
2147
|
-
function getStyleSource(url, style, originLink) {
|
|
2148
|
-
const replaceStyle = document.createElement("style");
|
|
2149
|
-
setMarkElement(replaceStyle);
|
|
2150
|
-
fetchSource(url).then((data) => {
|
|
2151
|
-
style.code = data;
|
|
2152
|
-
appCache.setBaseAppStyle(url, style);
|
|
2153
|
-
replaceStyle.textContent = data;
|
|
2154
|
-
dispatchLinkOrScriptLoad(originLink);
|
|
2155
|
-
}).catch((err) => {
|
|
2156
|
-
console.error(err);
|
|
2157
|
-
dispatchLinkOrScriptError(originLink);
|
|
2158
|
-
});
|
|
2159
|
-
return replaceStyle;
|
|
2160
|
-
}
|
|
2161
|
-
function getScriptSource(url, script, originScript) {
|
|
2162
|
-
const replaceScript = document.createElement("script");
|
|
2163
|
-
setMarkElement(replaceScript);
|
|
2164
|
-
fetchSource(url).then((code) => {
|
|
2165
|
-
script.setCode(code);
|
|
2166
|
-
try {
|
|
2167
|
-
replaceScript.textContent = code;
|
|
2168
|
-
if (!url.startsWith("inline-")) {
|
|
2169
|
-
originScript.setAttribute("origin-src", url);
|
|
2170
|
-
}
|
|
2171
|
-
} catch (e) {
|
|
2172
|
-
console.error(e, url);
|
|
2173
|
-
}
|
|
2174
|
-
dispatchLinkOrScriptLoad(originScript);
|
|
2175
|
-
}).catch((err) => {
|
|
2176
|
-
console.error(err);
|
|
2177
|
-
dispatchLinkOrScriptError(originScript);
|
|
2178
|
-
});
|
|
2179
|
-
return replaceScript;
|
|
2180
|
-
}
|
|
2181
|
-
function createNewNode(child) {
|
|
2182
|
-
if (child instanceof HTMLLinkElement) {
|
|
2183
|
-
const rel = child.getAttribute("rel");
|
|
2184
|
-
let href = child.getAttribute("href");
|
|
2185
|
-
if (rel === "stylesheet" && href) {
|
|
2186
|
-
href = fillUpPath(href, location.origin);
|
|
2187
|
-
const replaceStyle = document.createElement("style");
|
|
2188
|
-
const styleInstance = new Style({
|
|
2189
|
-
code: "",
|
|
2190
|
-
fromHtml: false,
|
|
2191
|
-
url: href
|
|
2192
|
-
});
|
|
2193
|
-
getStyleSource(href, styleInstance, child);
|
|
2194
|
-
return replaceStyle;
|
|
2195
|
-
}
|
|
2196
|
-
}
|
|
2197
|
-
if (child instanceof HTMLScriptElement) {
|
|
2198
|
-
let src = child.getAttribute("src");
|
|
2199
|
-
if (src && child.type !== "module") {
|
|
2200
|
-
src = fillUpPath(src, location.origin);
|
|
2201
|
-
const script = new Script({
|
|
2202
|
-
async: child.hasAttribute("async"),
|
|
2203
|
-
code: "",
|
|
2204
|
-
defer: child.defer || child.type === "module",
|
|
2205
|
-
fromHtml: false,
|
|
2206
|
-
isModule: false
|
|
2207
|
-
});
|
|
2208
|
-
appCache.setBaseAppScript(src, script);
|
|
2209
|
-
const replaceElement = getScriptSource(src, script, child);
|
|
2210
|
-
return replaceElement || child;
|
|
2211
|
-
}
|
|
2212
|
-
}
|
|
2213
|
-
return child;
|
|
2214
|
-
}
|
|
2215
|
-
function isLinkOrScript(node) {
|
|
2216
|
-
return node instanceof HTMLLinkElement || node instanceof HTMLScriptElement;
|
|
2217
|
-
}
|
|
2218
|
-
function baseElementInertHandle(parent, newChild, passiveChild, rawMethod) {
|
|
2219
|
-
if (isLinkOrScript(newChild)) {
|
|
2220
|
-
const targetChild = createNewNode(newChild);
|
|
2221
|
-
return rawMethod.call(parent, targetChild, passiveChild);
|
|
2222
|
-
}
|
|
2223
|
-
return rawMethod.call(parent, newChild, passiveChild);
|
|
2224
|
-
}
|
|
2225
|
-
function baseElementAppendHandle(parent, newChild, rawMethod) {
|
|
2226
|
-
if (isLinkOrScript(newChild)) {
|
|
2227
|
-
const targetChild = createNewNode(newChild);
|
|
2228
|
-
return rawMethod.call(parent, targetChild);
|
|
2229
|
-
}
|
|
2230
|
-
return rawMethod.call(parent, newChild);
|
|
2231
|
-
}
|
|
2232
|
-
|
|
2233
|
-
// src/base-app/collect-source.ts
|
|
2234
|
-
function collectBaseSource() {
|
|
2235
|
-
const rawBodyAppendChild = HTMLBodyElement.prototype.appendChild;
|
|
2236
|
-
const rawHeadAppendChild = HTMLHeadElement.prototype.appendChild;
|
|
2237
|
-
const rawHeadInsertBefore = HTMLHeadElement.prototype.appendChild;
|
|
2238
|
-
HTMLBodyElement.prototype.appendChild = function(newChild) {
|
|
2239
|
-
return baseElementAppendHandle(this, newChild, rawBodyAppendChild);
|
|
2240
|
-
};
|
|
2241
|
-
HTMLHeadElement.prototype.appendChild = function(newChild) {
|
|
2242
|
-
return baseElementAppendHandle(this, newChild, rawHeadAppendChild);
|
|
2243
|
-
};
|
|
2244
|
-
HTMLHeadElement.prototype.insertBefore = function(newChild, refChild) {
|
|
2245
|
-
return baseElementInertHandle(this, newChild, refChild, rawHeadInsertBefore);
|
|
2246
|
-
};
|
|
2247
|
-
window.addEventListener("load", () => {
|
|
2248
|
-
const nodeList = document.head.querySelectorAll("style");
|
|
2249
|
-
for (const node of Array.from(nodeList)) {
|
|
2250
|
-
node.textContent && appCache.setBaseAppStyle(
|
|
2251
|
-
randomUrl(),
|
|
2252
|
-
new Style({
|
|
2253
|
-
code: node.textContent,
|
|
2254
|
-
fromHtml: false,
|
|
2255
|
-
url: ""
|
|
2256
|
-
})
|
|
2257
|
-
);
|
|
2258
|
-
}
|
|
2259
|
-
});
|
|
2260
|
-
}
|
|
2261
|
-
export {
|
|
2262
|
-
collectBaseSource
|
|
2263
|
-
};
|
|
29
|
+
`:this.code}};function He(r,e){return!r.hasOwnProperty(e)||!Number.isNaN(e)&&e<r.length||typeof navigator<"u"&&navigator.userAgent.indexOf("Trident")!==-1&&r[e]&&typeof window<"u"&&r[e].parent===window}function Qe(r,e){let t=0,o,n;for(let i in r)if(!He(r,i)){if(t===0&&i!==q||t===1&&i!==U)return i;o?(z=i,n=e&&n||i):o=i===z,t+=1}return n}function et(r){U=void 0,q=U;for(let e in r)He(r,e)||(q?U||(U=e):q=e,z=e);return z}async function G(r){let e=Array.from(r.source.scripts.values()).filter(i=>i.fromHtml||i.initial),t=e.filter(i=>!i.async&&!i.defer||i.isModule);await Promise.all(t.map(i=>i.getCode(r))),await Promise.all(t.map(i=>i.executeCode(r)));let o=[],n=[];for(let i of e)(i.defer||i.async)&&(!i.code&&i.defer?o.push(i.executeCode(r)):n.push(i.executeCode(r)));await Promise.all([...n,...o]).catch(i=>{console.error(i)})}var Ne="about:blank",A=class{state="UNSET";container;data;iframe=null;initSource;isModuleApp=!1;isPreLoad=!1;keepAlive;mode="app";name;sandBox;scopeCss;scopeJs;scopeLocation;showSourceCode;source;url;constructor(e){this.name=e.id!==e.url?e.id:W(5),this.mode=e.mode??"app",this.container=e.container??void 0,this.showSourceCode=e.showSourceCode??!1,this.url=e.url,this.data=e.data||{},this.scopeJs=e.scopeJs??!0,this.scopeCss=e.scopeCss??!0,this.scopeLocation=e.scopeLocation??!1,this.isPreLoad=e.isPreLoad??!1,this.keepAlive=e.keepAlive??!1,this.initSource=e.initSource??[],this.scopeJs&&(this.sandBox=new R(this)),this.container instanceof HTMLElement&&this.container.setAttribute(I,this.name)}activated(e,t){this.isPreLoad=!1,this.state="ACTIVATED";let o=this;if(e&&this.container){e instanceof Element&&e.setAttribute(I,this.name);let n=document.createDocumentFragment(),i=Array.from(this.container.childNodes);for(let c of i)c.__BK_WEWEB_APP_KEY__=this.appCacheKey,Object.defineProperties(c,{ownerDocument:{get(){return o.sandBox?.rawDocument}}}),n.appendChild(c);e.innerHTML="",e.appendChild(n),this.container=e,this.initShadowRootContainer(),this.sandBox?.activated(this.data),t?.(this)}}createIframe(){return new Promise(e=>{let t=document.createElement("iframe"),o=new URL(T(this.url)),n=navigator.userAgent.indexOf("Chrome")>-1;if(t.setAttribute("src",`${n?Ne:location.origin}${o.pathname||"/"}${o.search}${o.hash}`),t.style.cssText="display: none;",document.body.appendChild(t),n)setTimeout(()=>e(t),0);else{let i=setInterval(()=>{t.contentWindow&&t.contentWindow.location.href!==Ne&&(t.contentWindow.stop(),t.contentDocument.body.parentElement.innerHTML="<head></head><body></body>",clearInterval(i),e(t))},0)}})}deactivated(){this.state="DEACTIVATED",this.sandBox?.deactivated()}initShadowRootContainer(){this.container instanceof ShadowRoot&&Object.defineProperties(this.container,{getBoundingClientRect:{get(){return this.host.getBoundingClientRect}}})}mount(e,t){this.isPreLoad=!1,this.container=e??this.container,this.initShadowRootContainer(),this.state="MOUNTING";let o=this;if(this.container){this.container instanceof Element&&this.container.setAttribute(I,this.name);let n=this.source.html.cloneNode(!0),i=document.createDocumentFragment();for(let c of Array.from(n.childNodes))c.__BK_WEWEB_APP_KEY__=this.appCacheKey,Object.defineProperties(c,{ownerDocument:{get(){return o.sandBox?.rawDocument}}}),i.appendChild(c);this.container.innerHTML="",this.container.appendChild(i),this.sandBox?.activated(this.data),G(this).finally(()=>{this.state="MOUNTED",t?.(this)})}}onError(){this.state="ERROR"}onMount(){this.isPreLoad||(this.state="LOADED",this.mount())}registerRunningApp(){M(this),Promise.resolve().then(()=>M(null))}async start(){if(!this.source||["ERROR","UNSET"].includes(this.status)){if(this.state="LOADING",this.scopeLocation||this.isModuleApp){let e=await this.createIframe();this.iframe=e}this.source=new v(this.url),await this.source.importEntry(this)}}unmount(e=!1){this.state="UNMOUNT",this.sandBox?.deactivated(),e&&a.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}get appCacheKey(){return this.url}get status(){return this.state}set status(e){this.state=e}};var tt=["text/javascript","text/ecmascript","application/javascript","application/ecmascript","module"],v=class{constructor(e){this.url=e;this.scripts=new Map,this.styles=new Map}html=null;rawHtml;scripts;styles;collectLink(e,t,o=!1){if(e.hasAttribute("exclude"))return{replace:document.createComment("\u3010bk-weweb\u3011style with exclude attribute is ignored")};if(e.hasAttribute("ignore"))return{replace:e};let n=e.getAttribute("rel"),i=e.getAttribute("href"),c;if(n==="stylesheet"&&i){i=h(i,this.url),c=document.createComment(`\u3010bk-weweb\u3011style with href=${i}`);let s=this.getStyle(i);return s||(s=new w({code:"",fromHtml:!o,prefetch:!!e.getAttribute("prefetch"),preload:!!e.getAttribute("preload"),url:i}),this.styles.set(i,s)),!o&&t.replaceChild(c,e),{replace:c,style:s}}return n&&["apple-touch-icon","icon","prefetch","preload","prerender"].includes(n)?(c=document.createComment(`\u3010bk-weweb\u3011style with rel=${n}${i?` & href=${i}`:""}`),!o&&t.removeChild(e),{replace:c}):(i&&e.setAttribute("href",h(i,this.url)),{replace:e})}collectScript(e,t,o=!1){if(e.hasAttribute("ignore")||e.type!=="module"&&j(e.getAttribute("src"))||e.hasAttribute("type")&&!tt.includes(e.type))return;let n=null;if(e.hasAttribute("exclude"))return n=document.createComment("\u3010bk-weweb\u3011script element with exclude attribute is removed"),!o&&t.replaceChild(n,e),{replace:n};let i=e.getAttribute("src");if(i){i=h(i,this.url);let c=this.getScript(i);return c||(c=new y({async:e.hasAttribute("async"),code:"",defer:e.defer||e.type==="module",fromHtml:!o,isModule:e.type==="module",url:i}),this.scripts.set(i,c)),n=document.createComment(`\u3010bk-weweb\u3011script with src='${i}'`),!o&&t.replaceChild(n,e),{replace:n,script:c}}if(e.textContent){let c=x(),s=new y({async:!1,code:e.textContent,defer:e.type==="module",fromHtml:!o,initial:!1,isModule:e.type==="module",url:c});return this.scripts.set(c,s),n=document.createComment("\u3010bk-weweb\u3011script with text content"),!o&&t.replaceChild(n,e),{replace:n,script:s}}return{replace:e}}collectScriptAndStyle(e){let t=Array.from(e.querySelectorAll("link"));for(let s of t||[])this.collectLink(s,s.parentElement);let o=Array.from(e.querySelectorAll("style"));for(let s of o||[])!s.hasAttribute("exclude")&&!s.hasAttribute("ignore")&&(this.styles.set(x(),new w({code:s.textContent||"",fromHtml:!0,url:""})),s.remove());let n=Array.from(e.querySelectorAll("script"));for(let s of n)this.collectScript(s,s.parentElement);let i=Array.from(e.querySelectorAll("meta"));for(let s of i)s.parentElement.removeChild(s);let c=Array.from(e.querySelectorAll("img"));for(let s of c)s.hasAttribute("src")&&s.setAttribute("src",h(s.getAttribute("src"),this.url))}getScript(e){return this.scripts.get(e)}getStyle(e){return this.styles.get(e)||Array.from(this.styles.values()).find(t=>t.code===e)}async importEntry(e){if(e.initSource?.length){let{collectScript:t,collectStyle:o}=await Re(e.initSource);t&&(this.scripts=t),o&&(this.styles=o)}e instanceof A?await this.importHtmlEntry(e):e instanceof C&&await this.importInstanceEntry(e)}async importHtmlEntry(e){let t=a.getCacheHtml(this.url);if(!t&&(t=await g(T(this.url),{cache:"no-cache"},e),!t))return console.error("load app entry error, place check"),Promise.reject();this.rawHtml=t;let o=document.createElement("div");o.__BK_WEWEB_APP_KEY__&&(o.__BK_WEWEB_APP_KEY__=void 0),o.innerHTML=t.replace(/<\/?head>/gim,"").replace(/<\/?body>/i,""),this.collectScriptAndStyle(o),await V(e,o),this.html=o}async importInstanceEntry(e){let t=a.getCacheScript(this.url)?.code;if(t||(t=await g(this.url,{cache:"no-cache"},e)),!t)return console.error("load app entry error, place check"),Promise.reject();this.scripts.set(this.url,new y({async:!1,code:t,defer:!1,fromHtml:!0,isModule:!!this.url.match(/\.ts$/),url:this.url}))}setScript(e,t){this.scripts.set(e,t instanceof y?t:new y(t))}setStyle(e,t){this.styles.set(e,t)}};var Se=class{baseSource;cache;constructor(){this.cache=new Map,this.baseSource=new v(location.href)}deleteApp(e){this.cache.delete(e)}getApp(e){if(!e)return;let t=this.cache.get(e);return t||Array.from(this.cache.values()).find(o=>o.name===e)}getBaseAppStyle(e){return this.baseSource.getStyle(e)}getCacheHtml(e){let o=Array.from(this.cache.values()).find(n=>n.url===e);return o&&o.source?.rawHtml||""}getCacheScript(e){let t=this.baseSource.getScript(e);return t?void 0:(Array.from(this.cache.values()).some(n=>(t=n.source?.getScript(e),!!t)),t)}getCacheStyle(e){let t=this.baseSource.getStyle(e);return t?void 0:(Array.from(this.cache.values()).some(n=>(t=n.source?.getStyle(e),!!t)),t)}setApp(e){this.cache.set(e.appCacheKey,e)}setBaseAppScript(e,t){this.baseSource.setScript(e,t)}setBaseAppStyle(e,t){this.baseSource.setStyle(e,t)}get hasActiveApp(){return Array.from(this.cache.values()).some(e=>e.status!=="UNMOUNT")}},a=new Se;window.__getAppOrInstance__=r=>r?a.getApp(r):a;function ot(r,e,t){let o=document.createElement("style");return E(o),g(r).then(n=>{e.code=n,a.setBaseAppStyle(r,e),o.textContent=n,_(t)}).catch(n=>{console.error(n),K(t)}),o}function rt(r,e,t){let o=document.createElement("script");return E(o),g(r).then(n=>{e.setCode(n);try{o.textContent=n,r.startsWith("inline-")||t.setAttribute("origin-src",r)}catch(i){console.error(i,r)}_(t)}).catch(n=>{console.error(n),K(t)}),o}function Ke(r){if(r instanceof HTMLLinkElement){let e=r.getAttribute("rel"),t=r.getAttribute("href");if(e==="stylesheet"&&t){t=h(t,location.origin);let o=document.createElement("style"),n=new w({code:"",fromHtml:!1,url:t});return ot(t,n,r),o}}if(r instanceof HTMLScriptElement){let e=r.getAttribute("src");if(e&&r.type!=="module"){e=h(e,location.origin);let t=new y({async:r.hasAttribute("async"),code:"",defer:r.defer||r.type==="module",fromHtml:!1,isModule:!1});return a.setBaseAppScript(e,t),rt(e,t,r)||r}}return r}function De(r){return r instanceof HTMLLinkElement||r instanceof HTMLScriptElement}function Ue(r,e,t,o){if(De(e)){let n=Ke(e);return o.call(r,n,t)}return o.call(r,e,t)}function we(r,e,t){if(De(e)){let o=Ke(e);return t.call(r,o)}return t.call(r,e)}function Oe(){let r=HTMLBodyElement.prototype.appendChild,e=HTMLHeadElement.prototype.appendChild,t=HTMLHeadElement.prototype.appendChild;HTMLBodyElement.prototype.appendChild=function(o){return we(this,o,r)},HTMLHeadElement.prototype.appendChild=function(o){return we(this,o,e)},HTMLHeadElement.prototype.insertBefore=function(o,n){return Ue(this,o,n,t)},window.addEventListener("load",()=>{let o=document.head.querySelectorAll("style");for(let n of Array.from(o))n.textContent&&a.setBaseAppStyle(x(),new w({code:n.textContent,fromHtml:!1,url:""}))})}export{Oe as collectBaseSource};
|
|
2264
30
|
//# sourceMappingURL=collect-source.esm.js.map
|