@zstackui/qiankun 2.6.3-beta-5
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/LICENSE +21 -0
- package/README.md +116 -0
- package/dist/index.umd.js +8681 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/index.umd.min.js +4 -0
- package/dist/index.umd.min.js.map +1 -0
- package/dist/src/__tests__/globalState.test.d.ts +5 -0
- package/dist/src/__tests__/utils.test.d.ts +1 -0
- package/dist/src/addons/engineFlag.d.ts +6 -0
- package/dist/src/addons/index.d.ts +6 -0
- package/dist/src/addons/runtimePublicPath.d.ts +6 -0
- package/dist/src/apis.d.ts +7 -0
- package/dist/src/effects.d.ts +3 -0
- package/dist/src/error.d.ts +3 -0
- package/dist/src/errorHandler.d.ts +7 -0
- package/dist/src/globalState.d.ts +7 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/interfaces.d.ts +107 -0
- package/dist/src/loader.d.ts +8 -0
- package/dist/src/prefetch.d.ts +14 -0
- package/dist/src/sandbox/__tests__/common.test.d.ts +5 -0
- package/dist/src/sandbox/__tests__/proxySandbox.test.d.ts +10 -0
- package/dist/src/sandbox/common.d.ts +21 -0
- package/dist/src/sandbox/index.d.ts +40 -0
- package/dist/src/sandbox/legacy/__tests__/sandbox.test.d.ts +5 -0
- package/dist/src/sandbox/legacy/sandbox.d.ts +28 -0
- package/dist/src/sandbox/patchers/__tests__/css.test.d.ts +5 -0
- package/dist/src/sandbox/patchers/__tests__/interval.test.d.ts +5 -0
- package/dist/src/sandbox/patchers/css.d.ts +17 -0
- package/dist/src/sandbox/patchers/dynamicAppend/__tests__/common.test.d.ts +1 -0
- package/dist/src/sandbox/patchers/dynamicAppend/common.d.ts +22 -0
- package/dist/src/sandbox/patchers/dynamicAppend/forLooseSandbox.d.ts +17 -0
- package/dist/src/sandbox/patchers/dynamicAppend/forStrictSandbox.d.ts +12 -0
- package/dist/src/sandbox/patchers/dynamicAppend/index.d.ts +6 -0
- package/dist/src/sandbox/patchers/historyListener.d.ts +5 -0
- package/dist/src/sandbox/patchers/index.d.ts +9 -0
- package/dist/src/sandbox/patchers/interval.d.ts +5 -0
- package/dist/src/sandbox/patchers/windowListener.d.ts +5 -0
- package/dist/src/sandbox/proxySandbox.d.ts +23 -0
- package/dist/src/sandbox/snapshotSandbox.d.ts +20 -0
- package/dist/src/utils.d.ts +44 -0
- package/dist/src/version.d.ts +1 -0
- package/es/addons/engineFlag.d.ts +6 -0
- package/es/addons/engineFlag.js +50 -0
- package/es/addons/index.d.ts +6 -0
- package/es/addons/index.js +13 -0
- package/es/addons/runtimePublicPath.d.ts +6 -0
- package/es/addons/runtimePublicPath.js +57 -0
- package/es/apis.d.ts +7 -0
- package/es/apis.js +278 -0
- package/es/effects.d.ts +3 -0
- package/es/effects.js +33 -0
- package/es/error.d.ts +3 -0
- package/es/error.js +16 -0
- package/es/errorHandler.d.ts +7 -0
- package/es/errorHandler.js +13 -0
- package/es/globalState.d.ts +7 -0
- package/es/globalState.js +101 -0
- package/es/index.d.ts +11 -0
- package/es/index.js +11 -0
- package/es/interfaces.d.ts +107 -0
- package/es/interfaces.js +8 -0
- package/es/loader.d.ts +8 -0
- package/es/loader.js +600 -0
- package/es/prefetch.d.ts +14 -0
- package/es/prefetch.js +125 -0
- package/es/sandbox/common.d.ts +21 -0
- package/es/sandbox/common.js +157 -0
- package/es/sandbox/index.d.ts +40 -0
- package/es/sandbox/index.js +105 -0
- package/es/sandbox/legacy/sandbox.d.ts +28 -0
- package/es/sandbox/legacy/sandbox.js +142 -0
- package/es/sandbox/patchers/css.d.ts +17 -0
- package/es/sandbox/patchers/css.js +186 -0
- package/es/sandbox/patchers/dynamicAppend/common.d.ts +22 -0
- package/es/sandbox/patchers/dynamicAppend/common.js +306 -0
- package/es/sandbox/patchers/dynamicAppend/forLooseSandbox.d.ts +17 -0
- package/es/sandbox/patchers/dynamicAppend/forLooseSandbox.js +77 -0
- package/es/sandbox/patchers/dynamicAppend/forStrictSandbox.d.ts +12 -0
- package/es/sandbox/patchers/dynamicAppend/forStrictSandbox.js +103 -0
- package/es/sandbox/patchers/dynamicAppend/index.d.ts +6 -0
- package/es/sandbox/patchers/dynamicAppend/index.js +6 -0
- package/es/sandbox/patchers/historyListener.d.ts +5 -0
- package/es/sandbox/patchers/historyListener.js +54 -0
- package/es/sandbox/patchers/index.d.ts +9 -0
- package/es/sandbox/patchers/index.js +45 -0
- package/es/sandbox/patchers/interval.d.ts +5 -0
- package/es/sandbox/patchers/interval.js +34 -0
- package/es/sandbox/patchers/windowListener.d.ts +5 -0
- package/es/sandbox/patchers/windowListener.js +34 -0
- package/es/sandbox/proxySandbox.d.ts +23 -0
- package/es/sandbox/proxySandbox.js +315 -0
- package/es/sandbox/snapshotSandbox.d.ts +20 -0
- package/es/sandbox/snapshotSandbox.js +59 -0
- package/es/utils.d.ts +44 -0
- package/es/utils.js +215 -0
- package/es/version.d.ts +1 -0
- package/es/version.js +1 -0
- package/lib/addons/engineFlag.d.ts +6 -0
- package/lib/addons/engineFlag.js +55 -0
- package/lib/addons/index.d.ts +6 -0
- package/lib/addons/index.js +21 -0
- package/lib/addons/runtimePublicPath.d.ts +6 -0
- package/lib/addons/runtimePublicPath.js +63 -0
- package/lib/apis.d.ts +7 -0
- package/lib/apis.js +288 -0
- package/lib/effects.d.ts +3 -0
- package/lib/effects.js +42 -0
- package/lib/error.d.ts +3 -0
- package/lib/error.js +23 -0
- package/lib/errorHandler.d.ts +7 -0
- package/lib/errorHandler.js +33 -0
- package/lib/globalState.d.ts +7 -0
- package/lib/globalState.js +110 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +89 -0
- package/lib/interfaces.d.ts +107 -0
- package/lib/interfaces.js +14 -0
- package/lib/loader.d.ts +8 -0
- package/lib/loader.js +606 -0
- package/lib/prefetch.d.ts +14 -0
- package/lib/prefetch.js +132 -0
- package/lib/sandbox/common.d.ts +21 -0
- package/lib/sandbox/common.js +169 -0
- package/lib/sandbox/index.d.ts +40 -0
- package/lib/sandbox/index.js +123 -0
- package/lib/sandbox/legacy/sandbox.d.ts +28 -0
- package/lib/sandbox/legacy/sandbox.js +148 -0
- package/lib/sandbox/patchers/css.d.ts +17 -0
- package/lib/sandbox/patchers/css.js +193 -0
- package/lib/sandbox/patchers/dynamicAppend/common.d.ts +22 -0
- package/lib/sandbox/patchers/dynamicAppend/common.js +322 -0
- package/lib/sandbox/patchers/dynamicAppend/forLooseSandbox.d.ts +17 -0
- package/lib/sandbox/patchers/dynamicAppend/forLooseSandbox.js +84 -0
- package/lib/sandbox/patchers/dynamicAppend/forStrictSandbox.d.ts +12 -0
- package/lib/sandbox/patchers/dynamicAppend/forStrictSandbox.js +110 -0
- package/lib/sandbox/patchers/dynamicAppend/index.d.ts +6 -0
- package/lib/sandbox/patchers/dynamicAppend/index.js +19 -0
- package/lib/sandbox/patchers/historyListener.d.ts +5 -0
- package/lib/sandbox/patchers/historyListener.js +62 -0
- package/lib/sandbox/patchers/index.d.ts +9 -0
- package/lib/sandbox/patchers/index.js +57 -0
- package/lib/sandbox/patchers/interval.d.ts +5 -0
- package/lib/sandbox/patchers/interval.js +42 -0
- package/lib/sandbox/patchers/windowListener.d.ts +5 -0
- package/lib/sandbox/patchers/windowListener.js +42 -0
- package/lib/sandbox/proxySandbox.d.ts +23 -0
- package/lib/sandbox/proxySandbox.js +321 -0
- package/lib/sandbox/snapshotSandbox.d.ts +20 -0
- package/lib/sandbox/snapshotSandbox.js +65 -0
- package/lib/utils.d.ts +44 -0
- package/lib/utils.js +240 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +7 -0
- package/package.json +137 -0
package/es/prefetch.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import _isFunction from "lodash/isFunction";
|
|
2
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
|
3
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
|
|
4
|
+
/**
|
|
5
|
+
* @author Kuitos
|
|
6
|
+
* @since 2019-02-26
|
|
7
|
+
*/
|
|
8
|
+
import { __awaiter } from "tslib";
|
|
9
|
+
import { importEntry } from 'import-html-entry';
|
|
10
|
+
import { getAppStatus, getMountedApps, NOT_LOADED } from 'single-spa';
|
|
11
|
+
// RIC and shim for browsers setTimeout() without it
|
|
12
|
+
var requestIdleCallback = window.requestIdleCallback || function requestIdleCallback(cb) {
|
|
13
|
+
var start = Date.now();
|
|
14
|
+
return setTimeout(function () {
|
|
15
|
+
cb({
|
|
16
|
+
didTimeout: false,
|
|
17
|
+
timeRemaining: function timeRemaining() {
|
|
18
|
+
return Math.max(0, 50 - (Date.now() - start));
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}, 1);
|
|
22
|
+
};
|
|
23
|
+
var isSlowNetwork = navigator.connection ? navigator.connection.saveData || navigator.connection.type !== 'wifi' && navigator.connection.type !== 'ethernet' && /([23])g/.test(navigator.connection.effectiveType) : false;
|
|
24
|
+
/**
|
|
25
|
+
* prefetch assets, do nothing while in mobile network
|
|
26
|
+
* @param entry
|
|
27
|
+
* @param opts
|
|
28
|
+
*/
|
|
29
|
+
function prefetch(entry, opts) {
|
|
30
|
+
var _this = this;
|
|
31
|
+
if (!navigator.onLine || isSlowNetwork) {
|
|
32
|
+
// Don't prefetch if in a slow network or offline
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
requestIdleCallback(function () {
|
|
36
|
+
return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regenerator().m(function _callee() {
|
|
37
|
+
var _yield$importEntry, getExternalScripts, getExternalStyleSheets;
|
|
38
|
+
return _regenerator().w(function (_context) {
|
|
39
|
+
while (1) switch (_context.n) {
|
|
40
|
+
case 0:
|
|
41
|
+
_context.n = 1;
|
|
42
|
+
return importEntry(entry, opts);
|
|
43
|
+
case 1:
|
|
44
|
+
_yield$importEntry = _context.v;
|
|
45
|
+
getExternalScripts = _yield$importEntry.getExternalScripts;
|
|
46
|
+
getExternalStyleSheets = _yield$importEntry.getExternalStyleSheets;
|
|
47
|
+
requestIdleCallback(getExternalStyleSheets);
|
|
48
|
+
requestIdleCallback(getExternalScripts);
|
|
49
|
+
case 2:
|
|
50
|
+
return _context.a(2);
|
|
51
|
+
}
|
|
52
|
+
}, _callee);
|
|
53
|
+
}));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function prefetchAfterFirstMounted(apps, opts) {
|
|
57
|
+
window.addEventListener('single-spa:first-mount', function listener() {
|
|
58
|
+
var notLoadedApps = apps.filter(function (app) {
|
|
59
|
+
return getAppStatus(app.name) === NOT_LOADED;
|
|
60
|
+
});
|
|
61
|
+
if (process.env.NODE_ENV === 'development') {
|
|
62
|
+
var mountedApps = getMountedApps();
|
|
63
|
+
console.log("[qiankun] prefetch starting after ".concat(mountedApps, " mounted..."), notLoadedApps);
|
|
64
|
+
}
|
|
65
|
+
notLoadedApps.forEach(function (_ref) {
|
|
66
|
+
var entry = _ref.entry;
|
|
67
|
+
return prefetch(entry, opts);
|
|
68
|
+
});
|
|
69
|
+
window.removeEventListener('single-spa:first-mount', listener);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
export function prefetchImmediately(apps, opts) {
|
|
73
|
+
if (process.env.NODE_ENV === 'development') {
|
|
74
|
+
console.log('[qiankun] prefetch starting for apps...', apps);
|
|
75
|
+
}
|
|
76
|
+
apps.forEach(function (_ref2) {
|
|
77
|
+
var entry = _ref2.entry;
|
|
78
|
+
return prefetch(entry, opts);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
export function doPrefetchStrategy(apps, prefetchStrategy, importEntryOpts) {
|
|
82
|
+
var _this2 = this;
|
|
83
|
+
var appsName2Apps = function appsName2Apps(names) {
|
|
84
|
+
return apps.filter(function (app) {
|
|
85
|
+
return names.includes(app.name);
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
if (Array.isArray(prefetchStrategy)) {
|
|
89
|
+
prefetchAfterFirstMounted(appsName2Apps(prefetchStrategy), importEntryOpts);
|
|
90
|
+
} else if (_isFunction(prefetchStrategy)) {
|
|
91
|
+
(function () {
|
|
92
|
+
return __awaiter(_this2, void 0, void 0, /*#__PURE__*/_regenerator().m(function _callee2() {
|
|
93
|
+
var _yield$prefetchStrate, _yield$prefetchStrate2, criticalAppNames, _yield$prefetchStrate3, minorAppsName;
|
|
94
|
+
return _regenerator().w(function (_context2) {
|
|
95
|
+
while (1) switch (_context2.n) {
|
|
96
|
+
case 0:
|
|
97
|
+
_context2.n = 1;
|
|
98
|
+
return prefetchStrategy(apps);
|
|
99
|
+
case 1:
|
|
100
|
+
_yield$prefetchStrate = _context2.v;
|
|
101
|
+
_yield$prefetchStrate2 = _yield$prefetchStrate.criticalAppNames;
|
|
102
|
+
criticalAppNames = _yield$prefetchStrate2 === void 0 ? [] : _yield$prefetchStrate2;
|
|
103
|
+
_yield$prefetchStrate3 = _yield$prefetchStrate.minorAppsName;
|
|
104
|
+
minorAppsName = _yield$prefetchStrate3 === void 0 ? [] : _yield$prefetchStrate3;
|
|
105
|
+
prefetchImmediately(appsName2Apps(criticalAppNames), importEntryOpts);
|
|
106
|
+
prefetchAfterFirstMounted(appsName2Apps(minorAppsName), importEntryOpts);
|
|
107
|
+
case 2:
|
|
108
|
+
return _context2.a(2);
|
|
109
|
+
}
|
|
110
|
+
}, _callee2);
|
|
111
|
+
}));
|
|
112
|
+
})();
|
|
113
|
+
} else {
|
|
114
|
+
switch (prefetchStrategy) {
|
|
115
|
+
case true:
|
|
116
|
+
prefetchAfterFirstMounted(apps, importEntryOpts);
|
|
117
|
+
break;
|
|
118
|
+
case 'all':
|
|
119
|
+
prefetchImmediately(apps, importEntryOpts);
|
|
120
|
+
break;
|
|
121
|
+
default:
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Kuitos
|
|
3
|
+
* @since 2020-04-13
|
|
4
|
+
*/
|
|
5
|
+
type AppInstance = {
|
|
6
|
+
name: string;
|
|
7
|
+
window: WindowProxy;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* get the app that running tasks at current tick
|
|
11
|
+
*/
|
|
12
|
+
export declare function getCurrentRunningApp(): AppInstance | null;
|
|
13
|
+
export declare function setCurrentRunningApp(appInstance: {
|
|
14
|
+
name: string;
|
|
15
|
+
window: WindowProxy;
|
|
16
|
+
} | null): void;
|
|
17
|
+
export declare function getTargetValue(target: any, value: any): any;
|
|
18
|
+
export declare function getProxyPropertyValue(getter: CallableFunction): any;
|
|
19
|
+
export declare function getZsLocalStorage(_proxyName: string): any;
|
|
20
|
+
export declare function getZsSessionStorage(_proxyName: string): any;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Kuitos
|
|
3
|
+
* @since 2020-04-13
|
|
4
|
+
*/
|
|
5
|
+
import { isBoundedFunction, isCallable, isConstructable } from '../utils';
|
|
6
|
+
var currentRunningApp = null;
|
|
7
|
+
/**
|
|
8
|
+
* get the app that running tasks at current tick
|
|
9
|
+
*/
|
|
10
|
+
export function getCurrentRunningApp() {
|
|
11
|
+
return currentRunningApp;
|
|
12
|
+
}
|
|
13
|
+
export function setCurrentRunningApp(appInstance) {
|
|
14
|
+
// set currentRunningApp and it's proxySandbox to global window, as its only use case is for document.createElement from now on, which hijacked by a global way
|
|
15
|
+
currentRunningApp = appInstance;
|
|
16
|
+
}
|
|
17
|
+
var functionBoundedValueMap = new WeakMap();
|
|
18
|
+
export function getTargetValue(target, _value) {
|
|
19
|
+
/*
|
|
20
|
+
仅绑定 isCallable && !isBoundedFunction && !isConstructable 的函数对象,如 window.console、window.atob 这类,不然微应用中调用时会抛出 Illegal invocation 异常
|
|
21
|
+
目前没有完美的检测方式,这里通过 prototype 中是否还有可枚举的拓展方法的方式来判断
|
|
22
|
+
@warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常)
|
|
23
|
+
*/
|
|
24
|
+
if (isCallable(_value) && !isBoundedFunction(_value) && !isConstructable(_value)) {
|
|
25
|
+
var cachedBoundFunction = functionBoundedValueMap.get(_value);
|
|
26
|
+
if (cachedBoundFunction) {
|
|
27
|
+
return cachedBoundFunction;
|
|
28
|
+
}
|
|
29
|
+
var boundValue = Function.prototype.bind.call(_value, target);
|
|
30
|
+
// some callable function has custom fields, we need to copy the enumerable props to boundValue. such as moment function.
|
|
31
|
+
// use for..in rather than Object.keys.forEach for performance reason
|
|
32
|
+
// eslint-disable-next-line guard-for-in,no-restricted-syntax
|
|
33
|
+
for (var key in _value) {
|
|
34
|
+
boundValue[key] = _value[key];
|
|
35
|
+
}
|
|
36
|
+
// copy prototype if bound function not have but target one have
|
|
37
|
+
// as prototype is non-enumerable mostly, we need to copy it from target function manually
|
|
38
|
+
if (_value.hasOwnProperty('prototype') && !boundValue.hasOwnProperty('prototype')) {
|
|
39
|
+
// we should not use assignment operator to set boundValue prototype like `boundValue.prototype = value.prototype`
|
|
40
|
+
// as the assignment will also look up prototype chain while it hasn't own prototype property,
|
|
41
|
+
// when the lookup succeed, the assignment will throw an TypeError like `Cannot assign to read only property 'prototype' of function` if its descriptor configured with writable false or just have a getter accessor
|
|
42
|
+
// see https://github.com/umijs/qiankun/issues/1121
|
|
43
|
+
Object.defineProperty(boundValue, 'prototype', {
|
|
44
|
+
value: _value.prototype,
|
|
45
|
+
enumerable: false,
|
|
46
|
+
writable: true
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
// Some util, like `function isNative() { return typeof Ctor === 'function' && /native code/.test(Ctor.toString()) }` relies on the original `toString()` result
|
|
50
|
+
// but bound functions will always return "function() {[native code]}" for `toString`, which is misleading
|
|
51
|
+
if (typeof _value.toString === 'function') {
|
|
52
|
+
var valueHasInstanceToString = _value.hasOwnProperty('toString') && !boundValue.hasOwnProperty('toString');
|
|
53
|
+
var boundValueHasPrototypeToString = boundValue.toString === Function.prototype.toString;
|
|
54
|
+
if (valueHasInstanceToString || boundValueHasPrototypeToString) {
|
|
55
|
+
var originToStringDescriptor = Object.getOwnPropertyDescriptor(valueHasInstanceToString ? _value : Function.prototype, 'toString');
|
|
56
|
+
Object.defineProperty(boundValue, 'toString', Object.assign(Object.assign({}, originToStringDescriptor), (originToStringDescriptor === null || originToStringDescriptor === void 0 ? void 0 : originToStringDescriptor.get) ? null : {
|
|
57
|
+
value: function value() {
|
|
58
|
+
return _value.toString();
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
functionBoundedValueMap.set(_value, boundValue);
|
|
64
|
+
return boundValue;
|
|
65
|
+
}
|
|
66
|
+
return _value;
|
|
67
|
+
}
|
|
68
|
+
var getterInvocationResultMap = new WeakMap();
|
|
69
|
+
export function getProxyPropertyValue(getter) {
|
|
70
|
+
var getterResult = getterInvocationResultMap.get(getter);
|
|
71
|
+
if (!getterResult) {
|
|
72
|
+
var result = getter();
|
|
73
|
+
getterInvocationResultMap.set(getter, result);
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
return getterResult;
|
|
77
|
+
}
|
|
78
|
+
export function getZsLocalStorage(_proxyName) {
|
|
79
|
+
var proxyName = _proxyName.split('_')[0];
|
|
80
|
+
var fakeStorage = {
|
|
81
|
+
getItem: function getItem(key) {
|
|
82
|
+
var prefixedKey = proxyName + key;
|
|
83
|
+
return localStorage.getItem(prefixedKey);
|
|
84
|
+
},
|
|
85
|
+
setItem: function setItem(key, value) {
|
|
86
|
+
var prefixedKey = proxyName + key;
|
|
87
|
+
localStorage.setItem(prefixedKey, String(value));
|
|
88
|
+
},
|
|
89
|
+
key: function key(nth) {
|
|
90
|
+
var _a;
|
|
91
|
+
return (_a = localStorage.key(nth)) === null || _a === void 0 ? void 0 : _a.slice(proxyName.length);
|
|
92
|
+
},
|
|
93
|
+
removeItem: function removeItem(key) {
|
|
94
|
+
var prefixedKey = proxyName + key;
|
|
95
|
+
localStorage.removeItem(prefixedKey);
|
|
96
|
+
},
|
|
97
|
+
clear: function clear() {
|
|
98
|
+
var keysToRemove = [];
|
|
99
|
+
for (var i = 0; i < localStorage.length; i += 1) {
|
|
100
|
+
var key = localStorage.key(i);
|
|
101
|
+
if (key && key.startsWith(proxyName)) {
|
|
102
|
+
keysToRemove.push(key);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
keysToRemove.forEach(function (key) {
|
|
106
|
+
localStorage.removeItem(key);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var proxy = new Proxy({}, {
|
|
111
|
+
get: function get(_, p) {
|
|
112
|
+
var _a;
|
|
113
|
+
return (_a = fakeStorage[p]) !== null && _a !== void 0 ? _a : localStorage[p];
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
return proxy;
|
|
117
|
+
}
|
|
118
|
+
export function getZsSessionStorage(_proxyName) {
|
|
119
|
+
var proxyName = _proxyName.split('_')[0];
|
|
120
|
+
var fakeStorage = {
|
|
121
|
+
getItem: function getItem(key) {
|
|
122
|
+
var prefixedKey = proxyName + key;
|
|
123
|
+
return sessionStorage.getItem(prefixedKey);
|
|
124
|
+
},
|
|
125
|
+
key: function key(nth) {
|
|
126
|
+
var _a;
|
|
127
|
+
return (_a = sessionStorage.key(nth)) === null || _a === void 0 ? void 0 : _a.slice(proxyName.length);
|
|
128
|
+
},
|
|
129
|
+
setItem: function setItem(key, value) {
|
|
130
|
+
var prefixedKey = proxyName + key;
|
|
131
|
+
sessionStorage.setItem(prefixedKey, String(value));
|
|
132
|
+
},
|
|
133
|
+
removeItem: function removeItem(key) {
|
|
134
|
+
var prefixedKey = proxyName + key;
|
|
135
|
+
sessionStorage.removeItem(prefixedKey);
|
|
136
|
+
},
|
|
137
|
+
clear: function clear() {
|
|
138
|
+
var keysToRemove = [];
|
|
139
|
+
for (var i = 0; i < sessionStorage.length; i += 1) {
|
|
140
|
+
var key = sessionStorage.key(i);
|
|
141
|
+
if (key && key.startsWith(proxyName)) {
|
|
142
|
+
keysToRemove.push(key);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
keysToRemove.forEach(function (key) {
|
|
146
|
+
sessionStorage.removeItem(key);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
var proxy = new Proxy({}, {
|
|
151
|
+
get: function get(_, p) {
|
|
152
|
+
var _a;
|
|
153
|
+
return (_a = fakeStorage[p]) !== null && _a !== void 0 ? _a : sessionStorage[p];
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
return proxy;
|
|
157
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Kuitos
|
|
3
|
+
* @since 2019-04-11
|
|
4
|
+
*/
|
|
5
|
+
import type { SandBox } from '../interfaces';
|
|
6
|
+
export { css } from './patchers';
|
|
7
|
+
export { getCurrentRunningApp } from './common';
|
|
8
|
+
/**
|
|
9
|
+
* 生成应用运行时沙箱
|
|
10
|
+
*
|
|
11
|
+
* 沙箱分两个类型:
|
|
12
|
+
* 1. app 环境沙箱
|
|
13
|
+
* app 环境沙箱是指应用初始化过之后,应用会在什么样的上下文环境运行。每个应用的环境沙箱只会初始化一次,因为子应用只会触发一次 bootstrap 。
|
|
14
|
+
* 子应用在切换时,实际上切换的是 app 环境沙箱。
|
|
15
|
+
* 2. render 沙箱
|
|
16
|
+
* 子应用在 app mount 开始前生成好的的沙箱。每次子应用切换过后,render 沙箱都会重现初始化。
|
|
17
|
+
*
|
|
18
|
+
* 这么设计的目的是为了保证每个子应用切换回来之后,还能运行在应用 bootstrap 之后的环境下。
|
|
19
|
+
*
|
|
20
|
+
* @param appName
|
|
21
|
+
* @param elementGetter
|
|
22
|
+
* @param scopedCSS
|
|
23
|
+
* @param useLooseSandbox
|
|
24
|
+
* @param excludeAssetFilter
|
|
25
|
+
* @param globalContext
|
|
26
|
+
* @param transformStorage
|
|
27
|
+
*/
|
|
28
|
+
export declare function createSandboxContainer(appName: string, elementGetter: () => HTMLElement | ShadowRoot, scopedCSS: boolean, useLooseSandbox?: boolean, excludeAssetFilter?: (url: string) => boolean, globalContext?: typeof window, transformStorage?: boolean): {
|
|
29
|
+
instance: SandBox;
|
|
30
|
+
/**
|
|
31
|
+
* 沙箱被 mount
|
|
32
|
+
* 可能是从 bootstrap 状态进入的 mount
|
|
33
|
+
* 也可能是从 unmount 之后再次唤醒进入 mount
|
|
34
|
+
*/
|
|
35
|
+
mount(): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* 恢复 global 状态,使其能回到应用加载之前的状态
|
|
38
|
+
*/
|
|
39
|
+
unmount(): Promise<void>;
|
|
40
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
|
3
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
|
|
4
|
+
import { __awaiter } from "tslib";
|
|
5
|
+
import LegacySandbox from './legacy/sandbox';
|
|
6
|
+
import { patchAtBootstrapping, patchAtMounting } from './patchers';
|
|
7
|
+
import ProxySandbox from './proxySandbox';
|
|
8
|
+
import SnapshotSandbox from './snapshotSandbox';
|
|
9
|
+
export { css } from './patchers';
|
|
10
|
+
export { getCurrentRunningApp } from './common';
|
|
11
|
+
/**
|
|
12
|
+
* 生成应用运行时沙箱
|
|
13
|
+
*
|
|
14
|
+
* 沙箱分两个类型:
|
|
15
|
+
* 1. app 环境沙箱
|
|
16
|
+
* app 环境沙箱是指应用初始化过之后,应用会在什么样的上下文环境运行。每个应用的环境沙箱只会初始化一次,因为子应用只会触发一次 bootstrap 。
|
|
17
|
+
* 子应用在切换时,实际上切换的是 app 环境沙箱。
|
|
18
|
+
* 2. render 沙箱
|
|
19
|
+
* 子应用在 app mount 开始前生成好的的沙箱。每次子应用切换过后,render 沙箱都会重现初始化。
|
|
20
|
+
*
|
|
21
|
+
* 这么设计的目的是为了保证每个子应用切换回来之后,还能运行在应用 bootstrap 之后的环境下。
|
|
22
|
+
*
|
|
23
|
+
* @param appName
|
|
24
|
+
* @param elementGetter
|
|
25
|
+
* @param scopedCSS
|
|
26
|
+
* @param useLooseSandbox
|
|
27
|
+
* @param excludeAssetFilter
|
|
28
|
+
* @param globalContext
|
|
29
|
+
* @param transformStorage
|
|
30
|
+
*/
|
|
31
|
+
export function createSandboxContainer(appName, elementGetter, scopedCSS, useLooseSandbox, excludeAssetFilter, globalContext, transformStorage) {
|
|
32
|
+
var sandbox;
|
|
33
|
+
if (window.Proxy) {
|
|
34
|
+
sandbox = useLooseSandbox ? new LegacySandbox(appName, globalContext) : new ProxySandbox(appName, globalContext, transformStorage);
|
|
35
|
+
} else {
|
|
36
|
+
sandbox = new SnapshotSandbox(appName);
|
|
37
|
+
}
|
|
38
|
+
// some side effect could be be invoked while bootstrapping, such as dynamic stylesheet injection with style-loader, especially during the development phase
|
|
39
|
+
var bootstrappingFreers = patchAtBootstrapping(appName, elementGetter, sandbox, scopedCSS, excludeAssetFilter);
|
|
40
|
+
// mounting freers are one-off and should be re-init at every mounting time
|
|
41
|
+
var mountingFreers = [];
|
|
42
|
+
var sideEffectsRebuilders = [];
|
|
43
|
+
return {
|
|
44
|
+
instance: sandbox,
|
|
45
|
+
/**
|
|
46
|
+
* 沙箱被 mount
|
|
47
|
+
* 可能是从 bootstrap 状态进入的 mount
|
|
48
|
+
* 也可能是从 unmount 之后再次唤醒进入 mount
|
|
49
|
+
*/
|
|
50
|
+
mount: function mount() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator().m(function _callee() {
|
|
52
|
+
var sideEffectsRebuildersAtBootstrapping, sideEffectsRebuildersAtMounting;
|
|
53
|
+
return _regenerator().w(function (_context) {
|
|
54
|
+
while (1) switch (_context.n) {
|
|
55
|
+
case 0:
|
|
56
|
+
/* ------------------------------------------ 因为有上下文依赖(window),以下代码执行顺序不能变 ------------------------------------------ */
|
|
57
|
+
/* ------------------------------------------ 1. 启动/恢复 沙箱------------------------------------------ */
|
|
58
|
+
sandbox.active();
|
|
59
|
+
sideEffectsRebuildersAtBootstrapping = sideEffectsRebuilders.slice(0, bootstrappingFreers.length);
|
|
60
|
+
sideEffectsRebuildersAtMounting = sideEffectsRebuilders.slice(bootstrappingFreers.length); // must rebuild the side effects which added at bootstrapping firstly to recovery to nature state
|
|
61
|
+
if (sideEffectsRebuildersAtBootstrapping.length) {
|
|
62
|
+
sideEffectsRebuildersAtBootstrapping.forEach(function (rebuild) {
|
|
63
|
+
return rebuild();
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/* ------------------------------------------ 2. 开启全局变量补丁 ------------------------------------------*/
|
|
67
|
+
// render 沙箱启动时开始劫持各类全局监听,尽量不要在应用初始化阶段有 事件监听/定时器 等副作用
|
|
68
|
+
mountingFreers = patchAtMounting(appName, elementGetter, sandbox, scopedCSS, excludeAssetFilter);
|
|
69
|
+
/* ------------------------------------------ 3. 重置一些初始化时的副作用 ------------------------------------------*/
|
|
70
|
+
// 存在 rebuilder 则表明有些副作用需要重建
|
|
71
|
+
if (sideEffectsRebuildersAtMounting.length) {
|
|
72
|
+
sideEffectsRebuildersAtMounting.forEach(function (rebuild) {
|
|
73
|
+
return rebuild();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
// clean up rebuilders
|
|
77
|
+
sideEffectsRebuilders = [];
|
|
78
|
+
case 1:
|
|
79
|
+
return _context.a(2);
|
|
80
|
+
}
|
|
81
|
+
}, _callee);
|
|
82
|
+
}));
|
|
83
|
+
},
|
|
84
|
+
/**
|
|
85
|
+
* 恢复 global 状态,使其能回到应用加载之前的状态
|
|
86
|
+
*/
|
|
87
|
+
unmount: function unmount() {
|
|
88
|
+
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator().m(function _callee2() {
|
|
89
|
+
return _regenerator().w(function (_context2) {
|
|
90
|
+
while (1) switch (_context2.n) {
|
|
91
|
+
case 0:
|
|
92
|
+
// record the rebuilders of window side effects (event listeners or timers)
|
|
93
|
+
// note that the frees of mounting phase are one-off as it will be re-init at next mounting
|
|
94
|
+
sideEffectsRebuilders = [].concat(_toConsumableArray(bootstrappingFreers), _toConsumableArray(mountingFreers)).map(function (free) {
|
|
95
|
+
return free();
|
|
96
|
+
});
|
|
97
|
+
sandbox.inactive();
|
|
98
|
+
case 1:
|
|
99
|
+
return _context2.a(2);
|
|
100
|
+
}
|
|
101
|
+
}, _callee2);
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Kuitos
|
|
3
|
+
* @since 2019-04-11
|
|
4
|
+
*/
|
|
5
|
+
import type { SandBox } from '../../interfaces';
|
|
6
|
+
import { SandBoxType } from '../../interfaces';
|
|
7
|
+
/**
|
|
8
|
+
* 基于 Proxy 实现的沙箱
|
|
9
|
+
* TODO: 为了兼容性 singular 模式下依旧使用该沙箱,等新沙箱稳定之后再切换
|
|
10
|
+
*/
|
|
11
|
+
export default class LegacySandbox implements SandBox {
|
|
12
|
+
/** 沙箱期间新增的全局变量 */
|
|
13
|
+
private addedPropsMapInSandbox;
|
|
14
|
+
/** 沙箱期间更新的全局变量 */
|
|
15
|
+
private modifiedPropsOriginalValueMapInSandbox;
|
|
16
|
+
/** 持续记录更新的(新增和修改的)全局变量的 map,用于在任意时刻做 snapshot */
|
|
17
|
+
private currentUpdatedPropsValueMap;
|
|
18
|
+
name: string;
|
|
19
|
+
proxy: WindowProxy;
|
|
20
|
+
globalContext: typeof window;
|
|
21
|
+
type: SandBoxType;
|
|
22
|
+
sandboxRunning: boolean;
|
|
23
|
+
latestSetProp: PropertyKey | null;
|
|
24
|
+
private setWindowProp;
|
|
25
|
+
active(): void;
|
|
26
|
+
inactive(): void;
|
|
27
|
+
constructor(name: string, globalContext?: Window & typeof globalThis);
|
|
28
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
3
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
4
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
5
|
+
import { SandBoxType } from '../../interfaces';
|
|
6
|
+
import { getTargetValue } from '../common';
|
|
7
|
+
function isPropConfigurable(target, prop) {
|
|
8
|
+
var descriptor = Object.getOwnPropertyDescriptor(target, prop);
|
|
9
|
+
return descriptor ? descriptor.configurable : true;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 基于 Proxy 实现的沙箱
|
|
13
|
+
* TODO: 为了兼容性 singular 模式下依旧使用该沙箱,等新沙箱稳定之后再切换
|
|
14
|
+
*/
|
|
15
|
+
var LegacySandbox = /*#__PURE__*/function () {
|
|
16
|
+
function LegacySandbox(name) {
|
|
17
|
+
var _this = this;
|
|
18
|
+
var globalContext = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window;
|
|
19
|
+
_classCallCheck(this, LegacySandbox);
|
|
20
|
+
/** 沙箱期间新增的全局变量 */
|
|
21
|
+
this.addedPropsMapInSandbox = new Map();
|
|
22
|
+
/** 沙箱期间更新的全局变量 */
|
|
23
|
+
this.modifiedPropsOriginalValueMapInSandbox = new Map();
|
|
24
|
+
/** 持续记录更新的(新增和修改的)全局变量的 map,用于在任意时刻做 snapshot */
|
|
25
|
+
this.currentUpdatedPropsValueMap = new Map();
|
|
26
|
+
this.sandboxRunning = true;
|
|
27
|
+
this.latestSetProp = null;
|
|
28
|
+
this.name = name;
|
|
29
|
+
this.globalContext = globalContext;
|
|
30
|
+
this.type = SandBoxType.LegacyProxy;
|
|
31
|
+
var addedPropsMapInSandbox = this.addedPropsMapInSandbox,
|
|
32
|
+
modifiedPropsOriginalValueMapInSandbox = this.modifiedPropsOriginalValueMapInSandbox,
|
|
33
|
+
currentUpdatedPropsValueMap = this.currentUpdatedPropsValueMap;
|
|
34
|
+
var rawWindow = globalContext;
|
|
35
|
+
var fakeWindow = Object.create(null);
|
|
36
|
+
var setTrap = function setTrap(p, value, originalValue) {
|
|
37
|
+
var sync2Window = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
|
|
38
|
+
if (_this.sandboxRunning) {
|
|
39
|
+
if (!rawWindow.hasOwnProperty(p)) {
|
|
40
|
+
addedPropsMapInSandbox.set(p, value);
|
|
41
|
+
} else if (!modifiedPropsOriginalValueMapInSandbox.has(p)) {
|
|
42
|
+
// 如果当前 window 对象存在该属性,且 record map 中未记录过,则记录该属性初始值
|
|
43
|
+
modifiedPropsOriginalValueMapInSandbox.set(p, originalValue);
|
|
44
|
+
}
|
|
45
|
+
currentUpdatedPropsValueMap.set(p, value);
|
|
46
|
+
if (sync2Window) {
|
|
47
|
+
// 必须重新设置 window 对象保证下次 get 时能拿到已更新的数据
|
|
48
|
+
rawWindow[p] = value;
|
|
49
|
+
}
|
|
50
|
+
_this.latestSetProp = p;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (process.env.NODE_ENV === 'development') {
|
|
54
|
+
console.warn("[qiankun] Set window.".concat(p.toString(), " while sandbox destroyed or inactive in ").concat(name, "!"));
|
|
55
|
+
}
|
|
56
|
+
// 在 strict-mode 下,Proxy 的 handler.set 返回 false 会抛出 TypeError,在沙箱卸载的情况下应该忽略错误
|
|
57
|
+
return true;
|
|
58
|
+
};
|
|
59
|
+
var proxy = new Proxy(fakeWindow, {
|
|
60
|
+
set: function set(_, p, value) {
|
|
61
|
+
var originalValue = rawWindow[p];
|
|
62
|
+
return setTrap(p, value, originalValue, true);
|
|
63
|
+
},
|
|
64
|
+
get: function get(_, p) {
|
|
65
|
+
// avoid who using window.window or window.self to escape the sandbox environment to touch the really window
|
|
66
|
+
// or use window.top to check if an iframe context
|
|
67
|
+
// see https://github.com/eligrey/FileSaver.js/blob/master/src/FileSaver.js#L13
|
|
68
|
+
if (p === 'top' || p === 'parent' || p === 'window' || p === 'self') {
|
|
69
|
+
return proxy;
|
|
70
|
+
}
|
|
71
|
+
var value = rawWindow[p];
|
|
72
|
+
return getTargetValue(rawWindow, value);
|
|
73
|
+
},
|
|
74
|
+
// trap in operator
|
|
75
|
+
// see https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/constants.js#L12
|
|
76
|
+
has: function has(_, p) {
|
|
77
|
+
return p in rawWindow;
|
|
78
|
+
},
|
|
79
|
+
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(_, p) {
|
|
80
|
+
var descriptor = Object.getOwnPropertyDescriptor(rawWindow, p);
|
|
81
|
+
// A property cannot be reported as non-configurable, if it does not exists as an own property of the target object
|
|
82
|
+
if (descriptor && !descriptor.configurable) {
|
|
83
|
+
descriptor.configurable = true;
|
|
84
|
+
}
|
|
85
|
+
return descriptor;
|
|
86
|
+
},
|
|
87
|
+
defineProperty: function defineProperty(_, p, attributes) {
|
|
88
|
+
var originalValue = rawWindow[p];
|
|
89
|
+
var done = Reflect.defineProperty(rawWindow, p, attributes);
|
|
90
|
+
var value = rawWindow[p];
|
|
91
|
+
setTrap(p, value, originalValue, false);
|
|
92
|
+
return done;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
this.proxy = proxy;
|
|
96
|
+
}
|
|
97
|
+
return _createClass(LegacySandbox, [{
|
|
98
|
+
key: "setWindowProp",
|
|
99
|
+
value: function setWindowProp(prop, value, toDelete) {
|
|
100
|
+
if (value === undefined && toDelete) {
|
|
101
|
+
// eslint-disable-next-line no-param-reassign
|
|
102
|
+
delete this.globalContext[prop];
|
|
103
|
+
} else if (isPropConfigurable(this.globalContext, prop) && _typeof(prop) !== 'symbol') {
|
|
104
|
+
Object.defineProperty(this.globalContext, prop, {
|
|
105
|
+
writable: true,
|
|
106
|
+
configurable: true
|
|
107
|
+
});
|
|
108
|
+
// eslint-disable-next-line no-param-reassign
|
|
109
|
+
this.globalContext[prop] = value;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}, {
|
|
113
|
+
key: "active",
|
|
114
|
+
value: function active() {
|
|
115
|
+
var _this2 = this;
|
|
116
|
+
if (!this.sandboxRunning) {
|
|
117
|
+
this.currentUpdatedPropsValueMap.forEach(function (v, p) {
|
|
118
|
+
return _this2.setWindowProp(p, v);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
this.sandboxRunning = true;
|
|
122
|
+
}
|
|
123
|
+
}, {
|
|
124
|
+
key: "inactive",
|
|
125
|
+
value: function inactive() {
|
|
126
|
+
var _this3 = this;
|
|
127
|
+
if (process.env.NODE_ENV === 'development') {
|
|
128
|
+
console.info("[qiankun:sandbox] ".concat(this.name, " modified global properties restore..."), [].concat(_toConsumableArray(this.addedPropsMapInSandbox.keys()), _toConsumableArray(this.modifiedPropsOriginalValueMapInSandbox.keys())));
|
|
129
|
+
}
|
|
130
|
+
// renderSandboxSnapshot = snapshot(currentUpdatedPropsValueMapForSnapshot);
|
|
131
|
+
// restore global props to initial snapshot
|
|
132
|
+
this.modifiedPropsOriginalValueMapInSandbox.forEach(function (v, p) {
|
|
133
|
+
return _this3.setWindowProp(p, v);
|
|
134
|
+
});
|
|
135
|
+
this.addedPropsMapInSandbox.forEach(function (_, p) {
|
|
136
|
+
return _this3.setWindowProp(p, undefined, true);
|
|
137
|
+
});
|
|
138
|
+
this.sandboxRunning = false;
|
|
139
|
+
}
|
|
140
|
+
}]);
|
|
141
|
+
}();
|
|
142
|
+
export { LegacySandbox as default };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Saviio
|
|
3
|
+
* @since 2020-4-19
|
|
4
|
+
*/
|
|
5
|
+
export declare class ScopedCSS {
|
|
6
|
+
private static ModifiedTag;
|
|
7
|
+
private sheet;
|
|
8
|
+
private swapNode;
|
|
9
|
+
constructor();
|
|
10
|
+
process(styleNode: HTMLStyleElement, prefix?: string): void;
|
|
11
|
+
private rewrite;
|
|
12
|
+
private ruleStyle;
|
|
13
|
+
private ruleMedia;
|
|
14
|
+
private ruleSupport;
|
|
15
|
+
}
|
|
16
|
+
export declare const QiankunCSSRewriteAttr = "data-qiankun";
|
|
17
|
+
export declare const process: (appWrapper: HTMLElement, stylesheetElement: HTMLStyleElement | HTMLLinkElement, appName: string) => void;
|