@micro-zoe/micro-app 1.0.0-rc.19 → 1.0.0-rc.20

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.19';
1
+ const version = '1.0.0-rc.20';
2
2
  // do not use isUndefined
3
3
  const isBrowser = typeof window !== 'undefined';
4
4
  // do not use isUndefined
@@ -636,6 +636,10 @@ function instanceOf(instance, constructor) {
636
636
  else if (!isFunction(constructor)) {
637
637
  throw new TypeError("Right-hand side of 'instanceof' is not callable");
638
638
  }
639
+ else if (typeof instance === 'number' || typeof instance === 'string' || typeof instance === 'boolean') {
640
+ // 检查 obj 是否是基本类型的包装器实例
641
+ return false;
642
+ }
639
643
  let proto = Object.getPrototypeOf(instance);
640
644
  while (proto) {
641
645
  if (proto === constructor.prototype) {
@@ -1233,7 +1237,8 @@ function scopedCSS(styleElement, app, linkPath) {
1233
1237
  if (styleElement.textContent) {
1234
1238
  commonAction(styleElement, app.name, prefix, app.url, linkPath);
1235
1239
  const observer = new MutationObserver(() => {
1236
- const isPrefixed = styleElement.textContent && new RegExp(prefix).test(styleElement.textContent);
1240
+ const escapedPrefix = prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1241
+ const isPrefixed = styleElement.textContent && new RegExp(escapedPrefix).test(styleElement.textContent);
1237
1242
  observer.disconnect();
1238
1243
  if (!isPrefixed) {
1239
1244
  styleElement.__MICRO_APP_HAS_SCOPED__ = false;
@@ -2926,6 +2931,7 @@ class BaseSandbox {
2926
2931
  // TODO: 是否可以和constants/SCOPE_WINDOW_ON_EVENT合并
2927
2932
  'onpopstate',
2928
2933
  'onhashchange',
2934
+ 'event',
2929
2935
  ];
2930
2936
  // Properties that can only get and set in microAppWindow, will not escape to rawWindow
2931
2937
  this.scopeProperties = Array.from(this.staticScopeProperties);
@@ -3577,8 +3583,13 @@ function patchWindowEffect(microAppWindow, appName) {
3577
3583
  return intervalId;
3578
3584
  };
3579
3585
  microAppWindow.setTimeout = function (handler, timeout, ...args) {
3580
- const timeoutId = rawSetTimeout.call(rawWindow, handler, timeout, ...args);
3581
- timeoutIdMap.set(timeoutId, { handler, timeout, args });
3586
+ const setTimeoutHander = function (...args) {
3587
+ timeoutIdMap.delete(timeoutId);
3588
+ typeof handler === 'function' && handler(...args);
3589
+ };
3590
+ const handlerWithCleanup = typeof handler === 'string' ? handler : setTimeoutHander;
3591
+ const timeoutId = rawSetTimeout.call(rawWindow, handlerWithCleanup, timeout, ...args);
3592
+ timeoutIdMap.set(timeoutId, { handler: handlerWithCleanup, timeout, args });
3582
3593
  return timeoutId;
3583
3594
  };
3584
3595
  microAppWindow.clearInterval = function (intervalId) {
@@ -5996,6 +6007,9 @@ function patchDocumentPrototype(appName, microAppWindow) {
5996
6007
  function querySelector(selectors) {
5997
6008
  var _a;
5998
6009
  const _this = getBindTarget(this);
6010
+ if (selectors === 'body') {
6011
+ return this.body;
6012
+ }
5999
6013
  if (!selectors ||
6000
6014
  isUniqueElement(selectors) ||
6001
6015
  rawDocument !== _this) {
@@ -6137,7 +6151,11 @@ function patchDocumentProperty(appName, microAppWindow, sandbox) {
6137
6151
  enumerable: true,
6138
6152
  configurable: true,
6139
6153
  get: () => {
6154
+ var _a;
6140
6155
  throttleDeferForIframeAppName(appName);
6156
+ if (tagName === 'body') {
6157
+ return (_a = sandbox.options.container) === null || _a === void 0 ? void 0 : _a.querySelector('micro-app-body');
6158
+ }
6141
6159
  return rawDocument[tagName];
6142
6160
  },
6143
6161
  set: (value) => { rawDocument[tagName] = value; },
@@ -6296,7 +6314,7 @@ function patchDocumentEffect(appName, microAppWindow) {
6296
6314
  */
6297
6315
  function patchElement(appName, url, microAppWindow, sandbox) {
6298
6316
  patchIframeNode(appName, microAppWindow, sandbox);
6299
- patchIframeAttribute(url, microAppWindow);
6317
+ patchIframeAttribute(url, microAppWindow, appName);
6300
6318
  }
6301
6319
  /**
6302
6320
  * patch iframe Node/Element
@@ -6471,17 +6489,27 @@ function patchIframeNode(appName, microAppWindow, sandbox) {
6471
6489
  value: ImageProxy,
6472
6490
  });
6473
6491
  }
6474
- function patchIframeAttribute(url, microAppWindow) {
6492
+ function patchIframeAttribute(url, microAppWindow, appName) {
6475
6493
  const microRootElement = microAppWindow.Element;
6476
6494
  const rawMicroSetAttribute = microRootElement.prototype.setAttribute;
6477
6495
  microRootElement.prototype.setAttribute = function setAttribute(key, value) {
6496
+ var _a, _b, _c, _d;
6478
6497
  if (/^micro-app(-\S+)?/i.test(this.tagName) &&
6479
6498
  key === 'data' &&
6480
6499
  this.setAttribute !== microRootElement.prototype.setAttribute) {
6481
6500
  this.setAttribute(key, value);
6482
6501
  }
6483
6502
  else {
6484
- if (((key === 'src' || key === 'srcset') && /^(img|script|video|audio|source|embed)$/i.test(this.tagName)) ||
6503
+ let appPlugins = (_c = (_b = (_a = microApp === null || microApp === void 0 ? void 0 : microApp.options) === null || _a === void 0 ? void 0 : _a.plugins) === null || _b === void 0 ? void 0 : _b.modules) === null || _c === void 0 ? void 0 : _c[appName];
6504
+ if (!isArray(appPlugins)) {
6505
+ appPlugins = [];
6506
+ }
6507
+ const aHrefResolver = (_d = appPlugins[0]) === null || _d === void 0 ? void 0 : _d.aHrefResolver;
6508
+ if (key === 'href' && /^a$/i.test(this.tagName) && typeof aHrefResolver === 'function') {
6509
+ // 试验性质:a 标签开放自定义补齐功能
6510
+ value = aHrefResolver(value, appName, url);
6511
+ }
6512
+ else if (((key === 'src' || key === 'srcset') && /^(img|script|video|audio|source|embed)$/i.test(this.tagName)) ||
6485
6513
  (key === 'href' && /^(link|image)$/i.test(this.tagName)) ||
6486
6514
  // If it is the anchor tag,eg. <a href="#xxx"/>, the path will not be completed
6487
6515
  (key === 'href' && /^(a)$/i.test(this.tagName) && !/^#/.test(value))) {
@@ -6922,6 +6950,7 @@ class CreateApp {
6922
6950
  // TODO: 类型优化,加上iframe沙箱
6923
6951
  this.sandBox = null;
6924
6952
  this.fiber = false;
6953
+ this.isReloading = false;
6925
6954
  appInstanceMap.set(name, this);
6926
6955
  // init actions
6927
6956
  this.name = name;
@@ -6932,6 +6961,7 @@ class CreateApp {
6932
6961
  // exec before getInlineModeState
6933
6962
  this.iframe = iframe !== null && iframe !== void 0 ? iframe : false;
6934
6963
  this.inline = this.getInlineModeState(inline);
6964
+ this.isReloading = false;
6935
6965
  /**
6936
6966
  * NOTE:
6937
6967
  * 1. Navigate after micro-app created, before mount
@@ -7297,6 +7327,7 @@ class CreateApp {
7297
7327
  if (this.umdMode && this.container && !destroy) {
7298
7328
  this.cloneContainer(this.source.html, this.container, false);
7299
7329
  }
7330
+ const shouldClearData = this.isReloading ? false : (clearData || destroy);
7300
7331
  /**
7301
7332
  * this.container maybe contains micro-app element, stop sandbox should exec after cloneContainer
7302
7333
  * NOTE:
@@ -7307,7 +7338,7 @@ class CreateApp {
7307
7338
  umdMode: this.umdMode,
7308
7339
  keepRouteState: keepRouteState && !destroy,
7309
7340
  destroy,
7310
- clearData: clearData || destroy,
7341
+ clearData: shouldClearData,
7311
7342
  });
7312
7343
  // dispatch unmount event to base app
7313
7344
  dispatchLifecyclesEvent(this.container, this.name, lifeCycles.UNMOUNT);
@@ -7465,7 +7496,11 @@ class CreateApp {
7465
7496
  */
7466
7497
  createSandbox() {
7467
7498
  if (this.useSandbox && !this.sandBox) {
7468
- this.sandBox = this.iframe ? new IframeSandbox(this.name, this.url, { attrs: this.attrs }) : new WithSandBox(this.name, this.url);
7499
+ const iframeOption = {
7500
+ attrs: this.attrs,
7501
+ container: this.container
7502
+ };
7503
+ this.sandBox = this.iframe ? new IframeSandbox(this.name, this.url, iframeOption) : new WithSandBox(this.name, this.url);
7469
7504
  }
7470
7505
  }
7471
7506
  // set app state
@@ -9160,7 +9195,15 @@ function reload(appName, destroy) {
9160
9195
  if (app) {
9161
9196
  const rootContainer = app.container && getRootContainer(app.container);
9162
9197
  if (rootContainer) {
9163
- resolve(rootContainer.reload(destroy));
9198
+ const currentData = microApp.getData(appName);
9199
+ app.isReloading = true;
9200
+ rootContainer.reload(destroy).then(() => {
9201
+ if (currentData) {
9202
+ microApp.setData(appName, currentData);
9203
+ }
9204
+ app.isReloading = false;
9205
+ resolve(true);
9206
+ });
9164
9207
  }
9165
9208
  else {
9166
9209
  logWarn(`app ${appName} is not rendered, cannot use reload`);