@micro-zoe/micro-app 1.0.0-rc.30 → 1.0.0-rc.32

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 CHANGED
@@ -1,4 +1,4 @@
1
- const version = '1.0.0-rc.30';
1
+ const version = '1.0.0-rc.32';
2
2
  // do not use isUndefined
3
3
  const isBrowser = typeof window !== 'undefined';
4
4
  // do not use isUndefined
@@ -249,13 +249,15 @@ function formatAppURL(url, appName = null) {
249
249
  if (!isString(url) || !url)
250
250
  return '';
251
251
  try {
252
- const { origin, pathname, search } = createURL(addProtocol(url), (window.rawWindow || window).location.href);
252
+ const { protocol, origin: rawOrigin, host, pathname, search } = createURL(addProtocol(url), (window.rawWindow || window).location.href);
253
253
  /**
254
254
  * keep the original url unchanged, such as .html .node .php .net .etc, search, except hash
255
255
  * BUG FIX: Never using '/' to complete url, refer to https://github.com/jd-opensource/micro-app/issues/1147
256
+ * FEATURE: URL.origin is browser-dependent for file protocol url, refer to https://github.com/jd-opensource/micro-app/pull/1695
256
257
  */
258
+ const origin = protocol === 'file:' ? `${protocol}//${host}` : rawOrigin;
257
259
  const fullPath = `${origin}${pathname}${search}`;
258
- return /^https?:\/\//.test(fullPath) ? fullPath : '';
260
+ return /^(https?|file):\/\//.test(fullPath) ? fullPath : '';
259
261
  }
260
262
  catch (e) {
261
263
  logError(e, appName);
@@ -284,7 +286,11 @@ function formatAppName(name) {
284
286
  * @param url app.url
285
287
  */
286
288
  function getEffectivePath(url) {
287
- const { origin, pathname } = createURL(url);
289
+ const { protocol, origin: rawOrigin, host, pathname } = createURL(url);
290
+ /*
291
+ * FEATURE: URL.origin is browser-dependent for file protocol url, refer to https://github.com/jd-opensource/micro-app/pull/1695
292
+ */
293
+ const origin = protocol === 'file:' ? `${protocol}//${host}` : rawOrigin;
288
294
  if (/\.(\w+)$/.test(pathname)) {
289
295
  const pathArr = `${origin}${pathname}`.split('/');
290
296
  pathArr.pop();
@@ -822,8 +828,8 @@ class HTMLLoader {
822
828
  }
823
829
  }
824
830
 
825
- // common reg
826
- const rootSelectorREG = /(^|\s+)(html|:root)(?=[\s>~[.#:]+|$)/;
831
+ // common regular expression
832
+ // const rootSelectorREG = /(^|\s+)(html|:root)(?=[\s>~[.#:]+|$)/
827
833
  const bodySelectorREG = /(^|\s+)((html[\s>~]+body)|body)(?=[\s>~[.#:]+|$)/;
828
834
  function parseError(msg, linkPath) {
829
835
  msg = linkPath ? `${linkPath} ${msg}` : msg;
@@ -850,6 +856,7 @@ class CSSParser {
850
856
  this.scopecssDisable = false; // use block comments /* scopecss-disable */ to disable scopecss in your file, and use /* scopecss-enable */ to enable scopecss
851
857
  this.scopecssDisableSelectors = []; // disable or enable scopecss for specific selectors
852
858
  this.scopecssDisableNextLine = false; // use block comments /* scopecss-disable-next-line */ to disable scopecss on a specific line
859
+ this.optionCssSelectors = []; // use this option to include specific selectors, so that they will be affected by scopecss ,like microApp.options.optionCss
853
860
  // https://developer.mozilla.org/en-US/docs/Web/API/CSSMediaRule
854
861
  this.mediaRule = this.createMatcherForRuleWithChildRule(/^@media *([^{]+)/, '@media');
855
862
  // https://developer.mozilla.org/en-US/docs/Web/API/CSSSupportsRule
@@ -872,6 +879,8 @@ class CSSParser {
872
879
  this.prefix = prefix;
873
880
  this.baseURI = baseURI;
874
881
  this.linkPath = linkPath || '';
882
+ // fetch optionCss configure
883
+ this.optionCssSelectors = microApp.options.optionCss || [];
875
884
  this.matchRules();
876
885
  return isFireFox() ? decodeURIComponent(this.result) : this.result;
877
886
  }
@@ -879,6 +888,7 @@ class CSSParser {
879
888
  this.cssText = this.prefix = this.baseURI = this.linkPath = this.result = '';
880
889
  this.scopecssDisable = this.scopecssDisableNextLine = false;
881
890
  this.scopecssDisableSelectors = [];
891
+ this.optionCssSelectors = [];
882
892
  }
883
893
  // core action for match rules
884
894
  matchRules() {
@@ -936,6 +946,14 @@ class CSSParser {
936
946
  }
937
947
  return match;
938
948
  });
949
+ // Check if it is in the optionCss list
950
+ const isIsolationRoot = this.optionCssSelectors.some(includeSelector => {
951
+ // isolation-root is used to isolation the :root styles between base-app and sub-app
952
+ if (includeSelector === 'isolation-root')
953
+ return true;
954
+ return false;
955
+ });
956
+ const rootSelectorREG = isIsolationRoot ? /(^|\s+)(html)(?=[\s>~[.#:]+|$)/ : /(^|\s+)(html|:root)(?=[\s>~[.#:]+|$)/; // without root
939
957
  if (selector && !(this.scopecssDisableNextLine ||
940
958
  (this.scopecssDisable && (!this.scopecssDisableSelectors.length ||
941
959
  this.scopecssDisableSelectors.includes(selector))) ||
@@ -965,7 +983,7 @@ class CSSParser {
965
983
  if (!this.scopecssDisableNextLine &&
966
984
  (!this.scopecssDisable || this.scopecssDisableSelectors.length)) {
967
985
  cssValue = cssValue.replace(/url\((["']?)(.*?)\1\)/gm, (all, _, $1) => {
968
- if (/^((data|blob):|#|%23)/.test($1) || /^(https?:)?\/\//.test($1)) {
986
+ if (/^((data|blob):|#|%23)/.test($1) || /^((https?|file):)?\/\//.test($1)) {
969
987
  return all;
970
988
  }
971
989
  // ./a/b.png ../a/b.png a/b.png
@@ -3406,6 +3424,95 @@ function createMicroDocument(appName, proxyDocument) {
3406
3424
  return MicroDocument;
3407
3425
  }
3408
3426
 
3427
+ const EXCLUDE_URL_PROTOCOLS = [
3428
+ 'blob:'
3429
+ ];
3430
+ // 重写 Worker 构造函数的类型
3431
+ const originalWorker = window.Worker;
3432
+ // create a proxy for the Worker constructor to intercept the creation of new Worker instances
3433
+ const workerInstanceMap = new Map();
3434
+ // release all workers of the app after sandboxes destoried.
3435
+ function releaseWorkersByApp(appName) {
3436
+ var _a;
3437
+ (_a = workerInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.forEach(w => w.terminate());
3438
+ workerInstanceMap.delete(appName);
3439
+ }
3440
+ function isSameOrigin(url) {
3441
+ try {
3442
+ // 检查 URL 是否与当前页面在同一个源
3443
+ const parsedUrl = url instanceof URL ? url : new URL(url);
3444
+ if (EXCLUDE_URL_PROTOCOLS.includes(parsedUrl.protocol)) {
3445
+ return true;
3446
+ }
3447
+ return (parsedUrl.protocol === window.location.protocol &&
3448
+ parsedUrl.hostname === window.location.hostname &&
3449
+ parsedUrl.port === window.location.port);
3450
+ }
3451
+ catch (error) {
3452
+ return false;
3453
+ }
3454
+ }
3455
+ function urlFromScript(script) {
3456
+ let blob;
3457
+ try {
3458
+ blob = new Blob([script], {
3459
+ type: 'application/javascript'
3460
+ });
3461
+ }
3462
+ catch (e) {
3463
+ const BlobBuilder =
3464
+ // @ts-ignore
3465
+ window.BlobBuilder ||
3466
+ // @ts-ignore
3467
+ window.WebKitBlobBuilder ||
3468
+ // @ts-ignore
3469
+ window.MozBlobBuilder ||
3470
+ // @ts-ignore
3471
+ window.MSBlobBuilder;
3472
+ const blobBuilder = new BlobBuilder();
3473
+ blobBuilder.append(script);
3474
+ blob = blobBuilder.getBlob('application/javascript');
3475
+ }
3476
+ const URL = window.URL || window.webkitURL;
3477
+ return URL.createObjectURL(blob);
3478
+ }
3479
+ // @ts-ignore
3480
+ const WorkerProxy = new Proxy(originalWorker, {
3481
+ construct(Target, args) {
3482
+ let [scriptURL, options] = args;
3483
+ options = options || {};
3484
+ const appName = getCurrentAppName();
3485
+ let url = scriptURL;
3486
+ if (appName) {
3487
+ const app = appInstanceMap.get(appName);
3488
+ url = CompletionPath(scriptURL, app.url);
3489
+ }
3490
+ let instance;
3491
+ if (url && !isSameOrigin(url)) {
3492
+ // 如果 scriptURL 是跨域的,使用 Blob URL 加载并执行 worker
3493
+ const workerScriptURL = JSON.stringify(String(url));
3494
+ // check if the type of worker is module or classic
3495
+ const script = options.type === 'module'
3496
+ ? `import ${workerScriptURL};`
3497
+ : `importScripts(${workerScriptURL});`;
3498
+ const workerPath = urlFromScript(script);
3499
+ instance = new Target(workerPath, options);
3500
+ }
3501
+ else {
3502
+ // 如果 scriptURL 是同源的,直接使用原生的 Worker 构造函数
3503
+ instance = new Target(scriptURL, options);
3504
+ }
3505
+ // register worker instance to app
3506
+ if (appName) {
3507
+ if (!workerInstanceMap.has(appName)) {
3508
+ workerInstanceMap.set(appName, new Set());
3509
+ }
3510
+ workerInstanceMap.get(appName).add(instance);
3511
+ }
3512
+ return instance;
3513
+ },
3514
+ });
3515
+
3409
3516
  /**
3410
3517
  * patch window of child app
3411
3518
  * @param appName app name
@@ -3452,9 +3559,18 @@ function patchWindowProperty(microAppWindow) {
3452
3559
  function createProxyWindow(appName, microAppWindow, sandbox) {
3453
3560
  const rawWindow = globalEnv.rawWindow;
3454
3561
  const descriptorTargetMap = new Map();
3562
+ rawDefineProperty(microAppWindow, 'Worker', {
3563
+ value: WorkerProxy,
3564
+ configurable: true,
3565
+ writable: true,
3566
+ });
3455
3567
  const proxyWindow = new Proxy(microAppWindow, {
3456
3568
  get: (target, key) => {
3457
3569
  throttleDeferForSetAppName(appName);
3570
+ // return WorkerProxy if child app accesses window.Worker
3571
+ if (key === 'Worker') {
3572
+ return WorkerProxy;
3573
+ }
3458
3574
  if (Reflect.has(target, key) ||
3459
3575
  (isString(key) && /^__MICRO_APP_/.test(key)) ||
3460
3576
  includes(sandbox.scopeProperties, key)) {
@@ -5150,6 +5266,7 @@ class WithSandBox extends BaseSandbox {
5150
5266
  */
5151
5267
  if (!umdMode || destroy) {
5152
5268
  clearMicroEventSource(this.microAppWindow.__MICRO_APP_NAME__);
5269
+ releaseWorkersByApp(this.microAppWindow.__MICRO_APP_NAME__);
5153
5270
  this.injectedKeys.forEach((key) => {
5154
5271
  Reflect.deleteProperty(this.microAppWindow, key);
5155
5272
  });
@@ -5642,75 +5759,6 @@ const proxy2RawDocumentMethods = [
5642
5759
  'prepend',
5643
5760
  ];
5644
5761
 
5645
- const EXCLUDE_URL_PROTOCOLS = [
5646
- 'blob:'
5647
- ];
5648
- // 重写 Worker 构造函数的类型
5649
- const originalWorker = window.Worker;
5650
- function isSameOrigin(url) {
5651
- try {
5652
- // 检查 URL 是否与当前页面在同一个源
5653
- const parsedUrl = url instanceof URL ? url : new URL(url);
5654
- if (EXCLUDE_URL_PROTOCOLS.includes(parsedUrl.protocol)) {
5655
- return true;
5656
- }
5657
- return (parsedUrl.protocol === window.location.protocol &&
5658
- parsedUrl.hostname === window.location.hostname &&
5659
- parsedUrl.port === window.location.port);
5660
- }
5661
- catch (error) {
5662
- return false;
5663
- }
5664
- }
5665
- function urlFromScript(script) {
5666
- let blob;
5667
- try {
5668
- blob = new Blob([script], {
5669
- type: 'application/javascript'
5670
- });
5671
- }
5672
- catch (e) {
5673
- const BlobBuilder =
5674
- // @ts-ignore
5675
- window.BlobBuilder ||
5676
- // @ts-ignore
5677
- window.WebKitBlobBuilder ||
5678
- // @ts-ignore
5679
- window.MozBlobBuilder ||
5680
- // @ts-ignore
5681
- window.MSBlobBuilder;
5682
- const blobBuilder = new BlobBuilder();
5683
- blobBuilder.append(script);
5684
- blob = blobBuilder.getBlob('application/javascript');
5685
- }
5686
- const URL = window.URL || window.webkitURL;
5687
- return URL.createObjectURL(blob);
5688
- }
5689
- // @ts-ignore
5690
- const WorkerProxy = new Proxy(originalWorker, {
5691
- construct(Target, args) {
5692
- let [scriptURL, options] = args;
5693
- options = options || {};
5694
- const appName = getCurrentAppName();
5695
- let url = scriptURL;
5696
- if (appName) {
5697
- const app = appInstanceMap.get(appName);
5698
- url = CompletionPath(scriptURL, app.url);
5699
- }
5700
- if (url && !isSameOrigin(url)) {
5701
- // 如果 scriptURL 是跨域的,使用 Blob URL 加载并执行 worker
5702
- const script = `import "${scriptURL}";`;
5703
- const workerPath = urlFromScript(script);
5704
- options.type = 'module';
5705
- return new Target(workerPath, options);
5706
- }
5707
- else {
5708
- // 如果 scriptURL 是同源的,直接使用原生的 Worker 构造函数
5709
- return new Target(scriptURL, options);
5710
- }
5711
- },
5712
- });
5713
-
5714
5762
  /**
5715
5763
  * patch window of child app
5716
5764
  * @param appName app name
@@ -6737,6 +6785,7 @@ class IframeSandbox {
6737
6785
  if (!umdMode || destroy) {
6738
6786
  this.deleteIframeElement();
6739
6787
  this.clearHijackUmdHooks();
6788
+ releaseWorkersByApp(this.microAppWindow.__MICRO_APP_NAME__);
6740
6789
  }
6741
6790
  if (--globalEnv.activeSandbox === 0) {
6742
6791
  releasePatchElementAndDocument();