@micro-zoe/micro-app 1.0.0-rc.31 → 1.0.0-rc.33
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/lib/index.esm.js +174 -95
- package/lib/index.esm.js.map +1 -1
- package/lib/index.min.js +1 -1
- package/lib/index.min.js.map +1 -1
- package/lib/index.umd.js +1 -1
- package/lib/index.umd.js.map +1 -1
- package/package.json +1 -1
package/lib/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const version = '1.0.0-rc.
|
|
1
|
+
const version = '1.0.0-rc.33';
|
|
2
2
|
// do not use isUndefined
|
|
3
3
|
const isBrowser = typeof window !== 'undefined';
|
|
4
4
|
// do not use isUndefined
|
|
@@ -65,13 +65,26 @@ function isBoundFunction(target) {
|
|
|
65
65
|
}
|
|
66
66
|
// is constructor function
|
|
67
67
|
function isConstructor(target) {
|
|
68
|
-
var _a;
|
|
69
68
|
if (isFunction(target)) {
|
|
70
69
|
const targetStr = Function.prototype.toString.call(target);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
if (/^class\s+/.test(targetStr) || /^function\s+[A-Z]/.test(targetStr)) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
const proto = target.prototype;
|
|
74
|
+
if ((proto === null || proto === void 0 ? void 0 : proto.constructor) === target) {
|
|
75
|
+
/**
|
|
76
|
+
* timestamp: 2026/09/16
|
|
77
|
+
* 1. own members on prototype (besides constructor) -> constructor
|
|
78
|
+
* 2. prototype inherits from a non-Object prototype (e.g. prototype = Object.create(ParentProto)),
|
|
79
|
+
* means members live upstream on the prototype chain -> still a constructor.
|
|
80
|
+
* Anonymous proxy constructors like risk-control's `win.XMLHttpRequest = function () {...}`
|
|
81
|
+
* with `prototype = Object.create(realXHRProto)` fall into this case; misjudging them as
|
|
82
|
+
* plain functions makes bindFunctionToRawTarget bind them and breaks `new` (open becomes undefined).
|
|
83
|
+
*/
|
|
84
|
+
return Object.getOwnPropertyNames(proto).length > 1 ||
|
|
85
|
+
Object.getPrototypeOf(proto) !== Object.prototype;
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
75
88
|
}
|
|
76
89
|
return false;
|
|
77
90
|
}
|
|
@@ -249,13 +262,15 @@ function formatAppURL(url, appName = null) {
|
|
|
249
262
|
if (!isString(url) || !url)
|
|
250
263
|
return '';
|
|
251
264
|
try {
|
|
252
|
-
const { origin, pathname, search } = createURL(addProtocol(url), (window.rawWindow || window).location.href);
|
|
265
|
+
const { protocol, origin: rawOrigin, host, pathname, search } = createURL(addProtocol(url), (window.rawWindow || window).location.href);
|
|
253
266
|
/**
|
|
254
267
|
* keep the original url unchanged, such as .html .node .php .net .etc, search, except hash
|
|
255
268
|
* BUG FIX: Never using '/' to complete url, refer to https://github.com/jd-opensource/micro-app/issues/1147
|
|
269
|
+
* FEATURE: URL.origin is browser-dependent for file protocol url, refer to https://github.com/jd-opensource/micro-app/pull/1695
|
|
256
270
|
*/
|
|
271
|
+
const origin = protocol === 'file:' ? `${protocol}//${host}` : rawOrigin;
|
|
257
272
|
const fullPath = `${origin}${pathname}${search}`;
|
|
258
|
-
return /^https
|
|
273
|
+
return /^(https?|file):\/\//.test(fullPath) ? fullPath : '';
|
|
259
274
|
}
|
|
260
275
|
catch (e) {
|
|
261
276
|
logError(e, appName);
|
|
@@ -284,7 +299,11 @@ function formatAppName(name) {
|
|
|
284
299
|
* @param url app.url
|
|
285
300
|
*/
|
|
286
301
|
function getEffectivePath(url) {
|
|
287
|
-
const { origin, pathname } = createURL(url);
|
|
302
|
+
const { protocol, origin: rawOrigin, host, pathname } = createURL(url);
|
|
303
|
+
/*
|
|
304
|
+
* FEATURE: URL.origin is browser-dependent for file protocol url, refer to https://github.com/jd-opensource/micro-app/pull/1695
|
|
305
|
+
*/
|
|
306
|
+
const origin = protocol === 'file:' ? `${protocol}//${host}` : rawOrigin;
|
|
288
307
|
if (/\.(\w+)$/.test(pathname)) {
|
|
289
308
|
const pathArr = `${origin}${pathname}`.split('/');
|
|
290
309
|
pathArr.pop();
|
|
@@ -834,6 +853,40 @@ function parseError(msg, linkPath) {
|
|
|
834
853
|
}
|
|
835
854
|
throw err;
|
|
836
855
|
}
|
|
856
|
+
// 仅在顶层(不在 () 或 [] 内)的逗号处分割选择器列表。
|
|
857
|
+
// () / [] 内的逗号属于 :is()/:not()/:where()/:has()/:nth-child(of) 或属性值,不是分隔符。
|
|
858
|
+
// 每段保留原分隔符(逗号+其后空白),无括号的选择器重组后与原输出逐字节一致。
|
|
859
|
+
function splitTopLevelSelectors(text) {
|
|
860
|
+
const segments = [];
|
|
861
|
+
let depth = 0;
|
|
862
|
+
let bodyStart = 0;
|
|
863
|
+
let separator = '';
|
|
864
|
+
let i = 0;
|
|
865
|
+
const len = text.length;
|
|
866
|
+
while (i < len) {
|
|
867
|
+
const ch = text.charAt(i);
|
|
868
|
+
if (ch === '(' || ch === '[') {
|
|
869
|
+
depth++;
|
|
870
|
+
}
|
|
871
|
+
else if (ch === ')' || ch === ']') {
|
|
872
|
+
if (depth > 0)
|
|
873
|
+
depth--;
|
|
874
|
+
}
|
|
875
|
+
else if (ch === ',' && depth === 0) {
|
|
876
|
+
segments.push({ separator, body: text.slice(bodyStart, i) });
|
|
877
|
+
let j = i + 1;
|
|
878
|
+
while (j < len && /\s/.test(text.charAt(j)))
|
|
879
|
+
j++;
|
|
880
|
+
separator = text.slice(i, j);
|
|
881
|
+
bodyStart = j;
|
|
882
|
+
i = j;
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
i++;
|
|
886
|
+
}
|
|
887
|
+
segments.push({ separator, body: text.slice(bodyStart) });
|
|
888
|
+
return segments;
|
|
889
|
+
}
|
|
837
890
|
/**
|
|
838
891
|
* Reference https://github.com/reworkcss/css
|
|
839
892
|
* CSSParser mainly deals with 3 scenes: styleRule, @, and comment
|
|
@@ -925,21 +978,13 @@ class CSSParser {
|
|
|
925
978
|
* should be ==> micro-app[name=xxx] :is(.a, .b, .c) a {}
|
|
926
979
|
* 6. :where(.a, .b, .c) a {}
|
|
927
980
|
* should be ==> micro-app[name=xxx] :where(.a, .b, .c) a {}
|
|
981
|
+
*
|
|
982
|
+
* 仅在顶层逗号处分割(括号深度感知),再逐段加前缀。
|
|
983
|
+
* 旧的 `/(^|,[\n\s]*)([^,]+)/g` 会在每个逗号处分割,把嵌套在
|
|
984
|
+
* :is()/:not()/:where()/:has()/[attr] 内的逗号拆成非法 CSS。
|
|
928
985
|
*/
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
const mock = `__mock_${p1}_${offset}Value__`;
|
|
932
|
-
attributeValues[mock] = p2;
|
|
933
|
-
return match.replace(p2, mock);
|
|
934
|
-
});
|
|
935
|
-
return matchRes.replace(/(^|,[\n\s]*)([^,]+)/g, (_, separator, selector) => {
|
|
936
|
-
selector = trim(selector);
|
|
937
|
-
selector = selector.replace(/\[[^\]=]+(?:=([^\]]+))?\]/g, (match, p1) => {
|
|
938
|
-
if (attributeValues[p1]) {
|
|
939
|
-
return match.replace(p1, attributeValues[p1]);
|
|
940
|
-
}
|
|
941
|
-
return match;
|
|
942
|
-
});
|
|
986
|
+
return splitTopLevelSelectors(m[0]).map(({ separator, body }) => {
|
|
987
|
+
let selector = trim(body);
|
|
943
988
|
// Check if it is in the optionCss list
|
|
944
989
|
const isIsolationRoot = this.optionCssSelectors.some(includeSelector => {
|
|
945
990
|
// isolation-root is used to isolation the :root styles between base-app and sub-app
|
|
@@ -960,7 +1005,7 @@ class CSSParser {
|
|
|
960
1005
|
}
|
|
961
1006
|
}
|
|
962
1007
|
return separator + selector;
|
|
963
|
-
});
|
|
1008
|
+
}).join('');
|
|
964
1009
|
}
|
|
965
1010
|
// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration
|
|
966
1011
|
styleDeclarations() {
|
|
@@ -977,7 +1022,7 @@ class CSSParser {
|
|
|
977
1022
|
if (!this.scopecssDisableNextLine &&
|
|
978
1023
|
(!this.scopecssDisable || this.scopecssDisableSelectors.length)) {
|
|
979
1024
|
cssValue = cssValue.replace(/url\((["']?)(.*?)\1\)/gm, (all, _, $1) => {
|
|
980
|
-
if (/^((data|blob):|#|%23)/.test($1) || /^(https
|
|
1025
|
+
if (/^((data|blob):|#|%23)/.test($1) || /^((https?|file):)?\/\//.test($1)) {
|
|
981
1026
|
return all;
|
|
982
1027
|
}
|
|
983
1028
|
// ./a/b.png ../a/b.png a/b.png
|
|
@@ -3418,6 +3463,95 @@ function createMicroDocument(appName, proxyDocument) {
|
|
|
3418
3463
|
return MicroDocument;
|
|
3419
3464
|
}
|
|
3420
3465
|
|
|
3466
|
+
const EXCLUDE_URL_PROTOCOLS = [
|
|
3467
|
+
'blob:'
|
|
3468
|
+
];
|
|
3469
|
+
// 重写 Worker 构造函数的类型
|
|
3470
|
+
const originalWorker = window.Worker;
|
|
3471
|
+
// create a proxy for the Worker constructor to intercept the creation of new Worker instances
|
|
3472
|
+
const workerInstanceMap = new Map();
|
|
3473
|
+
// release all workers of the app after sandboxes destoried.
|
|
3474
|
+
function releaseWorkersByApp(appName) {
|
|
3475
|
+
var _a;
|
|
3476
|
+
(_a = workerInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.forEach(w => w.terminate());
|
|
3477
|
+
workerInstanceMap.delete(appName);
|
|
3478
|
+
}
|
|
3479
|
+
function isSameOrigin(url) {
|
|
3480
|
+
try {
|
|
3481
|
+
// 检查 URL 是否与当前页面在同一个源
|
|
3482
|
+
const parsedUrl = url instanceof URL ? url : new URL(url);
|
|
3483
|
+
if (EXCLUDE_URL_PROTOCOLS.includes(parsedUrl.protocol)) {
|
|
3484
|
+
return true;
|
|
3485
|
+
}
|
|
3486
|
+
return (parsedUrl.protocol === window.location.protocol &&
|
|
3487
|
+
parsedUrl.hostname === window.location.hostname &&
|
|
3488
|
+
parsedUrl.port === window.location.port);
|
|
3489
|
+
}
|
|
3490
|
+
catch (error) {
|
|
3491
|
+
return false;
|
|
3492
|
+
}
|
|
3493
|
+
}
|
|
3494
|
+
function urlFromScript(script) {
|
|
3495
|
+
let blob;
|
|
3496
|
+
try {
|
|
3497
|
+
blob = new Blob([script], {
|
|
3498
|
+
type: 'application/javascript'
|
|
3499
|
+
});
|
|
3500
|
+
}
|
|
3501
|
+
catch (e) {
|
|
3502
|
+
const BlobBuilder =
|
|
3503
|
+
// @ts-ignore
|
|
3504
|
+
window.BlobBuilder ||
|
|
3505
|
+
// @ts-ignore
|
|
3506
|
+
window.WebKitBlobBuilder ||
|
|
3507
|
+
// @ts-ignore
|
|
3508
|
+
window.MozBlobBuilder ||
|
|
3509
|
+
// @ts-ignore
|
|
3510
|
+
window.MSBlobBuilder;
|
|
3511
|
+
const blobBuilder = new BlobBuilder();
|
|
3512
|
+
blobBuilder.append(script);
|
|
3513
|
+
blob = blobBuilder.getBlob('application/javascript');
|
|
3514
|
+
}
|
|
3515
|
+
const URL = window.URL || window.webkitURL;
|
|
3516
|
+
return URL.createObjectURL(blob);
|
|
3517
|
+
}
|
|
3518
|
+
// @ts-ignore
|
|
3519
|
+
const WorkerProxy = new Proxy(originalWorker, {
|
|
3520
|
+
construct(Target, args) {
|
|
3521
|
+
let [scriptURL, options] = args;
|
|
3522
|
+
options = options || {};
|
|
3523
|
+
const appName = getCurrentAppName();
|
|
3524
|
+
let url = scriptURL;
|
|
3525
|
+
if (appName) {
|
|
3526
|
+
const app = appInstanceMap.get(appName);
|
|
3527
|
+
url = CompletionPath(scriptURL, app.url);
|
|
3528
|
+
}
|
|
3529
|
+
let instance;
|
|
3530
|
+
if (url && !isSameOrigin(url)) {
|
|
3531
|
+
// 如果 scriptURL 是跨域的,使用 Blob URL 加载并执行 worker
|
|
3532
|
+
const workerScriptURL = JSON.stringify(String(url));
|
|
3533
|
+
// check if the type of worker is module or classic
|
|
3534
|
+
const script = options.type === 'module'
|
|
3535
|
+
? `import ${workerScriptURL};`
|
|
3536
|
+
: `importScripts(${workerScriptURL});`;
|
|
3537
|
+
const workerPath = urlFromScript(script);
|
|
3538
|
+
instance = new Target(workerPath, options);
|
|
3539
|
+
}
|
|
3540
|
+
else {
|
|
3541
|
+
// 如果 scriptURL 是同源的,直接使用原生的 Worker 构造函数
|
|
3542
|
+
instance = new Target(scriptURL, options);
|
|
3543
|
+
}
|
|
3544
|
+
// register worker instance to app
|
|
3545
|
+
if (appName) {
|
|
3546
|
+
if (!workerInstanceMap.has(appName)) {
|
|
3547
|
+
workerInstanceMap.set(appName, new Set());
|
|
3548
|
+
}
|
|
3549
|
+
workerInstanceMap.get(appName).add(instance);
|
|
3550
|
+
}
|
|
3551
|
+
return instance;
|
|
3552
|
+
},
|
|
3553
|
+
});
|
|
3554
|
+
|
|
3421
3555
|
/**
|
|
3422
3556
|
* patch window of child app
|
|
3423
3557
|
* @param appName app name
|
|
@@ -3464,9 +3598,18 @@ function patchWindowProperty(microAppWindow) {
|
|
|
3464
3598
|
function createProxyWindow(appName, microAppWindow, sandbox) {
|
|
3465
3599
|
const rawWindow = globalEnv.rawWindow;
|
|
3466
3600
|
const descriptorTargetMap = new Map();
|
|
3601
|
+
rawDefineProperty(microAppWindow, 'Worker', {
|
|
3602
|
+
value: WorkerProxy,
|
|
3603
|
+
configurable: true,
|
|
3604
|
+
writable: true,
|
|
3605
|
+
});
|
|
3467
3606
|
const proxyWindow = new Proxy(microAppWindow, {
|
|
3468
3607
|
get: (target, key) => {
|
|
3469
3608
|
throttleDeferForSetAppName(appName);
|
|
3609
|
+
// return WorkerProxy if child app accesses window.Worker
|
|
3610
|
+
if (key === 'Worker') {
|
|
3611
|
+
return WorkerProxy;
|
|
3612
|
+
}
|
|
3470
3613
|
if (Reflect.has(target, key) ||
|
|
3471
3614
|
(isString(key) && /^__MICRO_APP_/.test(key)) ||
|
|
3472
3615
|
includes(sandbox.scopeProperties, key)) {
|
|
@@ -5162,6 +5305,7 @@ class WithSandBox extends BaseSandbox {
|
|
|
5162
5305
|
*/
|
|
5163
5306
|
if (!umdMode || destroy) {
|
|
5164
5307
|
clearMicroEventSource(this.microAppWindow.__MICRO_APP_NAME__);
|
|
5308
|
+
releaseWorkersByApp(this.microAppWindow.__MICRO_APP_NAME__);
|
|
5165
5309
|
this.injectedKeys.forEach((key) => {
|
|
5166
5310
|
Reflect.deleteProperty(this.microAppWindow, key);
|
|
5167
5311
|
});
|
|
@@ -5654,75 +5798,6 @@ const proxy2RawDocumentMethods = [
|
|
|
5654
5798
|
'prepend',
|
|
5655
5799
|
];
|
|
5656
5800
|
|
|
5657
|
-
const EXCLUDE_URL_PROTOCOLS = [
|
|
5658
|
-
'blob:'
|
|
5659
|
-
];
|
|
5660
|
-
// 重写 Worker 构造函数的类型
|
|
5661
|
-
const originalWorker = window.Worker;
|
|
5662
|
-
function isSameOrigin(url) {
|
|
5663
|
-
try {
|
|
5664
|
-
// 检查 URL 是否与当前页面在同一个源
|
|
5665
|
-
const parsedUrl = url instanceof URL ? url : new URL(url);
|
|
5666
|
-
if (EXCLUDE_URL_PROTOCOLS.includes(parsedUrl.protocol)) {
|
|
5667
|
-
return true;
|
|
5668
|
-
}
|
|
5669
|
-
return (parsedUrl.protocol === window.location.protocol &&
|
|
5670
|
-
parsedUrl.hostname === window.location.hostname &&
|
|
5671
|
-
parsedUrl.port === window.location.port);
|
|
5672
|
-
}
|
|
5673
|
-
catch (error) {
|
|
5674
|
-
return false;
|
|
5675
|
-
}
|
|
5676
|
-
}
|
|
5677
|
-
function urlFromScript(script) {
|
|
5678
|
-
let blob;
|
|
5679
|
-
try {
|
|
5680
|
-
blob = new Blob([script], {
|
|
5681
|
-
type: 'application/javascript'
|
|
5682
|
-
});
|
|
5683
|
-
}
|
|
5684
|
-
catch (e) {
|
|
5685
|
-
const BlobBuilder =
|
|
5686
|
-
// @ts-ignore
|
|
5687
|
-
window.BlobBuilder ||
|
|
5688
|
-
// @ts-ignore
|
|
5689
|
-
window.WebKitBlobBuilder ||
|
|
5690
|
-
// @ts-ignore
|
|
5691
|
-
window.MozBlobBuilder ||
|
|
5692
|
-
// @ts-ignore
|
|
5693
|
-
window.MSBlobBuilder;
|
|
5694
|
-
const blobBuilder = new BlobBuilder();
|
|
5695
|
-
blobBuilder.append(script);
|
|
5696
|
-
blob = blobBuilder.getBlob('application/javascript');
|
|
5697
|
-
}
|
|
5698
|
-
const URL = window.URL || window.webkitURL;
|
|
5699
|
-
return URL.createObjectURL(blob);
|
|
5700
|
-
}
|
|
5701
|
-
// @ts-ignore
|
|
5702
|
-
const WorkerProxy = new Proxy(originalWorker, {
|
|
5703
|
-
construct(Target, args) {
|
|
5704
|
-
let [scriptURL, options] = args;
|
|
5705
|
-
options = options || {};
|
|
5706
|
-
const appName = getCurrentAppName();
|
|
5707
|
-
let url = scriptURL;
|
|
5708
|
-
if (appName) {
|
|
5709
|
-
const app = appInstanceMap.get(appName);
|
|
5710
|
-
url = CompletionPath(scriptURL, app.url);
|
|
5711
|
-
}
|
|
5712
|
-
if (url && !isSameOrigin(url)) {
|
|
5713
|
-
// 如果 scriptURL 是跨域的,使用 Blob URL 加载并执行 worker
|
|
5714
|
-
const script = `import "${scriptURL}";`;
|
|
5715
|
-
const workerPath = urlFromScript(script);
|
|
5716
|
-
options.type = 'module';
|
|
5717
|
-
return new Target(workerPath, options);
|
|
5718
|
-
}
|
|
5719
|
-
else {
|
|
5720
|
-
// 如果 scriptURL 是同源的,直接使用原生的 Worker 构造函数
|
|
5721
|
-
return new Target(scriptURL, options);
|
|
5722
|
-
}
|
|
5723
|
-
},
|
|
5724
|
-
});
|
|
5725
|
-
|
|
5726
5801
|
/**
|
|
5727
5802
|
* patch window of child app
|
|
5728
5803
|
* @param appName app name
|
|
@@ -6749,6 +6824,7 @@ class IframeSandbox {
|
|
|
6749
6824
|
if (!umdMode || destroy) {
|
|
6750
6825
|
this.deleteIframeElement();
|
|
6751
6826
|
this.clearHijackUmdHooks();
|
|
6827
|
+
releaseWorkersByApp(this.microAppWindow.__MICRO_APP_NAME__);
|
|
6752
6828
|
}
|
|
6753
6829
|
if (--globalEnv.activeSandbox === 0) {
|
|
6754
6830
|
releasePatchElementAndDocument();
|
|
@@ -7934,7 +8010,10 @@ function patchElementAndDocument() {
|
|
|
7934
8010
|
while (i < nodes.length) {
|
|
7935
8011
|
let node = nodes[i];
|
|
7936
8012
|
node = isNode(node) ? node : globalEnv.rawCreateTextNode.call(globalEnv.rawDocument, node);
|
|
7937
|
-
commonElementHandler(this, markElement(node), null,
|
|
8013
|
+
commonElementHandler(this, markElement(node), null,
|
|
8014
|
+
// ShadowRoot 的 append 继承自 DocumentFragment.prototype,需用 rawFragmentAppend;
|
|
8015
|
+
// 若用 rawAppend(Element.prototype.append) 调用会抛 Illegal invocation
|
|
8016
|
+
isDocumentFragment(this) || isDocumentShadowRoot(this) ? globalEnv.rawFragmentAppend : globalEnv.rawAppend);
|
|
7938
8017
|
i++;
|
|
7939
8018
|
}
|
|
7940
8019
|
};
|