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

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.d.ts CHANGED
@@ -148,6 +148,7 @@ declare module '@micro-zoe/micro-app/libs/utils' {
148
148
  export function isAudioElement(target: unknown): target is HTMLAudioElement;
149
149
  export function isVideoElement(target: unknown): target is HTMLVideoElement;
150
150
  export function isLinkElement(target: unknown): target is HTMLLinkElement;
151
+ export function isBodyElement(target: unknown): target is HTMLBodyElement;
151
152
  export function isStyleElement(target: unknown): target is HTMLStyleElement;
152
153
  export function isScriptElement(target: unknown): target is HTMLScriptElement;
153
154
  export function isIFrameElement(target: unknown): target is HTMLIFrameElement;
package/lib/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- const version = '1.0.0-rc.20';
1
+ const version = '1.0.0-rc.21';
2
2
  // do not use isUndefined
3
3
  const isBrowser = typeof window !== 'undefined';
4
4
  // do not use isUndefined
@@ -101,6 +101,9 @@ function isVideoElement(target) {
101
101
  function isLinkElement(target) {
102
102
  return toTypeString(target) === '[object HTMLLinkElement]';
103
103
  }
104
+ function isBodyElement(target) {
105
+ return toTypeString(target) === '[object HTMLBodyElement]';
106
+ }
104
107
  function isStyleElement(target) {
105
108
  return toTypeString(target) === '[object HTMLStyleElement]';
106
109
  }
@@ -1234,16 +1237,17 @@ function scopedCSS(styleElement, app, linkPath) {
1234
1237
  const prefix = createPrefix(app.name);
1235
1238
  if (!parser)
1236
1239
  parser = new CSSParser();
1240
+ const escapeRegExp = (regStr) => regStr.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1237
1241
  if (styleElement.textContent) {
1238
1242
  commonAction(styleElement, app.name, prefix, app.url, linkPath);
1239
1243
  const observer = new MutationObserver(() => {
1240
- const escapedPrefix = prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1244
+ const escapedPrefix = escapeRegExp(prefix);
1241
1245
  const isPrefixed = styleElement.textContent && new RegExp(escapedPrefix).test(styleElement.textContent);
1242
1246
  observer.disconnect();
1243
1247
  if (!isPrefixed) {
1244
1248
  styleElement.__MICRO_APP_HAS_SCOPED__ = false;
1249
+ scopedCSS(styleElement, app, linkPath);
1245
1250
  }
1246
- scopedCSS(styleElement, app, linkPath);
1247
1251
  });
1248
1252
  observer.observe(styleElement, { childList: true, characterData: true });
1249
1253
  }
@@ -2124,7 +2128,7 @@ function execScripts(app, initHook) {
2124
2128
  * @param callback callback of module script
2125
2129
  */
2126
2130
  function runScript(address, app, scriptInfo, callback, replaceElement) {
2127
- var _a;
2131
+ var _a, _b;
2128
2132
  try {
2129
2133
  actionsBeforeRunScript(app);
2130
2134
  const appSpaceData = scriptInfo.appSpace[app.name];
@@ -2164,9 +2168,16 @@ function runScript(address, app, scriptInfo, callback, replaceElement) {
2164
2168
  }
2165
2169
  }
2166
2170
  catch (e) {
2167
- console.error(`[micro-app from ${replaceElement ? 'runDynamicScript' : 'runScript'}] app ${app.name}: `, e, address);
2171
+ console.warn(`[micro-app from ${replaceElement ? 'runDynamicScript' : 'runScript'}] app ${app.name}: `, e, address);
2168
2172
  // throw error in with sandbox to parent app
2169
- throw e;
2173
+ const error = e;
2174
+ let throwError = true;
2175
+ if (typeof ((_b = microApp === null || microApp === void 0 ? void 0 : microApp.options) === null || _b === void 0 ? void 0 : _b.excludeRunScriptFilter) === 'function') {
2176
+ throwError = microApp.options.excludeRunScriptFilter(address, error, app.name, app.url) !== true;
2177
+ }
2178
+ if (throwError) {
2179
+ throw e;
2180
+ }
2170
2181
  }
2171
2182
  }
2172
2183
  /**
@@ -2324,53 +2335,49 @@ function processCode(configs, code, address) {
2324
2335
 
2325
2336
  /**
2326
2337
  * Recursively process each child element
2327
- * @param parent parent element
2338
+ * @param body body element
2328
2339
  * @param app app
2329
2340
  * @param microAppHead micro-app-head element
2330
2341
  */
2331
- function flatChildren(parent, app, microAppHead, fiberStyleTasks) {
2332
- const children = Array.from(parent.children);
2333
- children.length && children.forEach((child) => {
2334
- flatChildren(child, app, microAppHead, fiberStyleTasks);
2335
- });
2336
- for (const dom of children) {
2337
- if (isLinkElement(dom)) {
2338
- if (dom.hasAttribute('exclude') || checkExcludeUrl(dom.getAttribute('href'), app.name)) {
2339
- parent.replaceChild(document.createComment('link element with exclude attribute ignored by micro-app'), dom);
2340
- }
2341
- else if (!(dom.hasAttribute('ignore') || checkIgnoreUrl(dom.getAttribute('href'), app.name))) {
2342
- extractLinkFromHtml(dom, parent, app);
2343
- }
2344
- else if (dom.hasAttribute('href')) {
2345
- globalEnv.rawSetAttribute.call(dom, 'href', CompletionPath(dom.getAttribute('href'), app.url));
2346
- }
2342
+ function flatBodyChildren(body, app, fiberStyleTasks) {
2343
+ if (!body || !isBodyElement(body)) {
2344
+ return;
2345
+ }
2346
+ const links = Array.from(body.getElementsByTagName('link'));
2347
+ links.map((dom) => {
2348
+ if (dom.hasAttribute('exclude') || checkExcludeUrl(dom.getAttribute('href'), app.name)) {
2349
+ dom.parentElement.replaceChild(document.createComment('link element with exclude attribute ignored by micro-app'), dom);
2347
2350
  }
2348
- else if (isStyleElement(dom)) {
2349
- if (dom.hasAttribute('exclude')) {
2350
- parent.replaceChild(document.createComment('style element with exclude attribute ignored by micro-app'), dom);
2351
- }
2352
- else if (app.scopecss && !dom.hasAttribute('ignore')) {
2353
- injectFiberTask(fiberStyleTasks, () => scopedCSS(dom, app));
2354
- }
2351
+ else if (!(dom.hasAttribute('ignore') || checkIgnoreUrl(dom.getAttribute('href'), app.name))) {
2352
+ extractLinkFromHtml(dom, dom.parentElement, app);
2353
+ }
2354
+ else if (dom.hasAttribute('href')) {
2355
+ globalEnv.rawSetAttribute.call(dom, 'href', CompletionPath(dom.getAttribute('href'), app.url));
2356
+ }
2357
+ return dom;
2358
+ });
2359
+ const styles = Array.from(body.getElementsByTagName('style'));
2360
+ styles.map((dom) => {
2361
+ if (dom.hasAttribute('exclude')) {
2362
+ dom.parentElement.replaceChild(document.createComment('style element with exclude attribute ignored by micro-app'), dom);
2355
2363
  }
2356
- else if (isScriptElement(dom)) {
2357
- extractScriptElement(dom, parent, app);
2364
+ else if (app.scopecss && !dom.hasAttribute('ignore')) {
2365
+ injectFiberTask(fiberStyleTasks, () => scopedCSS(dom, app));
2358
2366
  }
2359
- else if (isImageElement(dom) && dom.hasAttribute('src')) {
2367
+ return dom;
2368
+ });
2369
+ const scripts = Array.from(body.getElementsByTagName('script'));
2370
+ scripts.map((dom) => {
2371
+ extractScriptElement(dom, dom.parentElement, app);
2372
+ return dom;
2373
+ });
2374
+ const images = Array.from(body.getElementsByTagName('img'));
2375
+ images.map((dom) => {
2376
+ if (dom.hasAttribute('src')) {
2360
2377
  globalEnv.rawSetAttribute.call(dom, 'src', CompletionPath(dom.getAttribute('src'), app.url));
2361
2378
  }
2362
- /**
2363
- * Don't remove meta and title, they have some special scenes
2364
- * e.g.
2365
- * document.querySelector('meta[name="viewport"]') // for flexible
2366
- * document.querySelector('meta[name="baseurl"]').baseurl // for api request
2367
- *
2368
- * Title point to main app title, child app title used to be compatible with some special scenes
2369
- */
2370
- // else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
2371
- // parent.removeChild(dom)
2372
- // }
2373
- }
2379
+ return dom;
2380
+ });
2374
2381
  }
2375
2382
  /**
2376
2383
  * Extract link and script, bind style scope
@@ -2387,7 +2394,7 @@ function extractSourceDom(htmlStr, app) {
2387
2394
  return logError(msg, app.name);
2388
2395
  }
2389
2396
  const fiberStyleTasks = app.isPrefetch || app.fiber ? [] : null;
2390
- flatChildren(wrapElement, app, microAppHead, fiberStyleTasks);
2397
+ flatBodyChildren(wrapElement, app, fiberStyleTasks);
2391
2398
  /**
2392
2399
  * Style and link are parallel, as it takes a lot of time for link to request resources. During this period, style processing can be performed to improve efficiency.
2393
2400
  */
@@ -6154,7 +6161,7 @@ function patchDocumentProperty(appName, microAppWindow, sandbox) {
6154
6161
  var _a;
6155
6162
  throttleDeferForIframeAppName(appName);
6156
6163
  if (tagName === 'body') {
6157
- return (_a = sandbox.options.container) === null || _a === void 0 ? void 0 : _a.querySelector('micro-app-body');
6164
+ return ((_a = sandbox.options.container) === null || _a === void 0 ? void 0 : _a.querySelector('micro-app-body')) || rawDocument[tagName];
6158
6165
  }
6159
6166
  return rawDocument[tagName];
6160
6167
  },