@micro-zoe/micro-app 0.5.0 → 0.5.1

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.umd.js CHANGED
@@ -1,2 +1,2 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).microApp={})}(this,(function(e){"use strict";const t="0.5.0",n="undefined"!=typeof window,o="undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:Function("return this")();function r(e){return"string"==typeof e}function i(e){return"function"==typeof e}const s=Array.isArray;function a(e){return"[object Object]"===toString.call(e)}function c(e){return"[object Promise]"===toString.call(e)}function l(e){return i(e)&&0===e.name.indexOf("bound ")&&!e.hasOwnProperty("prototype")}function u(e,t=null,...n){const o=t&&r(t)?` app ${t}:`:"";r(e)?console.error(`[micro-app]${o} ${e}`,...n):console.error(`[micro-app]${o}`,e,...n)}function p(e,t=null,...n){const o=t&&r(t)?` app ${t}:`:"";r(e)?console.warn(`[micro-app]${o} ${e}`,...n):console.warn(`[micro-app]${o}`,e,...n)}function d(e,...t){Promise.resolve().then(e.bind(null,...t))}function h(e){return e.startsWith("//")?`${location.protocol}${e}`:e}function m(e,t=null){if(!r(e)||!e)return"";try{const{origin:t,pathname:n,search:o}=new URL(h(e));if(/\.(\w+)$/.test(n))return`${t}${n}${o}`;const r=`${t}${n}/`.replace(/\/\/$/,"/");return/^https?:\/\//.test(r)?`${r}${o}`:""}catch(e){return u(e,t),""}}function f(e){return r(e)&&e?e.replace(/(^\d+)|([^\w\d-_])/gi,""):""}function _(e){const{origin:t,pathname:n}=new URL(e);if(/\.(\w+)$/.test(n)){const e=`${t}${n}`.split("/");return e.pop(),e.join("/")+"/"}return`${t}${n}/`.replace(/\/\/$/,"/")}function w(e,t){return!e||/^((((ht|f)tps?)|file):)?\/\//.test(e)||/^(data|blob):/.test(e)?e:new URL(e,_(h(t))).toString()}function y(e,t,n,o){let r=0;function i(){++r===e.length&&o&&o()}e.forEach(((e,o)=>{c(e)?e.then((e=>{t({data:e,index:o}),i()})).catch((e=>{n({error:e,index:o}),i()})):(t({data:e,index:o}),i())}))}const b=o.requestIdleCallback||function(e){const t=Date.now();return setTimeout((function(){e({didTimeout:!1,timeRemaining:()=>Math.max(0,50-(Date.now()-t))})}),1)};let g=null;function E(e){g=e}function v(){return g}function A(){E(null)}function N(){return/Safari/.test(navigator.userAgent)&&!/Chrome/.test(navigator.userAgent)}function C(e,t){const n=document.createElement(e,t);return n.__MICRO_APP_NAME__&&delete n.__MICRO_APP_NAME__,n}function O(e,t,n){if(t.innerHTML="",n){const n=e.cloneNode(!0),o=document.createDocumentFragment();Array.from(n.childNodes).forEach((e=>{o.appendChild(e)})),t.appendChild(o)}else Array.from(e.childNodes).forEach((e=>{t.appendChild(e)}))}function P(e){return!e||/(^\d)|([^\w\d-_\u4e00-\u9fa5])/gi.test(e)}function M(e){return/^body$/i.test(e)||/^head$/i.test(e)||/^html$/i.test(e)}function R(e){return function(e){return"undefined"!=typeof ShadowRoot&&e instanceof ShadowRoot}(e)?e.host:e}var D,S,U;function L(e,t=null,n={}){return i(je.fetch)?je.fetch(e,n,t):fetch(e,n).then((e=>e.text()))}!function(e){e.NAME="name",e.URL="url"}(D||(D={})),function(e){e.NOT_LOADED="NOT_LOADED",e.LOADING_SOURCE_CODE="LOADING_SOURCE_CODE",e.LOAD_SOURCE_FINISHED="LOAD_SOURCE_FINISHED",e.LOAD_SOURCE_ERROR="LOAD_SOURCE_ERROR",e.MOUNTING="MOUNTING",e.MOUNTED="MOUNTED",e.UNMOUNT="UNMOUNT"}(S||(S={})),function(e){e.CREATED="created",e.BEFOREMOUNT="beforemount",e.MOUNTED="mounted",e.UNMOUNT="unmount",e.ERROR="error"}(U||(U={}));const I={};function T(){if(n){const e=Element.prototype.setAttribute,t=Node.prototype.appendChild,n=Node.prototype.insertBefore,o=Node.prototype.replaceChild,r=Node.prototype.removeChild,i=Element.prototype.append,s=Element.prototype.prepend,a=Document.prototype.createElement,c=Document.prototype.createElementNS,l=Document.prototype.createDocumentFragment,u=Document.prototype.querySelector,p=Document.prototype.querySelectorAll,d=Document.prototype.getElementById,h=Document.prototype.getElementsByClassName,m=Document.prototype.getElementsByTagName,f=Document.prototype.getElementsByName,_=Function("return window")(),w=Function("return document")(),y="noModule"in document.createElement("script"),b=w.body.querySelector("#micro-app-template-style"),g=_.addEventListener,E=_.removeEventListener,v=_.setInterval,A=_.setTimeout,N=_.clearInterval,C=_.clearTimeout,O=w.addEventListener,P=w.removeEventListener;window.__MICRO_APP_BASE_APPLICATION__=!0,Object.assign(I,{rawSetAttribute:e,rawAppendChild:t,rawInsertBefore:n,rawReplaceChild:o,rawRemoveChild:r,rawAppend:i,rawPrepend:s,rawCreateElement:a,rawCreateElementNS:c,rawCreateDocumentFragment:l,rawQuerySelector:u,rawQuerySelectorAll:p,rawGetElementById:d,rawGetElementsByClassName:h,rawGetElementsByTagName:m,rawGetElementsByName:f,rawWindow:_,rawDocument:w,supportModuleScript:y,templateStyle:b,rawWindowAddEventListener:g,rawWindowRemoveEventListener:E,rawSetInterval:v,rawSetTimeout:A,rawClearInterval:N,rawClearTimeout:C,rawDocumentAddEventListener:O,rawDocumentRemoveEventListener:P})}}var x;function $(e,t){const{selectorText:n,cssText:o}=e;if(/^((html[\s>~,]+body)|(html|body|:root))$/.test(n))return o.replace(/^((html[\s>~,]+body)|(html|body|:root))/,t);if("*"===n)return o.replace("*",`${t} *`);const r=/(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;return o.replace(/^[\s\S]+{/,(e=>e.replace(/(^|,)([^,]+)/g,((e,n,o)=>r.test(o)?e.replace(r,t):`${n} ${t} ${o.replace(/^\s*/,"")}`))))}function k(e,t,n,o){return e.replace(/url\(["']?([^)"']+)["']?\)/gm,((e,r)=>{if(/^(data|blob):/.test(r))return e;if(/^(https?:)?\/\//.test(r)){if(!N())return e;{const t=r.replace(/^https?:/,"");if(-1!==n.indexOf(t))return e;r=r.replace(window.location.origin,"")}}return/^((\.\.?\/)|[^/])/.test(r)&&o&&(t=function(e){const t=e.split("/");return t.pop(),h(t.join("/")+"/")}(o)),`url("${w(r,t)}")`}))}function W(e,t,n){const o=B(Array.from(e.cssRules),t);return`@${n} ${e.conditionText} {${o}}`}function B(e,t){let n="";for(const o of e)switch(o.type){case x.STYLE_RULE:n+=$(o,t);break;case x.MEDIA_RULE:n+=W(o,t,"media");break;case x.SUPPORTS_RULE:n+=W(o,t,"supports");break;default:n+=o.cssText}return n.replace(/^\s+/,"")}function j(e,t,n,o,r,i){var s,a;if(!t.__MICRO_APP_HAS_SCOPED__){let c=k(B(Array.from(null!==(a=null===(s=e.sheet)||void 0===s?void 0:s.cssRules)&&void 0!==a?a:[]),o),r,n,i);N()&&(c=c.replace(/([;{]\s*content:\s*)([^\s"][^";}]*)/gm,((e,t,n)=>"none"===n||/^(url\()|(counter\()|(attr\()|(open-quote)|(close-quote)/.test(n)?e:`${t}"${n}"`))),t.textContent=c,t.__MICRO_APP_HAS_SCOPED__=!0}}function G(e,t){if(t.scopecss){const n=`${je.tagName}[name=${t.name}]`;let o=I.templateStyle;if(o||(I.templateStyle=o=C("style"),o.setAttribute("id","micro-app-template-style"),I.rawDocument.body.appendChild(o),o.sheet.disabled=!0),e.textContent)o.textContent=e.textContent,j(o,e,e.textContent,n,t.url,e.__MICRO_APP_LINK_PATH__),o.textContent="";else{const o=new MutationObserver((function(){var r,i;o.disconnect(),!e.textContent&&(null===(i=null===(r=e.sheet)||void 0===r?void 0:r.cssRules)||void 0===i?void 0:i.length)||e.hasAttribute("data-styled")||j(e,e,e.textContent,n,t.url,e.__MICRO_APP_LINK_PATH__)}));o.observe(e,{childList:!0})}}return e}function F(e,t){Object.defineProperties(e,{currentTarget:{get:()=>t},srcElement:{get:()=>t},target:{get:()=>t}})}function H(e){const t=new CustomEvent("load");F(t,e),i(e.onload)?e.onload(t):e.dispatchEvent(t)}function K(e){const t=new CustomEvent("error");F(t,e),i(e.onerror)?e.onerror(t):e.dispatchEvent(t)}!function(e){e[e.STYLE_RULE=1]="STYLE_RULE",e[e.MEDIA_RULE=4]="MEDIA_RULE",e[e.SUPPORTS_RULE=12]="SUPPORTS_RULE"}(x||(x={}));const q=new Map;function z(e,t,n,o,r=!1){const i=e.getAttribute("rel");let s=e.getAttribute("href"),a=null;if("stylesheet"===i&&s){if(s=w(s,n.url),r)return{url:s,info:{code:"",isGlobal:e.hasAttribute("global")}};{a=document.createComment(`link element with href=${s} move to micro-app-head as style element`);const t=document.createComment(`placeholder for link with href=${s}`);o.appendChild(t),n.source.links.set(s,{code:"",placeholder:t,isGlobal:e.hasAttribute("global")})}}else i&&["prefetch","preload","prerender","icon","apple-touch-icon"].includes(i)?r?a=document.createComment(`link element with rel=${i}${s?" & href="+s:""} removed by micro-app`):t.removeChild(e):s&&e.setAttribute("href",w(s,n.url));return r?{replaceComment:a}:a?t.replaceChild(a,e):void 0}function Y(e,t,n){const o=Array.from(t.source.links.entries()),r=[];for(const[e]of o){const n=q.get(e);n?r.push(n):r.push(L(e,t.name))}y(r,(e=>{!function(e,t,n,o,r){t.isGlobal&&!q.has(e)&&q.set(e,n);const i=C("style");i.textContent=n,i.__MICRO_APP_LINK_PATH__=e,o.replaceChild(G(i,r),t.placeholder),t.placeholder=null,t.code=n}(o[e.index][0],o[e.index][1],e.data,n,t)}),(e=>{u(e,t.name)}),(()=>{t.onLoad(e)}))}const V=new Map;function Q(e,t,n,o=!1){let r=null,i=e.getAttribute("src");if(e.hasAttribute("exclude"))r=document.createComment("script element with exclude attribute removed by micro-app");else{if(e.type&&!["text/javascript","text/ecmascript","application/javascript","application/ecmascript","module"].includes(e.type)||e.hasAttribute("ignore"))return null;if(I.supportModuleScript&&e.noModule||!I.supportModuleScript&&"module"===e.type)r=document.createComment((e.noModule?"noModule":"module")+" script ignored by micro-app");else if(i){i=w(i,n.url);const t={code:"",isExternal:!0,isDynamic:o,async:e.hasAttribute("async"),defer:e.defer||"module"===e.type,module:"module"===e.type,isGlobal:e.hasAttribute("global")};if(o)return{url:i,info:t};n.source.scripts.set(i,t),r=document.createComment(`script with src='${i}' extract by micro-app`)}else if(e.textContent){const t="inline-"+Math.random().toString(36).substr(2,15),i={code:e.textContent,isExternal:!1,isDynamic:o,async:!1,defer:"module"===e.type,module:"module"===e.type};if(o)return{url:t,info:i};n.source.scripts.set(t,i),r=document.createComment("inline script extract by micro-app")}else o||(r=document.createComment("script element removed by micro-app"))}return o?{replaceComment:r}:t.replaceChild(r,e)}function X(e,t){const n=Array.from(t.source.scripts.entries()),o=[],r=[];for(const[e,i]of n)if(i.isExternal){const n=V.get(e);n?i.code=n:i.defer||i.async||(o.push(L(e,t.name)),r.push([e,i]))}o.length?y(o,(e=>{!function(e,t,n){t.isGlobal&&!V.has(e)&&V.set(e,n);t.code=n}(r[e.index][0],r[e.index][1],e.data)}),(e=>{u(e,t.name)}),(()=>{t.onLoad(e)})):t.onLoad(e)}function J(e,t,n,o,r){var i;try{const s=te(e,t,n.code,n.module);if(t.inline||n.module){const a=C("script");if(Z(e,s,n.module,a,r),o)return a;null===(i=t.container)||void 0===i||i.querySelector("micro-app-body").appendChild(a)}else if(ee(s,n),o)return document.createComment("dynamic script extract by micro-app")}catch(e){console.error(`[micro-app from runScript] app ${t.name}: `,e)}}function Z(e,t,n,o,r){if(n){const n=new Blob([t],{type:"text/javascript"});o.src=URL.createObjectURL(n),o.setAttribute("type","module"),e.startsWith("inline-")||o.setAttribute("originSrc",e),r&&(r.moduleCount&&r.moduleCount--,o.onload=r.bind(o,0===r.moduleCount))}else o.textContent=t}function ee(e,t){t.code2Function||(t.code2Function=new Function(e)),t.code2Function.call(window)}function te(e,t,n,o){return a(je.plugins)&&(n=function(e,t,n,o){var r;if(s(o.global))for(const n of o.global)a(n)&&i(n.loader)&&(t=n.loader(t,e,n.options));if(s(null===(r=o.modules)||void 0===r?void 0:r[n]))for(const r of o.modules[n])a(r)&&i(r.loader)&&(t=r.loader(t,e,r.options));return t}(e,n,t.name,je.plugins)),t.sandBox&&!o?(I.rawWindow.__MICRO_APP_PROXY_WINDOW__=t.sandBox.proxyWindow,`;(function(window, self){with(window){;${n}\n}}).call(window.__MICRO_APP_PROXY_WINDOW__, window.__MICRO_APP_PROXY_WINDOW__, window.__MICRO_APP_PROXY_WINDOW__);`):n}function ne(e,t,n){const o=Array.from(e.children);o.length&&o.forEach((e=>{ne(e,t,n)}));for(const r of o)r instanceof HTMLLinkElement?r.hasAttribute("exclude")?e.replaceChild(document.createComment("link element with exclude attribute ignored by micro-app"),r):r.hasAttribute("ignore")?r.hasAttribute("href")&&r.setAttribute("href",w(r.getAttribute("href"),t.url)):z(r,e,t,n):r instanceof HTMLStyleElement?r.hasAttribute("exclude")?e.replaceChild(document.createComment("style element with exclude attribute ignored by micro-app"),r):t.scopecss&&!r.hasAttribute("ignore")&&n.appendChild(G(r,t)):r instanceof HTMLScriptElement?Q(r,e,t):r instanceof HTMLMetaElement||r instanceof HTMLTitleElement?e.removeChild(r):r instanceof HTMLImageElement&&r.hasAttribute("src")&&r.setAttribute("src",w(r.getAttribute("src"),t.url))}function oe(e,t){const n=function(e){const t=C("div");return t.innerHTML=e,t}(e),o=n.querySelector("micro-app-head"),r=n.querySelector("micro-app-body");if(!o||!r){const e=`element ${o?"body":"head"} is missing`;return t.onerror(new Error(e)),u(e,t.name)}ne(n,t,o),t.source.links.size?Y(n,t,o):t.onLoad(n),t.source.scripts.size?X(n,t):t.onLoad(n)}const re=new WeakMap;const ie=new WeakMap;const se=new WeakMap;function ae(e,t){if(se.has(t))return se.get(t);if(i(t)&&!function(e){if(ie.has(e))return ie.get(e);const t=e.toString(),n=e.prototype&&e.prototype.constructor===e&&Object.getOwnPropertyNames(e.prototype).length>1||/^function\s+[A-Z]/.test(t)||/^class\s+/.test(t);return ie.set(e,n),n}(t)&&!function(e){if(re.has(e))return re.get(e);const t=l(e);return re.set(e,t),t}(t)){const n=t.bind(e);for(const e in t)n[e]=t[e];return t.hasOwnProperty("prototype")&&!n.hasOwnProperty("prototype")&&(n.prototype=t.prototype),se.set(t,n),n}return t}const ce=new Map;let le=!1;const ue=new Map;function pe(){const{rawDocument:e,rawDocumentAddEventListener:t,rawDocumentRemoveEventListener:n}=I;!le&&function(){if(le=!0,Object.getOwnPropertyDescriptor(document,"onclick"))return p("Cannot redefine document property onclick");const e=document.onclick;document.onclick=null;let t=!1;function n(e){ce.forEach((t=>{i(t)&&t.call(document,e)}))}Object.defineProperty(document,"onclick",{configurable:!0,enumerable:!0,get(){const e=v();return e?ce.get(e):ce.get("base")},set(e){const o=v();o?ce.set(o,e):ce.set("base",e),!t&&i(e)&&(t=!0,I.rawDocumentAddEventListener.call(I.rawDocument,"click",n,!1))}}),e&&(document.onclick=e)}(),document.addEventListener=function(n,o,r){var i;const s=v();if(s&&(!(null===(i=Ce.get(s))||void 0===i?void 0:i.umdMode)||!l(o))){const e=ue.get(s);if(e){const t=e.get(n);t?t.add(o):e.set(n,new Set([o]))}else ue.set(s,new Map([[n,new Set([o])]]));o&&(o.__MICRO_MARK_OPTIONS__=r)}t.call(e,n,o,r)},document.removeEventListener=function(t,o,r){var i;const s=v();if(s&&(!(null===(i=Ce.get(s))||void 0===i?void 0:i.umdMode)||!l(o))){const e=ue.get(s);if(e){const n=e.get(t);(null==n?void 0:n.size)&&n.has(o)&&n.delete(o)}}n.call(e,t,o,r)}}function de(e,t){return"unmount"===e?`unmount-${t.__MICRO_APP_NAME__}`:e}const he=new class{constructor(){this.eventList=new Map}isLegalName(e){return!!e||(u("event-center: Invalid name"),!1)}on(e,t,n=!1){if(this.isLegalName(e)){if(!i(t))return u("event-center: Invalid callback function");let o=this.eventList.get(e);o?n&&Object.getOwnPropertyNames(o.data).length&&t(o.data):(o={data:{},callbacks:new Set},this.eventList.set(e,o)),o.callbacks.add(t)}}off(e,t){if(this.isLegalName(e)){const n=this.eventList.get(e);n&&(i(t)?n.callbacks.delete(t):n.callbacks.clear())}}dispatch(e,t){if(this.isLegalName(e)){if(!a(t))return u("event-center: data must be object");let n=this.eventList.get(e);if(n){if(n.data!==t){n.data=t;for(const e of n.callbacks)e(t)}}else n={data:t,callbacks:new Set},this.eventList.set(e,n)}}getData(e){var t;const n=this.eventList.get(e);return null!==(t=null==n?void 0:n.data)&&void 0!==t?t:null}};function me(e,t){return r(e)&&e?t?`__from_base_app_${e}__`:`__from_micro_app_${e}__`:""}class fe{addGlobalDataListener(e,t){const n=this.appName;n&&(e.__APP_NAME__=n,e.__AUTO_TRIGGER__=t),he.on("global",e,t)}removeGlobalDataListener(e){i(e)&&he.off("global",e)}setGlobalData(e){A(),he.dispatch("global",e)}getGlobalData(){return he.getData("global")}clearGlobalDataListener(){const e=this.appName,t=he.eventList.get("global");if(t)for(const n of t.callbacks)(e&&e===n.__APP_NAME__||!e&&!n.__APP_NAME__)&&t.callbacks.delete(n)}}class _e extends fe{addDataListener(e,t,n){he.on(me(f(e),!1),t,n)}removeDataListener(e,t){i(t)&&he.off(me(f(e),!1),t)}getData(e,t=!1){return he.getData(me(f(e),t))}setData(e,t){he.dispatch(me(f(e),!0),t)}clearDataListener(e){he.off(me(f(e),!1))}}class we extends fe{constructor(e){super(),this.appName=f(e),!this.appName&&u(`Invalid appName ${e}`)}addDataListener(e,t){e.__AUTO_TRIGGER__=t,he.on(me(this.appName,!0),e,t)}removeDataListener(e){i(e)&&he.off(me(this.appName,!0),e)}getData(){return he.getData(me(this.appName,!0))}dispatch(e){A(),he.dispatch(me(this.appName,!1),e);const t=Ce.get(this.appName);if((null==t?void 0:t.container)&&a(e)){const n=new CustomEvent("datachange",{detail:{data:e}});R(t.container).dispatchEvent(n)}}clearDataListener(){he.off(me(this.appName,!0))}}const ye=["System","__cjsWrapper","__REACT_ERROR_OVERLAY_GLOBAL_HOOK__"],be=["location"],ge={undefined:!0,Array:!0,Object:!0,String:!0,Boolean:!0,Math:!0,Number:!0,Symbol:!0,parseFloat:!0,Float32Array:!0};let Ee;function ve(e){Ee&&clearTimeout(Ee),Ee=setTimeout(e,0)}class Ae{constructor(e,t,n){this.scopeProperties=["webpackJsonp"],this.escapeProperties=[],this.injectedKeys=new Set,this.escapeKeys=new Set,this.active=!1,this.microWindow={};const o=I.rawWindow,i=I.rawDocument,s=new Map,a=e=>this.microWindow.hasOwnProperty(e)||o.hasOwnProperty(e);this.getScopeProperties(e),this.inject(this.microWindow,e,t),Object.assign(this,function(e){const t=e.__MICRO_APP_NAME__,n=new Map,o=new Map,r=new Map,{rawWindow:i,rawDocument:s,rawWindowAddEventListener:a,rawWindowRemoveEventListener:c,rawSetInterval:l,rawSetTimeout:u,rawClearInterval:p,rawClearTimeout:d,rawDocumentRemoveEventListener:h}=I;e.addEventListener=function(t,o,r){t=de(t,e);const s=n.get(t);s?s.add(o):n.set(t,new Set([o])),o&&(o.__MICRO_MARK_OPTIONS__=r),a.call(i,t,o,r)},e.removeEventListener=function(t,o,r){t=de(t,e);const s=n.get(t);(null==s?void 0:s.size)&&s.has(o)&&s.delete(o),c.call(i,t,o,r)},e.setInterval=function(e,t,...n){const r=l.call(i,e,t,...n);return o.set(r,{handler:e,timeout:t,args:n}),r},e.setTimeout=function(e,t,...n){const o=u.call(i,e,t,...n);return r.set(o,{handler:e,timeout:t,args:n}),o},e.clearInterval=function(e){o.delete(e),p.call(i,e)},e.clearTimeout=function(e){r.delete(e),d.call(i,e)};const m=new Map,f=new Map;let _,w=new Map,y=new Map;return{recordUmdEffect:()=>{n.forEach(((e,t)=>{e.size&&m.set(t,new Set(e))})),o.size&&(w=new Map(o)),r.size&&(y=new Map(r)),_=ce.get(t);const e=ue.get(t);e&&e.forEach(((e,t)=>{e.size&&f.set(t,new Set(e))}))},rebuildUmdEffect:()=>{m.forEach(((t,n)=>{for(const o of t)e.addEventListener(n,o,null==o?void 0:o.__MICRO_MARK_OPTIONS__)})),w.forEach((t=>{e.setInterval(t.handler,t.timeout,...t.args)})),y.forEach((t=>{e.setTimeout(t.handler,t.timeout,...t.args)})),_&&ce.set(t,_),E(t),f.forEach(((e,t)=>{for(const n of e)document.addEventListener(t,n,null==n?void 0:n.__MICRO_MARK_OPTIONS__)})),E(null)},releaseEffect:()=>{n.size&&(n.forEach(((e,t)=>{for(const n of e)c.call(i,t,n)})),n.clear()),o.size&&(o.forEach(((e,t)=>{p.call(i,t)})),o.clear()),r.size&&(r.forEach(((e,t)=>{d.call(i,t)})),r.clear()),ce.delete(t);const e=ue.get(t);e&&(e.forEach(((e,t)=>{for(const n of e)h.call(s,t,n)})),e.clear())}}}(this.microWindow)),this.proxyWindow=new Proxy(this.microWindow,{get:(t,s)=>{if(s===Symbol.unscopables)return ge;if(["window","self","globalThis"].includes(s))return this.proxyWindow;if("top"===s||"parent"===s)return o===o.parent?this.proxyWindow:Reflect.get(o,s);if("hasOwnProperty"===s)return a;if("document"===s||"eval"===s)switch(this.active&&(E(e),(n?ve:d)((()=>E(null)))),s){case"document":return i;case"eval":return eval}if(Reflect.has(t,s))return Reflect.get(t,s);if(this.scopeProperties.includes(s)||r(s)&&/^__MICRO_APP_/.test(s))return Reflect.get(t,s);const c=Reflect.get(o,s);return ae(o,c)},set:(e,t,n)=>{if(this.active){if(be.includes(t))Reflect.set(o,t,n);else if(e.hasOwnProperty(t)||!o.hasOwnProperty(t)||this.scopeProperties.includes(t))Reflect.set(e,t,n),this.injectedKeys.add(t);else{const r=Object.getOwnPropertyDescriptor(o,t),{writable:i,configurable:s,enumerable:a}=r;i&&(Object.defineProperty(e,t,{configurable:s,enumerable:a,writable:i,value:n}),this.injectedKeys.add(t))}(this.escapeProperties.includes(t)||ye.includes(t)&&!Reflect.has(o,t))&&!this.scopeProperties.includes(t)&&(Reflect.set(o,t,n),this.escapeKeys.add(t))}return!0},has:(e,t)=>this.scopeProperties.includes(t)?t in e:t in ge||t in e||t in o,getOwnPropertyDescriptor:(e,t)=>{if(e.hasOwnProperty(t))return s.set(t,"target"),Object.getOwnPropertyDescriptor(e,t);if(o.hasOwnProperty(t)){s.set(t,"rawWindow");const e=Object.getOwnPropertyDescriptor(o,t);return e&&!e.configurable&&(e.configurable=!0),e}},defineProperty:(e,t,n)=>"rawWindow"===s.get(t)?Reflect.defineProperty(o,t,n):Reflect.defineProperty(e,t,n),ownKeys:e=>Reflect.ownKeys(o).concat(Reflect.ownKeys(e)).filter((function(e){return!(e in this)&&(this[e]=!0)}),Object.create(null)),deleteProperty:(e,t)=>!e.hasOwnProperty(t)||(this.escapeKeys.has(t)&&Reflect.deleteProperty(o,t),Reflect.deleteProperty(e,t))})}start(e){this.active||(this.active=!0,this.microWindow.__MICRO_APP_BASE_ROUTE__=this.microWindow.__MICRO_APP_BASE_URL__=e,I.rawWindow._babelPolyfill&&(I.rawWindow._babelPolyfill=!1),1==++Ae.activeCount&&pe())}stop(){this.active&&(this.active=!1,this.releaseEffect(),this.microWindow.microApp.clearDataListener(),this.microWindow.microApp.clearGlobalDataListener(),this.injectedKeys.forEach((e=>{Reflect.deleteProperty(this.microWindow,e)})),this.injectedKeys.clear(),this.escapeKeys.forEach((e=>{Reflect.deleteProperty(I.rawWindow,e)})),this.escapeKeys.clear(),0==--Ae.activeCount&&(document.addEventListener=I.rawDocumentAddEventListener,document.removeEventListener=I.rawDocumentRemoveEventListener))}recordUmdSnapshot(){this.microWindow.__MICRO_APP_UMD_MODE__=!0,this.recordUmdEffect(),function(e){const t=e.appName;e.umdDataListeners={global:new Set,normal:new Set};const n=he.eventList.get("global");if(n)for(const o of n.callbacks)t===o.__APP_NAME__&&e.umdDataListeners.global.add(o);const o=he.eventList.get(me(t,!0));o&&(e.umdDataListeners.normal=new Set(o.callbacks))}(this.microWindow.microApp),this.recordUmdinjectedValues=new Map,this.injectedKeys.forEach((e=>{this.recordUmdinjectedValues.set(e,Reflect.get(this.microWindow,e))}))}rebuildUmdSnapshot(){this.recordUmdinjectedValues.forEach(((e,t)=>{Reflect.set(this.proxyWindow,t,e)})),this.rebuildUmdEffect(),function(e){for(const t of e.umdDataListeners.global)e.addGlobalDataListener(t,t.__AUTO_TRIGGER__);for(const t of e.umdDataListeners.normal)e.addDataListener(t,t.__AUTO_TRIGGER__)}(this.microWindow.microApp)}getScopeProperties(e){var t;if(a(je.plugins)){if(s(je.plugins.global))for(const e of je.plugins.global)a(e)&&(s(e.scopeProperties)&&(this.scopeProperties=this.scopeProperties.concat(e.scopeProperties)),s(e.escapeProperties)&&(this.escapeProperties=this.escapeProperties.concat(e.escapeProperties)));if(s(null===(t=je.plugins.modules)||void 0===t?void 0:t[e]))for(const t of je.plugins.modules[e])a(t)&&(s(t.scopeProperties)&&(this.scopeProperties=this.scopeProperties.concat(t.scopeProperties)),s(t.escapeProperties)&&(this.escapeProperties=this.escapeProperties.concat(t.escapeProperties)))}}inject(e,t,n){e.__MICRO_APP_ENVIRONMENT__=!0,e.__MICRO_APP_NAME__=t,e.__MICRO_APP_PUBLIC_PATH__=_(n),e.microApp=new we(t),e.rawWindow=I.rawWindow,e.rawDocument=I.rawDocument,e.removeDomScope=A}}function Ne(e,t,n,o){var r;if(!e)return u(`element does not exist in lifecycle ${n}`,t);e=R(e),A();const s=Object.assign({name:t,container:e},o&&{error:o}),a=new CustomEvent(n,{detail:s});!function(e,t){Object.defineProperties(e,{currentTarget:{get:()=>t},target:{get:()=>t}})}(a,e),i(null===(r=je.lifeCycles)||void 0===r?void 0:r[n])&&je.lifeCycles[n](a),e.dispatchEvent(a)}Ae.activeCount=0;const Ce=new Map;class Oe{constructor({name:e,url:t,ssrUrl:n,container:o,inline:r,scopecss:i,useSandbox:s,macro:a,baseroute:c}){this.status=S.NOT_LOADED,this.loadSourceLevel=0,this.umdHookMount=null,this.umdHookUnmount=null,this.libraryName=null,this.umdMode=!1,this.isPrefetch=!1,this.container=null,this.macro=!1,this.baseroute="",this.sandBox=null,this.container=null!=o?o:null,this.inline=null!=r&&r,this.baseroute=null!=c?c:"",this.ssrUrl=null!=n?n:"",this.name=e,this.url=t,this.useSandbox=s,this.scopecss=this.useSandbox&&i,this.macro=null!=a&&a,this.source={links:new Map,scripts:new Map},this.loadSourceCode(),this.useSandbox&&(this.sandBox=new Ae(e,t,this.macro))}loadSourceCode(){var e;this.status=S.LOADING_SOURCE_CODE,L((e=this).ssrUrl||e.url,e.name,{cache:"no-cache"}).then((t=>{if(!t){const t="html is empty, please check in detail";return e.onerror(new Error(t)),u(t,e.name)}oe(t=t.replace(/<head[^>]*>[\s\S]*?<\/head>/i,(e=>e.replace(/<head/i,"<micro-app-head").replace(/<\/head>/i,"</micro-app-head>"))).replace(/<body[^>]*>[\s\S]*?<\/body>/i,(e=>e.replace(/<body/i,"<micro-app-body").replace(/<\/body>/i,"</micro-app-body>"))),e)})).catch((t=>{u(`Failed to fetch data from ${e.url}, micro-app stop rendering`,e.name,t),e.onLoadError(t)}))}onLoad(e){if(2==++this.loadSourceLevel){if(this.source.html=e,this.isPrefetch||S.UNMOUNT===this.status)return;this.status=S.LOAD_SOURCE_FINISHED,this.mount()}}onLoadError(e){this.loadSourceLevel=-1,S.UNMOUNT!==this.status&&(this.onerror(e),this.status=S.LOAD_SOURCE_ERROR)}mount(e,t,n){var o,r,s;if("boolean"==typeof t&&t!==this.inline&&(this.inline=t),this.container=null!==(o=this.container)&&void 0!==o?o:e,this.baseroute=null!=n?n:this.baseroute,2!==this.loadSourceLevel)return void(this.status=S.LOADING_SOURCE_CODE);let a;if(Ne(this.container,this.name,U.BEFOREMOUNT),this.status=S.MOUNTING,O(this.source.html,this.container,!this.umdMode),null===(r=this.sandBox)||void 0===r||r.start(this.baseroute),this.umdMode){null===(s=this.sandBox)||void 0===s||s.rebuildUmdSnapshot();try{a=this.umdHookMount()}catch(e){u("an error occurred in the mount function \n",this.name,e)}this.handleMounted(a)}else{let e=!1;!function(e,t,n){const o=Array.from(e.entries()),r=[],i=[];for(const[e,s]of o)s.isDynamic||(s.defer||s.async?(s.isExternal&&!s.code?r.push(L(e,t.name)):r.push(s.code),i.push([e,s]),s.module&&(n.moduleCount=n.moduleCount?++n.moduleCount:1)):(J(e,t,s,!1),n(!1)));r.length?Promise.all(r).then((e=>{e.forEach(((e,o)=>{const[r,s]=i[o];s.code=s.code||e,J(r,t,s,!1,n),!s.module&&n(!1)})),n(void 0===n.moduleCount)})).catch((e=>{u(e,t.name),n(!0)})):n(!0)}(this.source.scripts,this,(t=>{var n;if(!this.umdMode){const{mount:e,unmount:t}=this.getUmdLibraryHooks();if(i(e)&&i(t)){this.umdHookMount=e,this.umdHookUnmount=t,this.umdMode=!0,null===(n=this.sandBox)||void 0===n||n.recordUmdSnapshot();try{a=this.umdHookMount()}catch(e){u("an error occurred in the mount function \n",this.name,e)}}}e||!0!==t&&!this.umdMode||(e=!0,this.handleMounted(a))}))}}handleMounted(e){c(e)?e.then((()=>this.dispatchMountedEvent())).catch((e=>this.onerror(e))):this.dispatchMountedEvent()}dispatchMountedEvent(){S.UNMOUNT!==this.status&&(this.status=S.MOUNTED,Ne(this.container,this.name,U.MOUNTED))}unmount(e){let t;if(this.status===S.LOAD_SOURCE_ERROR&&(e=!0),this.status=S.UNMOUNT,this.umdHookUnmount)try{t=this.umdHookUnmount()}catch(e){u("an error occurred in the unmount function \n",this.name,e)}!function(e){const t=new CustomEvent(`unmount-${e}`);window.dispatchEvent(t)}(this.name),this.handleUnmounted(e,t)}handleUnmounted(e,t){c(t)?t.then((()=>this.actionsForUnmount(e))).catch((()=>this.actionsForUnmount(e))):this.actionsForUnmount(e)}actionsForUnmount(e){var t;Ne(this.container,this.name,U.UNMOUNT),null===(t=this.sandBox)||void 0===t||t.stop(),e?this.actionsForCompletelyDestory():this.umdMode&&this.container.childElementCount&&O(this.container,this.source.html,!1),this.container=null}actionsForCompletelyDestory(){!this.useSandbox&&this.umdMode&&delete window[this.libraryName],Ce.delete(this.name)}onerror(e){Ne(this.container,this.name,U.ERROR,e)}getAppStatus(){return this.status}getUmdLibraryHooks(){var e,t;if(S.UNMOUNT!==this.status){const n=null!==(t=null===(e=this.sandBox)||void 0===e?void 0:e.proxyWindow)&&void 0!==t?t:I.rawWindow;return this.libraryName=R(this.container).getAttribute("library")||`micro-app-${this.name}`,"object"==typeof n[this.libraryName]?n[this.libraryName]:{}}return{}}}const Pe=new WeakMap;function Me(e,t,n){if(t instanceof HTMLStyleElement){if(t.hasAttribute("exclude")){const e=document.createComment("style element with exclude attribute ignored by micro-app");return Pe.set(t,e),e}return n.scopecss&&!t.hasAttribute("ignore")?G(t,n):t}if(t instanceof HTMLLinkElement){if(t.hasAttribute("exclude")){const e=document.createComment("link element with exclude attribute ignored by micro-app");return Pe.set(t,e),e}if(t.hasAttribute("ignore"))return t;const{url:o,info:r,replaceComment:i}=z(t,e,n,null,!0);if(o&&r){const e=C("style");return e.__MICRO_APP_LINK_PATH__=o,function(e,t,n,o,r){if(n.source.links.has(e))return r.textContent=n.source.links.get(e).code,G(r,n),void d((()=>H(o)));if(q.has(e)){const i=q.get(e);return t.code=i,n.source.links.set(e,t),r.textContent=i,G(r,n),void d((()=>H(o)))}L(e,n.name).then((i=>{t.code=i,n.source.links.set(e,t),t.isGlobal&&q.set(e,i),r.textContent=i,G(r,n),H(o)})).catch((e=>{u(e,n.name),K(o)}))}(o,r,n,t,e),Pe.set(t,e),e}return i?(Pe.set(t,i),i):t}if(t instanceof HTMLScriptElement){const{replaceComment:o,url:r,info:i}=Q(t,e,n,!0)||{};if(r&&i){if(i.isExternal){const e=function(e,t,n,o){const r=()=>H(o);if(n.source.scripts.has(e)){const t=n.source.scripts.get(e);return!t.module&&d(r),J(e,n,t,!0,r)}if(V.has(e)){const o=V.get(e);return t.code=o,n.source.scripts.set(e,t),!t.module&&d(r),J(e,n,t,!0,r)}let i;return i=n.inline||t.module?C("script"):document.createComment(`dynamic script with src='${e}' extract by micro-app`),L(e,n.name).then((s=>{t.code=s,n.source.scripts.set(e,t),t.isGlobal&&V.set(e,s);try{s=te(e,n,s,t.module),n.inline||t.module?Z(e,s,t.module,i,r):ee(s,t)}catch(t){console.error(`[micro-app from runDynamicScript] app ${n.name}: `,t,e)}!t.module&&H(o)})).catch((e=>{u(e,n.name),K(o)})),i}(r,i,n,t);return Pe.set(t,e),e}{const e=J(r,n,i,!0);return Pe.set(t,e),e}}return o?(Pe.set(t,o),o):t}return t}function Re(e,t,n,o,r){if(n===document.head){const i=e.container.querySelector("micro-app-head");return r&&!i.contains(r)?I.rawAppendChild.call(i,o):t!==I.rawRemoveChild||i.contains(o)?t===I.rawAppend||t===I.rawPrepend?t.call(i,o):t.call(i,o,r):n.contains(o)?t.call(n,o):o}if(n===document.body){const i=e.container.querySelector("micro-app-body");return r&&!i.contains(r)?I.rawAppendChild.call(i,o):t!==I.rawRemoveChild||i.contains(o)?t===I.rawAppend||t===I.rawPrepend?t.call(i,o):t.call(i,o,r):n.contains(o)?t.call(n,o):o}return t===I.rawAppend||t===I.rawPrepend?t.call(n,o):t.call(n,o,r)}function De(e){var t;return null!==(t=Pe.get(e))&&void 0!==t?t:e}function Se(e,t,n,o){if(null==t?void 0:t.__MICRO_APP_NAME__){const r=Ce.get(t.__MICRO_APP_NAME__);return(null==r?void 0:r.container)?Re(r,o,e,Me(e,t,r),n&&De(n)):o===I.rawAppend||o===I.rawPrepend?o.call(e,t):o.call(e,t,n)}if(o===I.rawAppend||o===I.rawPrepend){const n=v();if(!(t instanceof Node)&&n){const r=Ce.get(n);if(null==r?void 0:r.container){if(e===document.head)return o.call(r.container.querySelector("micro-app-head"),t);if(e===document.body)return o.call(r.container.querySelector("micro-app-body"),t)}}return o.call(e,t)}return o.call(e,t,n)}function Ue(){!function(){const e=I.rawDocument;function t(t){var n,o,r;const i=v();return i&&t&&!M(t)&&e===this?null!==(r=null===(o=null===(n=Ce.get(i))||void 0===n?void 0:n.container)||void 0===o?void 0:o.querySelector(t))&&void 0!==r?r:null:I.rawQuerySelector.call(this,t)}function n(t){var n,o,r;const i=v();return i&&t&&!M(t)&&e===this?null!==(r=null===(o=null===(n=Ce.get(i))||void 0===n?void 0:n.container)||void 0===o?void 0:o.querySelectorAll(t))&&void 0!==r?r:[]:I.rawQuerySelectorAll.call(this,t)}Document.prototype.createElement=function(e,t){return Le(I.rawCreateElement.call(this,e,t))},Document.prototype.createElementNS=function(e,t,n){return Le(I.rawCreateElementNS.call(this,e,t,n))},Document.prototype.createDocumentFragment=function(){return Le(I.rawCreateDocumentFragment.call(this))},Document.prototype.querySelector=t,Document.prototype.querySelectorAll=n,Document.prototype.getElementById=function(e){if(!v()||P(e))return I.rawGetElementById.call(this,e);try{return t.call(this,`#${e}`)}catch(t){return I.rawGetElementById.call(this,e)}},Document.prototype.getElementsByClassName=function(e){if(!v()||P(e))return I.rawGetElementsByClassName.call(this,e);try{return n.call(this,`.${e}`)}catch(t){return I.rawGetElementsByClassName.call(this,e)}},Document.prototype.getElementsByTagName=function(e){var t;const o=v();if(!o||M(e)||P(e)||!(null===(t=Ce.get(o))||void 0===t?void 0:t.inline)&&/^script$/i.test(e))return I.rawGetElementsByTagName.call(this,e);try{return n.call(this,e)}catch(t){return I.rawGetElementsByTagName.call(this,e)}},Document.prototype.getElementsByName=function(e){if(!v()||P(e))return I.rawGetElementsByName.call(this,e);try{return n.call(this,`[name=${e}]`)}catch(t){return I.rawGetElementsByName.call(this,e)}}}(),Element.prototype.setAttribute=function(e,t){if(/^micro-app(-\S+)?/i.test(this.tagName)&&"data"===e)if(a(t)){const e={};Object.getOwnPropertyNames(t).forEach((n=>{r(n)&&0===n.indexOf("__")||(e[n]=t[n])})),this.data=e}else"[object Object]"!==t&&p("property data must be an object",this.getAttribute("name"));else if((("src"===e||"srcset"===e)&&/^(img|script)$/i.test(this.tagName)||"href"===e&&/^link$/i.test(this.tagName))&&this.__MICRO_APP_NAME__&&Ce.has(this.__MICRO_APP_NAME__)){const n=Ce.get(this.__MICRO_APP_NAME__);I.rawSetAttribute.call(this,e,w(t,n.url))}else I.rawSetAttribute.call(this,e,t)},Node.prototype.appendChild=function(e){return Se(this,e,null,I.rawAppendChild)},Node.prototype.insertBefore=function(e,t){return Se(this,e,t,I.rawInsertBefore)},Node.prototype.replaceChild=function(e,t){return Se(this,e,t,I.rawReplaceChild)},Element.prototype.append=function(...e){let t=0;const n=e.length;for(;t<n;)Se(this,e[t],null,I.rawAppend),t++},Element.prototype.prepend=function(...e){let t=e.length;for(;t>0;)Se(this,e[t-1],null,I.rawPrepend),t--},Node.prototype.removeChild=function(e){if(null==e?void 0:e.__MICRO_APP_NAME__){const t=Ce.get(e.__MICRO_APP_NAME__);return(null==t?void 0:t.container)?Re(t,I.rawRemoveChild,this,De(e)):I.rawRemoveChild.call(this,e)}return I.rawRemoveChild.call(this,e)}}function Le(e){const t=v();return t&&(e.__MICRO_APP_NAME__=t),e}function Ie(){E(null),Document.prototype.createElement=I.rawCreateElement,Document.prototype.createElementNS=I.rawCreateElementNS,Document.prototype.createDocumentFragment=I.rawCreateDocumentFragment,Document.prototype.querySelector=I.rawQuerySelector,Document.prototype.querySelectorAll=I.rawQuerySelectorAll,Document.prototype.getElementById=I.rawGetElementById,Document.prototype.getElementsByClassName=I.rawGetElementsByClassName,Document.prototype.getElementsByTagName=I.rawGetElementsByTagName,Document.prototype.getElementsByName=I.rawGetElementsByName,Element.prototype.setAttribute=I.rawSetAttribute,Node.prototype.appendChild=I.rawAppendChild,Node.prototype.insertBefore=I.rawInsertBefore,Node.prototype.replaceChild=I.rawReplaceChild,Node.prototype.removeChild=I.rawRemoveChild,Element.prototype.append=I.rawAppend,Element.prototype.prepend=I.rawPrepend}let Te=!1;function xe(){$e(),Ce.forEach((e=>{e.container&&R(e.container).disconnectedCallback()})),!window.__MICRO_APP_UMD_MODE__&&Ce.clear(),ke.size&&(ke.clear(),Ie())}function $e(){window.__MICRO_APP_ENVIRONMENT__&&window.removeEventListener("unmount",xe,!1)}const ke=new Map;function We(e){class n extends HTMLElement{constructor(){super(),this.isWating=!1,this.cacheData=null,this.hasConnected=!1,this.appName="",this.appUrl="",this.ssrUrl="",this.version=t,this.handleAttributeUpdate=()=>{var e;this.isWating=!1;const t=f(this.getAttribute("name")),n=m(this.getAttribute("url"),this.appName);if(this.legalAttribute("name",t)&&this.legalAttribute("url",n)){const o=Ce.get(t);if(t!==this.appName&&o&&S.UNMOUNT!==o.getAppStatus()&&!o.isPrefetch)return this.setAttribute("name",this.appName),u(`an app named ${t} already exists`,this.appName);t===this.appName&&n===this.appUrl||(this.handleUnmount(t===this.appName),this.getDisposeResult("ssr")?this.ssrUrl=w(I.rawWindow.location.pathname,n):this.ssrUrl&&(this.ssrUrl=""),this.appName=t,this.appUrl=n,(null!==(e=this.shadowRoot)&&void 0!==e?e:this).innerHTML="",t!==this.getAttribute("name")&&this.setAttribute("name",this.appName),o&&o.url===this.appUrl&&o.ssrUrl===this.ssrUrl?this.handleAppMount(o):this.handleCreateApp())}else t!==this.appName&&this.setAttribute("name",this.appName)},this.querySelector("micro-app-head")||this.performWhenFirstCreated()}static get observedAttributes(){return["name","url"]}connectedCallback(){this.hasConnected=!0,ke.has(this)||this.performWhenFirstCreated(),d((()=>Ne(this,this.appName,U.CREATED))),this.initialMount()}disconnectedCallback(){this.hasConnected=!1,ke.delete(this),this.handleUnmount(this.getDisposeResult("destroy")||this.getDisposeResult("destory")),0===ke.size&&Ie()}attributeChangedCallback(e,t,n){if(this.legalAttribute(e,n)&&this[e===D.NAME?"appName":"appUrl"]!==n)if(e!==D.URL||this.appUrl)if(e!==D.NAME||this.appName)this.isWating||(this.isWating=!0,d(this.handleAttributeUpdate));else{const e=f(n);if(!e)return u(`Invalid attribute name ${n}`,this.appName);this.cacheData&&(je.setData(e,this.cacheData),this.cacheData=null),this.appName=e,e!==n&&this.setAttribute("name",this.appName),this.handleInitialNameAndUrl()}else{if(!(n=m(n,this.appName)))return u(`Invalid attribute url ${n}`,this.appName);this.appUrl=n,this.handleInitialNameAndUrl()}}handleInitialNameAndUrl(){this.hasConnected&&this.initialMount()}performWhenFirstCreated(){1===ke.set(this,!0).size&&(Ue(),function(){if(!Te){Te=!0;const e=C("style");e.setAttribute("type","text/css"),e.textContent=`\n${je.tagName}, micro-app-body { display: block; } \nmicro-app-head { display: none; }`,I.rawDocument.head.appendChild(e)}}(),$e(),window.__MICRO_APP_ENVIRONMENT__&&window.addEventListener("unmount",xe,!1))}initialMount(){if(!this.appName||!this.appUrl)return;this.getDisposeResult("shadowDOM")&&!this.shadowRoot&&i(this.attachShadow)&&this.attachShadow({mode:"open"}),this.getDisposeResult("ssr")?this.ssrUrl=w(I.rawWindow.location.pathname,this.appUrl):this.ssrUrl&&(this.ssrUrl="");const e=Ce.get(this.appName);if(e){const t=e.ssrUrl||e.url;t!==(this.ssrUrl||this.appUrl)||!e.isPrefetch&&e.getAppStatus()!==S.UNMOUNT?e.isPrefetch||e.getAppStatus()===S.UNMOUNT?(p(`the ${e.isPrefetch?"prefetch":"unmounted"} app with url: ${t} is replaced by a new app`,this.appName),this.handleCreateApp()):u(`an app named ${this.appName} already exists`,this.appName):this.handleAppMount(e)}else this.handleCreateApp()}legalAttribute(e,t){return!(!r(t)||!t)||(u(`unexpected attribute ${e}, please check again`,this.appName),!1)}handleAppMount(e){e.isPrefetch=!1,d((()=>{var t;return e.mount(null!==(t=this.shadowRoot)&&void 0!==t?t:this,this.getDisposeResult("inline"),this.getBaseRouteCompatible())}))}handleCreateApp(){var e;Ce.has(this.appName)&&Ce.get(this.appName).actionsForCompletelyDestory();const t=new Oe({name:this.appName,url:this.appUrl,ssrUrl:this.ssrUrl,container:null!==(e=this.shadowRoot)&&void 0!==e?e:this,inline:this.getDisposeResult("inline"),scopecss:!(this.getDisposeResult("disableScopecss")||this.getDisposeResult("shadowDOM")),useSandbox:!this.getDisposeResult("disableSandbox"),macro:this.getDisposeResult("macro"),baseroute:this.getBaseRouteCompatible()});Ce.set(this.appName,t)}handleUnmount(e){const t=Ce.get(this.appName);t&&S.UNMOUNT!==t.getAppStatus()&&t.unmount(e)}getDisposeResult(e){return(this.hasAttribute(e)||je[e])&&"false"!==this.getAttribute(e)}getBaseRouteCompatible(){var e,t;return null!==(t=null!==(e=this.getAttribute("baseroute"))&&void 0!==e?e:this.getAttribute("baseurl"))&&void 0!==t?t:""}set data(e){this.appName?je.setData(this.appName,e):this.cacheData=e}get data(){return this.appName?je.getData(this.appName,!0):this.cacheData?this.cacheData:null}}window.customElements.define(e,n)}function Be(e){if(!n)return u("preFetch is only supported in browser environment");b((()=>{i(e)&&(e=e()),function(e){const t=[];return s(e)&&e.forEach((e=>{a(e)&&(e.name=f(e.name),e.url=m(e.url,e.name),e.name&&e.url&&!Ce.has(e.name)&&t.push(e))})),t}(e).forEach((e=>{var t,n,o;const r=new Oe({name:e.name,url:e.url,scopecss:!(null!==(t=e.disableScopecss)&&void 0!==t?t:je.disableScopecss),useSandbox:!(null!==(n=e.disableSandbox)&&void 0!==n?n:je.disableSandbox),macro:null!==(o=e.macro)&&void 0!==o?o:je.macro});r.isPrefetch=!0,Ce.set(e.name,r)}))}))}var je=new class extends _e{constructor(){super(...arguments),this.tagName="micro-app",this.preFetch=Be}start(e){if(!n||!window.customElements)return u("micro-app is not supported in this environment");if(null==e?void 0:e.tagName){if(!/^micro-app(-\S+)?/.test(e.tagName))return u(`${e.tagName} is invalid tagName`);this.tagName=e.tagName}if(window.customElements.get(this.tagName))return p(`element ${this.tagName} is already defined`);if(T(),e&&a(e)&&(this.shadowDOM=e.shadowDOM,this.destroy=e.destroy,this.destory=e.destory,this.inline=e.inline,this.disableScopecss=e.disableScopecss,this.disableSandbox=e.disableSandbox,this.macro=e.macro,i(e.fetch)&&(this.fetch=e.fetch),a(e.lifeCycles)&&(this.lifeCycles=e.lifeCycles),e.preFetchApps&&Be(e.preFetchApps),e.globalAssets&&(a(t=e.globalAssets)&&b((()=>{if(s(t.js)){const e=t.js.filter((e=>r(e)&&e.includes(".js")&&!V.has(e))),n=[];e.forEach((e=>{n.push(L(e))})),y(n,(t=>{const n=e[t.index];V.has(n)||V.set(n,t.data)}),(e=>{u(e)}))}if(s(t.css)){const e=t.css.filter((e=>r(e)&&e.includes(".css")&&!q.has(e))),n=[];e.forEach((e=>{n.push(L(e))})),y(n,(t=>{const n=e[t.index];q.has(n)||q.set(n,t.data)}),(e=>{u(e)}))}}))),a(e.plugins))){const t=e.plugins.modules;if(a(t))for(const e in t){const n=f(e);n&&e!==n&&(t[n]=t[e],delete t[e])}this.plugins=e.plugins}var t;We(this.tagName)}};e.EventCenterForMicroApp=we,e.default=je,e.preFetch=Be,e.pureCreateElement=C,e.removeDomScope=A,e.version=t,Object.defineProperty(e,"__esModule",{value:!0})}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).microApp={})}(this,(function(e){"use strict";const t="0.5.1",n="undefined"!=typeof window,o="undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:Function("return this")();function r(e){return"string"==typeof e}function i(e){return"function"==typeof e}const s=Array.isArray;function a(e){return"[object Object]"===toString.call(e)}function c(e){return"[object Promise]"===toString.call(e)}function l(e){return i(e)&&0===e.name.indexOf("bound ")&&!e.hasOwnProperty("prototype")}function u(e,t=null,...n){const o=t&&r(t)?` app ${t}:`:"";r(e)?console.error(`[micro-app]${o} ${e}`,...n):console.error(`[micro-app]${o}`,e,...n)}function p(e,t=null,...n){const o=t&&r(t)?` app ${t}:`:"";r(e)?console.warn(`[micro-app]${o} ${e}`,...n):console.warn(`[micro-app]${o}`,e,...n)}function d(e,...t){Promise.resolve().then(e.bind(null,...t))}function h(e){return e.startsWith("//")?`${location.protocol}${e}`:e}function m(e,t=null){if(!r(e)||!e)return"";try{const{origin:t,pathname:n,search:o}=new URL(h(e));if(/\.(\w+)$/.test(n))return`${t}${n}${o}`;const r=`${t}${n}/`.replace(/\/\/$/,"/");return/^https?:\/\//.test(r)?`${r}${o}`:""}catch(e){return u(e,t),""}}function f(e){return r(e)&&e?e.replace(/(^\d+)|([^\w\d-_])/gi,""):""}function _(e){const{origin:t,pathname:n}=new URL(e);if(/\.(\w+)$/.test(n)){const e=`${t}${n}`.split("/");return e.pop(),e.join("/")+"/"}return`${t}${n}/`.replace(/\/\/$/,"/")}function w(e,t){return!e||/^((((ht|f)tps?)|file):)?\/\//.test(e)||/^(data|blob):/.test(e)?e:new URL(e,_(h(t))).toString()}function y(e,t,n,o){let r=0;function i(){++r===e.length&&o&&o()}e.forEach(((e,o)=>{c(e)?e.then((e=>{t({data:e,index:o}),i()})).catch((e=>{n({error:e,index:o}),i()})):(t({data:e,index:o}),i())}))}const b=o.requestIdleCallback||function(e){const t=Date.now();return setTimeout((function(){e({didTimeout:!1,timeRemaining:()=>Math.max(0,50-(Date.now()-t))})}),1)};let g=null;function E(e){g=e}function A(){return g}function v(){E(null)}function N(){return/Safari/.test(navigator.userAgent)&&!/Chrome/.test(navigator.userAgent)}function C(e,t){const n=document.createElement(e,t);return n.__MICRO_APP_NAME__&&delete n.__MICRO_APP_NAME__,n}function O(e,t,n){if(t.innerHTML="",n){const n=e.cloneNode(!0),o=document.createDocumentFragment();Array.from(n.childNodes).forEach((e=>{o.appendChild(e)})),t.appendChild(o)}else Array.from(e.childNodes).forEach((e=>{t.appendChild(e)}))}function P(e){return!e||/(^\d)|([^\w\d-_\u4e00-\u9fa5])/gi.test(e)}function M(e){return/^body$/i.test(e)||/^head$/i.test(e)||/^html$/i.test(e)}function R(e){return function(e){return"undefined"!=typeof ShadowRoot&&e instanceof ShadowRoot}(e)?e.host:e}var D,S,U;function L(e,t=null,n={}){return i(je.fetch)?je.fetch(e,n,t):fetch(e,n).then((e=>e.text()))}!function(e){e.NAME="name",e.URL="url"}(D||(D={})),function(e){e.NOT_LOADED="NOT_LOADED",e.LOADING_SOURCE_CODE="LOADING_SOURCE_CODE",e.LOAD_SOURCE_FINISHED="LOAD_SOURCE_FINISHED",e.LOAD_SOURCE_ERROR="LOAD_SOURCE_ERROR",e.MOUNTING="MOUNTING",e.MOUNTED="MOUNTED",e.UNMOUNT="UNMOUNT"}(S||(S={})),function(e){e.CREATED="created",e.BEFOREMOUNT="beforemount",e.MOUNTED="mounted",e.UNMOUNT="unmount",e.ERROR="error"}(U||(U={}));const I={};function T(){if(n){const e=Element.prototype.setAttribute,t=Node.prototype.appendChild,n=Node.prototype.insertBefore,o=Node.prototype.replaceChild,r=Node.prototype.removeChild,i=Element.prototype.append,s=Element.prototype.prepend,a=Document.prototype.createElement,c=Document.prototype.createElementNS,l=Document.prototype.createDocumentFragment,u=Document.prototype.querySelector,p=Document.prototype.querySelectorAll,d=Document.prototype.getElementById,h=Document.prototype.getElementsByClassName,m=Document.prototype.getElementsByTagName,f=Document.prototype.getElementsByName,_=Function("return window")(),w=Function("return document")(),y="noModule"in document.createElement("script"),b=w.body.querySelector("#micro-app-template-style"),g=_.addEventListener,E=_.removeEventListener,A=_.setInterval,v=_.setTimeout,N=_.clearInterval,C=_.clearTimeout,O=w.addEventListener,P=w.removeEventListener;window.__MICRO_APP_BASE_APPLICATION__=!0,Object.assign(I,{rawSetAttribute:e,rawAppendChild:t,rawInsertBefore:n,rawReplaceChild:o,rawRemoveChild:r,rawAppend:i,rawPrepend:s,rawCreateElement:a,rawCreateElementNS:c,rawCreateDocumentFragment:l,rawQuerySelector:u,rawQuerySelectorAll:p,rawGetElementById:d,rawGetElementsByClassName:h,rawGetElementsByTagName:m,rawGetElementsByName:f,rawWindow:_,rawDocument:w,supportModuleScript:y,templateStyle:b,rawWindowAddEventListener:g,rawWindowRemoveEventListener:E,rawSetInterval:A,rawSetTimeout:v,rawClearInterval:N,rawClearTimeout:C,rawDocumentAddEventListener:O,rawDocumentRemoveEventListener:P})}}var x;function $(e,t){const{selectorText:n,cssText:o}=e;if(/^((html[\s>~,]+body)|(html|body|:root))$/.test(n))return o.replace(/^((html[\s>~,]+body)|(html|body|:root))/,t);if("*"===n)return o.replace("*",`${t} *`);const r=/(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;return o.replace(/^[\s\S]+{/,(e=>e.replace(/(^|,)([^,]+)/g,((e,n,o)=>r.test(o)?e.replace(r,t):`${n} ${t} ${o.replace(/^\s*/,"")}`))))}function k(e,t,n,o){return e.replace(/url\(["']?([^)"']+)["']?\)/gm,((e,r)=>{if(/^(data|blob):/.test(r))return e;if(/^(https?:)?\/\//.test(r)){if(!N())return e;{const t=r.replace(/^https?:/,"");if(-1!==n.indexOf(t))return e;r=r.replace(window.location.origin,"")}}return/^((\.\.?\/)|[^/])/.test(r)&&o&&(t=function(e){const t=e.split("/");return t.pop(),h(t.join("/")+"/")}(o)),`url("${w(r,t)}")`}))}function W(e,t,n){const o=B(Array.from(e.cssRules),t);return`@${n} ${e.conditionText} {${o}}`}function B(e,t){let n="";for(const o of e)switch(o.type){case x.STYLE_RULE:n+=$(o,t);break;case x.MEDIA_RULE:n+=W(o,t,"media");break;case x.SUPPORTS_RULE:n+=W(o,t,"supports");break;default:n+=o.cssText}return n.replace(/^\s+/,"")}function j(e,t,n,o,r,i){var s,a;if(!t.__MICRO_APP_HAS_SCOPED__){let c=k(B(Array.from(null!==(a=null===(s=e.sheet)||void 0===s?void 0:s.cssRules)&&void 0!==a?a:[]),o),r,n,i);N()&&(c=c.replace(/([;{]\s*content:\s*)([^\s"][^";}]*)/gm,((e,t,n)=>"none"===n||/^(url\()|(counter\()|(attr\()|(open-quote)|(close-quote)/.test(n)?e:`${t}"${n}"`))),t.textContent=c,t.__MICRO_APP_HAS_SCOPED__=!0}}function G(e,t){if(t.scopecss){const n=`${je.tagName}[name=${t.name}]`;let o=I.templateStyle;if(o||(I.templateStyle=o=C("style"),o.setAttribute("id","micro-app-template-style"),I.rawDocument.body.appendChild(o),o.sheet.disabled=!0),e.textContent)o.textContent=e.textContent,j(o,e,e.textContent,n,t.url,e.__MICRO_APP_LINK_PATH__),o.textContent="";else{const o=new MutationObserver((function(){var r,i;o.disconnect(),!e.textContent&&(null===(i=null===(r=e.sheet)||void 0===r?void 0:r.cssRules)||void 0===i?void 0:i.length)||e.hasAttribute("data-styled")||j(e,e,e.textContent,n,t.url,e.__MICRO_APP_LINK_PATH__)}));o.observe(e,{childList:!0})}}return e}function F(e,t){Object.defineProperties(e,{currentTarget:{get:()=>t},srcElement:{get:()=>t},target:{get:()=>t}})}function H(e){const t=new CustomEvent("load");F(t,e),i(e.onload)?e.onload(t):e.dispatchEvent(t)}function K(e){const t=new CustomEvent("error");F(t,e),i(e.onerror)?e.onerror(t):e.dispatchEvent(t)}!function(e){e[e.STYLE_RULE=1]="STYLE_RULE",e[e.MEDIA_RULE=4]="MEDIA_RULE",e[e.SUPPORTS_RULE=12]="SUPPORTS_RULE"}(x||(x={}));const q=new Map;function z(e,t,n,o=!1){const r=e.getAttribute("rel");let i=e.getAttribute("href"),s=null;if("stylesheet"===r&&i){if(i=w(i,n.url),o)return{url:i,info:{code:"",isGlobal:e.hasAttribute("global")}};s=document.createComment(`link element with href=${i} move to micro-app-head as style element`),n.source.links.set(i,{code:"",placeholder:s,isGlobal:e.hasAttribute("global")})}else r&&["prefetch","preload","prerender","icon","apple-touch-icon"].includes(r)?o?s=document.createComment(`link element with rel=${r}${i?" & href="+i:""} removed by micro-app`):t.removeChild(e):i&&e.setAttribute("href",w(i,n.url));return o?{replaceComment:s}:s?t.replaceChild(s,e):void 0}function Y(e,t,n){const o=Array.from(t.source.links.entries()),r=[];for(const[e]of o){const n=q.get(e);n?r.push(n):r.push(L(e,t.name))}y(r,(e=>{!function(e,t,n,o,r){t.isGlobal&&!q.has(e)&&q.set(e,n);const i=C("style");i.textContent=n,i.__MICRO_APP_LINK_PATH__=e,i.setAttribute("data-origin-href",e),o.replaceChild(G(i,r),t.placeholder),t.placeholder=null,t.code=n}(o[e.index][0],o[e.index][1],e.data,n,t)}),(e=>{u(e,t.name)}),(()=>{t.onLoad(e)}))}const V=new Map;function Q(e,t,n,o=!1){let r=null,i=e.getAttribute("src");if(e.hasAttribute("exclude"))r=document.createComment("script element with exclude attribute removed by micro-app");else{if(e.type&&!["text/javascript","text/ecmascript","application/javascript","application/ecmascript","module"].includes(e.type)||e.hasAttribute("ignore"))return null;if(I.supportModuleScript&&e.noModule||!I.supportModuleScript&&"module"===e.type)r=document.createComment((e.noModule?"noModule":"module")+" script ignored by micro-app");else if(i){i=w(i,n.url);const t={code:"",isExternal:!0,isDynamic:o,async:e.hasAttribute("async"),defer:e.defer||"module"===e.type,module:"module"===e.type,isGlobal:e.hasAttribute("global")};if(o)return{url:i,info:t};n.source.scripts.set(i,t),r=document.createComment(`script with src='${i}' extract by micro-app`)}else if(e.textContent){const t="inline-"+Math.random().toString(36).substr(2,15),i={code:e.textContent,isExternal:!1,isDynamic:o,async:!1,defer:"module"===e.type,module:"module"===e.type};if(o)return{url:t,info:i};n.source.scripts.set(t,i),r=document.createComment("inline script extract by micro-app")}else o||(r=document.createComment("script element removed by micro-app"))}return o?{replaceComment:r}:t.replaceChild(r,e)}function X(e,t){const n=Array.from(t.source.scripts.entries()),o=[],r=[];for(const[e,i]of n)if(i.isExternal){const n=V.get(e);n?i.code=n:i.defer||i.async||(o.push(L(e,t.name)),r.push([e,i]))}o.length?y(o,(e=>{!function(e,t,n){t.isGlobal&&!V.has(e)&&V.set(e,n);t.code=n}(r[e.index][0],r[e.index][1],e.data)}),(e=>{u(e,t.name)}),(()=>{t.onLoad(e)})):t.onLoad(e)}function J(e,t,n,o,r){var i;try{const s=te(e,t,n.code,n.module);if(t.inline||n.module){const a=C("script");if(Z(e,s,n.module,a,r),o)return a;null===(i=t.container)||void 0===i||i.querySelector("micro-app-body").appendChild(a)}else if(ee(s,n),o)return document.createComment("dynamic script extract by micro-app")}catch(e){console.error(`[micro-app from runScript] app ${t.name}: `,e)}}function Z(e,t,n,o,r){if(n){const e=new Blob([t],{type:"text/javascript"});o.src=URL.createObjectURL(e),o.setAttribute("type","module"),r&&(r.moduleCount&&r.moduleCount--,o.onload=r.bind(o,0===r.moduleCount))}else o.textContent=t;e.startsWith("inline-")||o.setAttribute("data-origin-src",e)}function ee(e,t){t.code2Function||(t.code2Function=new Function(e)),t.code2Function.call(window)}function te(e,t,n,o){return a(je.plugins)&&(n=function(e,t,n,o){var r;if(s(o.global))for(const n of o.global)a(n)&&i(n.loader)&&(t=n.loader(t,e,n.options));if(s(null===(r=o.modules)||void 0===r?void 0:r[n]))for(const r of o.modules[n])a(r)&&i(r.loader)&&(t=r.loader(t,e,r.options));return t}(e,n,t.name,je.plugins)),t.sandBox&&!o?(I.rawWindow.__MICRO_APP_PROXY_WINDOW__=t.sandBox.proxyWindow,`;(function(window, self){with(window){;${n}\n}}).call(window.__MICRO_APP_PROXY_WINDOW__, window.__MICRO_APP_PROXY_WINDOW__, window.__MICRO_APP_PROXY_WINDOW__);`):n}function ne(e,t,n){const o=Array.from(e.children);o.length&&o.forEach((e=>{ne(e,t)}));for(const n of o)n instanceof HTMLLinkElement?n.hasAttribute("exclude")?e.replaceChild(document.createComment("link element with exclude attribute ignored by micro-app"),n):n.hasAttribute("ignore")?n.hasAttribute("href")&&n.setAttribute("href",w(n.getAttribute("href"),t.url)):z(n,e,t):n instanceof HTMLStyleElement?n.hasAttribute("exclude")?e.replaceChild(document.createComment("style element with exclude attribute ignored by micro-app"),n):t.scopecss&&!n.hasAttribute("ignore")&&G(n,t):n instanceof HTMLScriptElement?Q(n,e,t):n instanceof HTMLMetaElement||n instanceof HTMLTitleElement?e.removeChild(n):n instanceof HTMLImageElement&&n.hasAttribute("src")&&n.setAttribute("src",w(n.getAttribute("src"),t.url))}function oe(e,t){const n=function(e){const t=C("div");return t.innerHTML=e,t}(e),o=n.querySelector("micro-app-head"),r=n.querySelector("micro-app-body");if(!o||!r){const e=`element ${o?"body":"head"} is missing`;return t.onerror(new Error(e)),u(e,t.name)}ne(n,t),t.source.links.size?Y(n,t,o):t.onLoad(n),t.source.scripts.size?X(n,t):t.onLoad(n)}const re=new WeakMap;const ie=new WeakMap;const se=new WeakMap;function ae(e,t){if(se.has(t))return se.get(t);if(i(t)&&!function(e){if(ie.has(e))return ie.get(e);const t=e.toString(),n=e.prototype&&e.prototype.constructor===e&&Object.getOwnPropertyNames(e.prototype).length>1||/^function\s+[A-Z]/.test(t)||/^class\s+/.test(t);return ie.set(e,n),n}(t)&&!function(e){if(re.has(e))return re.get(e);const t=l(e);return re.set(e,t),t}(t)){const n=t.bind(e);for(const e in t)n[e]=t[e];return t.hasOwnProperty("prototype")&&!n.hasOwnProperty("prototype")&&(n.prototype=t.prototype),se.set(t,n),n}return t}const ce=new Map;let le=!1;const ue=new Map;function pe(){const{rawDocument:e,rawDocumentAddEventListener:t,rawDocumentRemoveEventListener:n}=I;!le&&function(){if(le=!0,Object.getOwnPropertyDescriptor(document,"onclick"))return p("Cannot redefine document property onclick");const e=document.onclick;document.onclick=null;let t=!1;function n(e){ce.forEach((t=>{i(t)&&t.call(document,e)}))}Object.defineProperty(document,"onclick",{configurable:!0,enumerable:!0,get(){const e=A();return e?ce.get(e):ce.get("base")},set(e){const o=A();o?ce.set(o,e):ce.set("base",e),!t&&i(e)&&(t=!0,I.rawDocumentAddEventListener.call(I.rawDocument,"click",n,!1))}}),e&&(document.onclick=e)}(),document.addEventListener=function(n,o,r){var i;const s=A();if(s&&(!(null===(i=Ce.get(s))||void 0===i?void 0:i.umdMode)||!l(o))){const e=ue.get(s);if(e){const t=e.get(n);t?t.add(o):e.set(n,new Set([o]))}else ue.set(s,new Map([[n,new Set([o])]]));o&&(o.__MICRO_MARK_OPTIONS__=r)}t.call(e,n,o,r)},document.removeEventListener=function(t,o,r){var i;const s=A();if(s&&(!(null===(i=Ce.get(s))||void 0===i?void 0:i.umdMode)||!l(o))){const e=ue.get(s);if(e){const n=e.get(t);(null==n?void 0:n.size)&&n.has(o)&&n.delete(o)}}n.call(e,t,o,r)}}function de(e,t){return"unmount"===e?`unmount-${t.__MICRO_APP_NAME__}`:e}const he=new class{constructor(){this.eventList=new Map}isLegalName(e){return!!e||(u("event-center: Invalid name"),!1)}on(e,t,n=!1){if(this.isLegalName(e)){if(!i(t))return u("event-center: Invalid callback function");let o=this.eventList.get(e);o?n&&Object.getOwnPropertyNames(o.data).length&&t(o.data):(o={data:{},callbacks:new Set},this.eventList.set(e,o)),o.callbacks.add(t)}}off(e,t){if(this.isLegalName(e)){const n=this.eventList.get(e);n&&(i(t)?n.callbacks.delete(t):n.callbacks.clear())}}dispatch(e,t){if(this.isLegalName(e)){if(!a(t))return u("event-center: data must be object");let n=this.eventList.get(e);if(n){if(n.data!==t){n.data=t;for(const e of n.callbacks)e(t)}}else n={data:t,callbacks:new Set},this.eventList.set(e,n)}}getData(e){var t;const n=this.eventList.get(e);return null!==(t=null==n?void 0:n.data)&&void 0!==t?t:null}};function me(e,t){return r(e)&&e?t?`__from_base_app_${e}__`:`__from_micro_app_${e}__`:""}class fe{addGlobalDataListener(e,t){const n=this.appName;n&&(e.__APP_NAME__=n,e.__AUTO_TRIGGER__=t),he.on("global",e,t)}removeGlobalDataListener(e){i(e)&&he.off("global",e)}setGlobalData(e){v(),he.dispatch("global",e)}getGlobalData(){return he.getData("global")}clearGlobalDataListener(){const e=this.appName,t=he.eventList.get("global");if(t)for(const n of t.callbacks)(e&&e===n.__APP_NAME__||!e&&!n.__APP_NAME__)&&t.callbacks.delete(n)}}class _e extends fe{addDataListener(e,t,n){he.on(me(f(e),!1),t,n)}removeDataListener(e,t){i(t)&&he.off(me(f(e),!1),t)}getData(e,t=!1){return he.getData(me(f(e),t))}setData(e,t){he.dispatch(me(f(e),!0),t)}clearDataListener(e){he.off(me(f(e),!1))}}class we extends fe{constructor(e){super(),this.appName=f(e),!this.appName&&u(`Invalid appName ${e}`)}addDataListener(e,t){e.__AUTO_TRIGGER__=t,he.on(me(this.appName,!0),e,t)}removeDataListener(e){i(e)&&he.off(me(this.appName,!0),e)}getData(){return he.getData(me(this.appName,!0))}dispatch(e){v(),he.dispatch(me(this.appName,!1),e);const t=Ce.get(this.appName);if((null==t?void 0:t.container)&&a(e)){const n=new CustomEvent("datachange",{detail:{data:e}});R(t.container).dispatchEvent(n)}}clearDataListener(){he.off(me(this.appName,!0))}}const ye=["System","__cjsWrapper","__REACT_ERROR_OVERLAY_GLOBAL_HOOK__"],be=["location"],ge={undefined:!0,Array:!0,Object:!0,String:!0,Boolean:!0,Math:!0,Number:!0,Symbol:!0,parseFloat:!0,Float32Array:!0};let Ee;function Ae(e){Ee&&clearTimeout(Ee),Ee=setTimeout(e,0)}class ve{constructor(e,t,n){this.scopeProperties=["webpackJsonp"],this.escapeProperties=[],this.injectedKeys=new Set,this.escapeKeys=new Set,this.active=!1,this.microWindow={};const o=I.rawWindow,i=I.rawDocument,s=new Map,a=e=>this.microWindow.hasOwnProperty(e)||o.hasOwnProperty(e);this.getScopeProperties(e),this.inject(this.microWindow,e,t),Object.assign(this,function(e){const t=e.__MICRO_APP_NAME__,n=new Map,o=new Map,r=new Map,{rawWindow:i,rawDocument:s,rawWindowAddEventListener:a,rawWindowRemoveEventListener:c,rawSetInterval:l,rawSetTimeout:u,rawClearInterval:p,rawClearTimeout:d,rawDocumentRemoveEventListener:h}=I;e.addEventListener=function(t,o,r){t=de(t,e);const s=n.get(t);s?s.add(o):n.set(t,new Set([o])),o&&(o.__MICRO_MARK_OPTIONS__=r),a.call(i,t,o,r)},e.removeEventListener=function(t,o,r){t=de(t,e);const s=n.get(t);(null==s?void 0:s.size)&&s.has(o)&&s.delete(o),c.call(i,t,o,r)},e.setInterval=function(e,t,...n){const r=l.call(i,e,t,...n);return o.set(r,{handler:e,timeout:t,args:n}),r},e.setTimeout=function(e,t,...n){const o=u.call(i,e,t,...n);return r.set(o,{handler:e,timeout:t,args:n}),o},e.clearInterval=function(e){o.delete(e),p.call(i,e)},e.clearTimeout=function(e){r.delete(e),d.call(i,e)};const m=new Map,f=new Map;let _,w=new Map,y=new Map;return{recordUmdEffect:()=>{n.forEach(((e,t)=>{e.size&&m.set(t,new Set(e))})),o.size&&(w=new Map(o)),r.size&&(y=new Map(r)),_=ce.get(t);const e=ue.get(t);e&&e.forEach(((e,t)=>{e.size&&f.set(t,new Set(e))}))},rebuildUmdEffect:()=>{m.forEach(((t,n)=>{for(const o of t)e.addEventListener(n,o,null==o?void 0:o.__MICRO_MARK_OPTIONS__)})),w.forEach((t=>{e.setInterval(t.handler,t.timeout,...t.args)})),y.forEach((t=>{e.setTimeout(t.handler,t.timeout,...t.args)})),_&&ce.set(t,_),E(t),f.forEach(((e,t)=>{for(const n of e)document.addEventListener(t,n,null==n?void 0:n.__MICRO_MARK_OPTIONS__)})),E(null)},releaseEffect:()=>{n.size&&(n.forEach(((e,t)=>{for(const n of e)c.call(i,t,n)})),n.clear()),o.size&&(o.forEach(((e,t)=>{p.call(i,t)})),o.clear()),r.size&&(r.forEach(((e,t)=>{d.call(i,t)})),r.clear()),ce.delete(t);const e=ue.get(t);e&&(e.forEach(((e,t)=>{for(const n of e)h.call(s,t,n)})),e.clear())}}}(this.microWindow)),this.proxyWindow=new Proxy(this.microWindow,{get:(t,s)=>{if(s===Symbol.unscopables)return ge;if(["window","self","globalThis"].includes(s))return this.proxyWindow;if("top"===s||"parent"===s)return o===o.parent?this.proxyWindow:Reflect.get(o,s);if("hasOwnProperty"===s)return a;if("document"===s||"eval"===s)switch(this.active&&(E(e),(n?Ae:d)((()=>E(null)))),s){case"document":return i;case"eval":return eval}if(Reflect.has(t,s))return Reflect.get(t,s);if(this.scopeProperties.includes(s)||r(s)&&/^__MICRO_APP_/.test(s))return Reflect.get(t,s);const c=Reflect.get(o,s);return ae(o,c)},set:(e,t,n)=>{if(this.active){if(be.includes(t))Reflect.set(o,t,n);else if(e.hasOwnProperty(t)||!o.hasOwnProperty(t)||this.scopeProperties.includes(t))Reflect.set(e,t,n),this.injectedKeys.add(t);else{const r=Object.getOwnPropertyDescriptor(o,t),{writable:i,configurable:s,enumerable:a}=r;i&&(Object.defineProperty(e,t,{configurable:s,enumerable:a,writable:i,value:n}),this.injectedKeys.add(t))}(this.escapeProperties.includes(t)||ye.includes(t)&&!Reflect.has(o,t))&&!this.scopeProperties.includes(t)&&(Reflect.set(o,t,n),this.escapeKeys.add(t))}return!0},has:(e,t)=>this.scopeProperties.includes(t)?t in e:t in ge||t in e||t in o,getOwnPropertyDescriptor:(e,t)=>{if(e.hasOwnProperty(t))return s.set(t,"target"),Object.getOwnPropertyDescriptor(e,t);if(o.hasOwnProperty(t)){s.set(t,"rawWindow");const e=Object.getOwnPropertyDescriptor(o,t);return e&&!e.configurable&&(e.configurable=!0),e}},defineProperty:(e,t,n)=>"rawWindow"===s.get(t)?Reflect.defineProperty(o,t,n):Reflect.defineProperty(e,t,n),ownKeys:e=>Reflect.ownKeys(o).concat(Reflect.ownKeys(e)).filter((function(e){return!(e in this)&&(this[e]=!0)}),Object.create(null)),deleteProperty:(e,t)=>!e.hasOwnProperty(t)||(this.escapeKeys.has(t)&&Reflect.deleteProperty(o,t),Reflect.deleteProperty(e,t))})}start(e){this.active||(this.active=!0,this.microWindow.__MICRO_APP_BASE_ROUTE__=this.microWindow.__MICRO_APP_BASE_URL__=e,I.rawWindow._babelPolyfill&&(I.rawWindow._babelPolyfill=!1),1==++ve.activeCount&&pe())}stop(){this.active&&(this.active=!1,this.releaseEffect(),this.microWindow.microApp.clearDataListener(),this.microWindow.microApp.clearGlobalDataListener(),this.injectedKeys.forEach((e=>{Reflect.deleteProperty(this.microWindow,e)})),this.injectedKeys.clear(),this.escapeKeys.forEach((e=>{Reflect.deleteProperty(I.rawWindow,e)})),this.escapeKeys.clear(),0==--ve.activeCount&&(document.addEventListener=I.rawDocumentAddEventListener,document.removeEventListener=I.rawDocumentRemoveEventListener))}recordUmdSnapshot(){this.microWindow.__MICRO_APP_UMD_MODE__=!0,this.recordUmdEffect(),function(e){const t=e.appName;e.umdDataListeners={global:new Set,normal:new Set};const n=he.eventList.get("global");if(n)for(const o of n.callbacks)t===o.__APP_NAME__&&e.umdDataListeners.global.add(o);const o=he.eventList.get(me(t,!0));o&&(e.umdDataListeners.normal=new Set(o.callbacks))}(this.microWindow.microApp),this.recordUmdinjectedValues=new Map,this.injectedKeys.forEach((e=>{this.recordUmdinjectedValues.set(e,Reflect.get(this.microWindow,e))}))}rebuildUmdSnapshot(){this.recordUmdinjectedValues.forEach(((e,t)=>{Reflect.set(this.proxyWindow,t,e)})),this.rebuildUmdEffect(),function(e){for(const t of e.umdDataListeners.global)e.addGlobalDataListener(t,t.__AUTO_TRIGGER__);for(const t of e.umdDataListeners.normal)e.addDataListener(t,t.__AUTO_TRIGGER__)}(this.microWindow.microApp)}getScopeProperties(e){var t;if(a(je.plugins)){if(s(je.plugins.global))for(const e of je.plugins.global)a(e)&&(s(e.scopeProperties)&&(this.scopeProperties=this.scopeProperties.concat(e.scopeProperties)),s(e.escapeProperties)&&(this.escapeProperties=this.escapeProperties.concat(e.escapeProperties)));if(s(null===(t=je.plugins.modules)||void 0===t?void 0:t[e]))for(const t of je.plugins.modules[e])a(t)&&(s(t.scopeProperties)&&(this.scopeProperties=this.scopeProperties.concat(t.scopeProperties)),s(t.escapeProperties)&&(this.escapeProperties=this.escapeProperties.concat(t.escapeProperties)))}}inject(e,t,n){e.__MICRO_APP_ENVIRONMENT__=!0,e.__MICRO_APP_NAME__=t,e.__MICRO_APP_PUBLIC_PATH__=_(n),e.microApp=new we(t),e.rawWindow=I.rawWindow,e.rawDocument=I.rawDocument,e.removeDomScope=v}}function Ne(e,t,n,o){var r;if(!e)return u(`element does not exist in lifecycle ${n}`,t);e=R(e),v();const s=Object.assign({name:t,container:e},o&&{error:o}),a=new CustomEvent(n,{detail:s});!function(e,t){Object.defineProperties(e,{currentTarget:{get:()=>t},target:{get:()=>t}})}(a,e),i(null===(r=je.lifeCycles)||void 0===r?void 0:r[n])&&je.lifeCycles[n](a),e.dispatchEvent(a)}ve.activeCount=0;const Ce=new Map;class Oe{constructor({name:e,url:t,ssrUrl:n,container:o,inline:r,scopecss:i,useSandbox:s,macro:a,baseroute:c}){this.status=S.NOT_LOADED,this.loadSourceLevel=0,this.umdHookMount=null,this.umdHookUnmount=null,this.libraryName=null,this.umdMode=!1,this.isPrefetch=!1,this.container=null,this.macro=!1,this.baseroute="",this.sandBox=null,this.container=null!=o?o:null,this.inline=null!=r&&r,this.baseroute=null!=c?c:"",this.ssrUrl=null!=n?n:"",this.name=e,this.url=t,this.useSandbox=s,this.scopecss=this.useSandbox&&i,this.macro=null!=a&&a,this.source={links:new Map,scripts:new Map},this.loadSourceCode(),this.useSandbox&&(this.sandBox=new ve(e,t,this.macro))}loadSourceCode(){var e;this.status=S.LOADING_SOURCE_CODE,L((e=this).ssrUrl||e.url,e.name,{cache:"no-cache"}).then((t=>{if(!t){const t="html is empty, please check in detail";return e.onerror(new Error(t)),u(t,e.name)}oe(t=t.replace(/<head[^>]*>[\s\S]*?<\/head>/i,(e=>e.replace(/<head/i,"<micro-app-head").replace(/<\/head>/i,"</micro-app-head>"))).replace(/<body[^>]*>[\s\S]*?<\/body>/i,(e=>e.replace(/<body/i,"<micro-app-body").replace(/<\/body>/i,"</micro-app-body>"))),e)})).catch((t=>{u(`Failed to fetch data from ${e.url}, micro-app stop rendering`,e.name,t),e.onLoadError(t)}))}onLoad(e){if(2==++this.loadSourceLevel){if(this.source.html=e,this.isPrefetch||S.UNMOUNT===this.status)return;this.status=S.LOAD_SOURCE_FINISHED,this.mount()}}onLoadError(e){this.loadSourceLevel=-1,S.UNMOUNT!==this.status&&(this.onerror(e),this.status=S.LOAD_SOURCE_ERROR)}mount(e,t,n){var o,r,s;if("boolean"==typeof t&&t!==this.inline&&(this.inline=t),this.container=null!==(o=this.container)&&void 0!==o?o:e,this.baseroute=null!=n?n:this.baseroute,2!==this.loadSourceLevel)return void(this.status=S.LOADING_SOURCE_CODE);let a;if(Ne(this.container,this.name,U.BEFOREMOUNT),this.status=S.MOUNTING,O(this.source.html,this.container,!this.umdMode),null===(r=this.sandBox)||void 0===r||r.start(this.baseroute),this.umdMode){null===(s=this.sandBox)||void 0===s||s.rebuildUmdSnapshot();try{a=this.umdHookMount()}catch(e){u("an error occurred in the mount function \n",this.name,e)}this.handleMounted(a)}else{let e=!1;!function(e,t,n){const o=Array.from(e.entries()),r=[],i=[];for(const[e,s]of o)s.isDynamic||(s.defer||s.async?(s.isExternal&&!s.code?r.push(L(e,t.name)):r.push(s.code),i.push([e,s]),s.module&&(n.moduleCount=n.moduleCount?++n.moduleCount:1)):(J(e,t,s,!1),n(!1)));r.length?Promise.all(r).then((e=>{e.forEach(((e,o)=>{const[r,s]=i[o];s.code=s.code||e,J(r,t,s,!1,n),!s.module&&n(!1)})),n(void 0===n.moduleCount)})).catch((e=>{u(e,t.name),n(!0)})):n(!0)}(this.source.scripts,this,(t=>{var n;if(!this.umdMode){const{mount:e,unmount:t}=this.getUmdLibraryHooks();if(i(e)&&i(t)){this.umdHookMount=e,this.umdHookUnmount=t,this.umdMode=!0,null===(n=this.sandBox)||void 0===n||n.recordUmdSnapshot();try{a=this.umdHookMount()}catch(e){u("an error occurred in the mount function \n",this.name,e)}}}e||!0!==t&&!this.umdMode||(e=!0,this.handleMounted(a))}))}}handleMounted(e){c(e)?e.then((()=>this.dispatchMountedEvent())).catch((e=>this.onerror(e))):this.dispatchMountedEvent()}dispatchMountedEvent(){S.UNMOUNT!==this.status&&(this.status=S.MOUNTED,Ne(this.container,this.name,U.MOUNTED))}unmount(e){let t;if(this.status===S.LOAD_SOURCE_ERROR&&(e=!0),this.status=S.UNMOUNT,this.umdHookUnmount)try{t=this.umdHookUnmount()}catch(e){u("an error occurred in the unmount function \n",this.name,e)}!function(e){const t=new CustomEvent(`unmount-${e}`);window.dispatchEvent(t)}(this.name),this.handleUnmounted(e,t)}handleUnmounted(e,t){c(t)?t.then((()=>this.actionsForUnmount(e))).catch((()=>this.actionsForUnmount(e))):this.actionsForUnmount(e)}actionsForUnmount(e){var t;Ne(this.container,this.name,U.UNMOUNT),null===(t=this.sandBox)||void 0===t||t.stop(),e?this.actionsForCompletelyDestory():this.umdMode&&this.container.childElementCount&&O(this.container,this.source.html,!1),this.container=null}actionsForCompletelyDestory(){!this.useSandbox&&this.umdMode&&delete window[this.libraryName],Ce.delete(this.name)}onerror(e){Ne(this.container,this.name,U.ERROR,e)}getAppStatus(){return this.status}getUmdLibraryHooks(){var e,t;if(S.UNMOUNT!==this.status){const n=null!==(t=null===(e=this.sandBox)||void 0===e?void 0:e.proxyWindow)&&void 0!==t?t:I.rawWindow;return this.libraryName=R(this.container).getAttribute("library")||`micro-app-${this.name}`,"object"==typeof n[this.libraryName]?n[this.libraryName]:{}}return{}}}const Pe=new WeakMap;function Me(e,t,n){if(t instanceof HTMLStyleElement){if(t.hasAttribute("exclude")){const e=document.createComment("style element with exclude attribute ignored by micro-app");return Pe.set(t,e),e}return n.scopecss&&!t.hasAttribute("ignore")?G(t,n):t}if(t instanceof HTMLLinkElement){if(t.hasAttribute("exclude")){const e=document.createComment("link element with exclude attribute ignored by micro-app");return Pe.set(t,e),e}if(t.hasAttribute("ignore"))return t;const{url:o,info:r,replaceComment:i}=z(t,e,n,!0);if(o&&r){const e=C("style");return e.__MICRO_APP_LINK_PATH__=o,function(e,t,n,o,r){if(n.source.links.has(e))return r.textContent=n.source.links.get(e).code,G(r,n),void d((()=>H(o)));if(q.has(e)){const i=q.get(e);return t.code=i,n.source.links.set(e,t),r.textContent=i,G(r,n),void d((()=>H(o)))}L(e,n.name).then((i=>{t.code=i,n.source.links.set(e,t),t.isGlobal&&q.set(e,i),r.textContent=i,G(r,n),H(o)})).catch((e=>{u(e,n.name),K(o)}))}(o,r,n,t,e),Pe.set(t,e),e}return i?(Pe.set(t,i),i):t}if(t instanceof HTMLScriptElement){const{replaceComment:o,url:r,info:i}=Q(t,e,n,!0)||{};if(r&&i){if(i.isExternal){const e=function(e,t,n,o){const r=()=>H(o);if(n.source.scripts.has(e)){const t=n.source.scripts.get(e);return!t.module&&d(r),J(e,n,t,!0,r)}if(V.has(e)){const o=V.get(e);return t.code=o,n.source.scripts.set(e,t),!t.module&&d(r),J(e,n,t,!0,r)}let i;return i=n.inline||t.module?C("script"):document.createComment(`dynamic script with src='${e}' extract by micro-app`),L(e,n.name).then((s=>{t.code=s,n.source.scripts.set(e,t),t.isGlobal&&V.set(e,s);try{s=te(e,n,s,t.module),n.inline||t.module?Z(e,s,t.module,i,r):ee(s,t)}catch(t){console.error(`[micro-app from runDynamicScript] app ${n.name}: `,t,e)}!t.module&&H(o)})).catch((e=>{u(e,n.name),K(o)})),i}(r,i,n,t);return Pe.set(t,e),e}{const e=J(r,n,i,!0);return Pe.set(t,e),e}}return o?(Pe.set(t,o),o):t}return t}function Re(e,t,n,o,r){if(n===document.head){const i=e.container.querySelector("micro-app-head");return r&&!i.contains(r)?I.rawAppendChild.call(i,o):t!==I.rawRemoveChild||i.contains(o)?t===I.rawAppend||t===I.rawPrepend?t.call(i,o):t.call(i,o,r):n.contains(o)?t.call(n,o):o}if(n===document.body){const i=e.container.querySelector("micro-app-body");return r&&!i.contains(r)?I.rawAppendChild.call(i,o):t!==I.rawRemoveChild||i.contains(o)?t===I.rawAppend||t===I.rawPrepend?t.call(i,o):t.call(i,o,r):n.contains(o)?t.call(n,o):o}return t===I.rawAppend||t===I.rawPrepend?t.call(n,o):t.call(n,o,r)}function De(e){var t;return null!==(t=Pe.get(e))&&void 0!==t?t:e}function Se(e,t,n,o){if(null==t?void 0:t.__MICRO_APP_NAME__){const r=Ce.get(t.__MICRO_APP_NAME__);return(null==r?void 0:r.container)?Re(r,o,e,Me(e,t,r),n&&De(n)):o===I.rawAppend||o===I.rawPrepend?o.call(e,t):o.call(e,t,n)}if(o===I.rawAppend||o===I.rawPrepend){const n=A();if(!(t instanceof Node)&&n){const r=Ce.get(n);if(null==r?void 0:r.container){if(e===document.head)return o.call(r.container.querySelector("micro-app-head"),t);if(e===document.body)return o.call(r.container.querySelector("micro-app-body"),t)}}return o.call(e,t)}return o.call(e,t,n)}function Ue(){!function(){const e=I.rawDocument;function t(t){var n,o,r;const i=A();return i&&t&&!M(t)&&e===this?null!==(r=null===(o=null===(n=Ce.get(i))||void 0===n?void 0:n.container)||void 0===o?void 0:o.querySelector(t))&&void 0!==r?r:null:I.rawQuerySelector.call(this,t)}function n(t){var n,o,r;const i=A();return i&&t&&!M(t)&&e===this?null!==(r=null===(o=null===(n=Ce.get(i))||void 0===n?void 0:n.container)||void 0===o?void 0:o.querySelectorAll(t))&&void 0!==r?r:[]:I.rawQuerySelectorAll.call(this,t)}Document.prototype.createElement=function(e,t){return Le(I.rawCreateElement.call(this,e,t))},Document.prototype.createElementNS=function(e,t,n){return Le(I.rawCreateElementNS.call(this,e,t,n))},Document.prototype.createDocumentFragment=function(){return Le(I.rawCreateDocumentFragment.call(this))},Document.prototype.querySelector=t,Document.prototype.querySelectorAll=n,Document.prototype.getElementById=function(e){if(!A()||P(e))return I.rawGetElementById.call(this,e);try{return t.call(this,`#${e}`)}catch(t){return I.rawGetElementById.call(this,e)}},Document.prototype.getElementsByClassName=function(e){if(!A()||P(e))return I.rawGetElementsByClassName.call(this,e);try{return n.call(this,`.${e}`)}catch(t){return I.rawGetElementsByClassName.call(this,e)}},Document.prototype.getElementsByTagName=function(e){var t;const o=A();if(!o||M(e)||P(e)||!(null===(t=Ce.get(o))||void 0===t?void 0:t.inline)&&/^script$/i.test(e))return I.rawGetElementsByTagName.call(this,e);try{return n.call(this,e)}catch(t){return I.rawGetElementsByTagName.call(this,e)}},Document.prototype.getElementsByName=function(e){if(!A()||P(e))return I.rawGetElementsByName.call(this,e);try{return n.call(this,`[name=${e}]`)}catch(t){return I.rawGetElementsByName.call(this,e)}}}(),Element.prototype.setAttribute=function(e,t){if(/^micro-app(-\S+)?/i.test(this.tagName)&&"data"===e)if(a(t)){const e={};Object.getOwnPropertyNames(t).forEach((n=>{r(n)&&0===n.indexOf("__")||(e[n]=t[n])})),this.data=e}else"[object Object]"!==t&&p("property data must be an object",this.getAttribute("name"));else if((("src"===e||"srcset"===e)&&/^(img|script)$/i.test(this.tagName)||"href"===e&&/^link$/i.test(this.tagName))&&this.__MICRO_APP_NAME__&&Ce.has(this.__MICRO_APP_NAME__)){const n=Ce.get(this.__MICRO_APP_NAME__);I.rawSetAttribute.call(this,e,w(t,n.url))}else I.rawSetAttribute.call(this,e,t)},Node.prototype.appendChild=function(e){return Se(this,e,null,I.rawAppendChild)},Node.prototype.insertBefore=function(e,t){return Se(this,e,t,I.rawInsertBefore)},Node.prototype.replaceChild=function(e,t){return Se(this,e,t,I.rawReplaceChild)},Element.prototype.append=function(...e){let t=0;const n=e.length;for(;t<n;)Se(this,e[t],null,I.rawAppend),t++},Element.prototype.prepend=function(...e){let t=e.length;for(;t>0;)Se(this,e[t-1],null,I.rawPrepend),t--},Node.prototype.removeChild=function(e){if(null==e?void 0:e.__MICRO_APP_NAME__){const t=Ce.get(e.__MICRO_APP_NAME__);return(null==t?void 0:t.container)?Re(t,I.rawRemoveChild,this,De(e)):I.rawRemoveChild.call(this,e)}return I.rawRemoveChild.call(this,e)}}function Le(e){const t=A();return t&&(e.__MICRO_APP_NAME__=t),e}function Ie(){E(null),Document.prototype.createElement=I.rawCreateElement,Document.prototype.createElementNS=I.rawCreateElementNS,Document.prototype.createDocumentFragment=I.rawCreateDocumentFragment,Document.prototype.querySelector=I.rawQuerySelector,Document.prototype.querySelectorAll=I.rawQuerySelectorAll,Document.prototype.getElementById=I.rawGetElementById,Document.prototype.getElementsByClassName=I.rawGetElementsByClassName,Document.prototype.getElementsByTagName=I.rawGetElementsByTagName,Document.prototype.getElementsByName=I.rawGetElementsByName,Element.prototype.setAttribute=I.rawSetAttribute,Node.prototype.appendChild=I.rawAppendChild,Node.prototype.insertBefore=I.rawInsertBefore,Node.prototype.replaceChild=I.rawReplaceChild,Node.prototype.removeChild=I.rawRemoveChild,Element.prototype.append=I.rawAppend,Element.prototype.prepend=I.rawPrepend}let Te=!1;function xe(){$e(),Ce.forEach((e=>{e.container&&R(e.container).disconnectedCallback()})),!window.__MICRO_APP_UMD_MODE__&&Ce.clear(),ke.size&&(ke.clear(),Ie())}function $e(){window.__MICRO_APP_ENVIRONMENT__&&window.removeEventListener("unmount",xe,!1)}const ke=new Map;function We(e){class n extends HTMLElement{constructor(){super(),this.isWating=!1,this.cacheData=null,this.hasConnected=!1,this.appName="",this.appUrl="",this.ssrUrl="",this.version=t,this.handleAttributeUpdate=()=>{var e;this.isWating=!1;const t=f(this.getAttribute("name")),n=m(this.getAttribute("url"),this.appName);if(this.legalAttribute("name",t)&&this.legalAttribute("url",n)){const o=Ce.get(t);if(t!==this.appName&&o&&S.UNMOUNT!==o.getAppStatus()&&!o.isPrefetch)return this.setAttribute("name",this.appName),u(`an app named ${t} already exists`,this.appName);t===this.appName&&n===this.appUrl||(this.handleUnmount(t===this.appName),this.getDisposeResult("ssr")?this.ssrUrl=w(I.rawWindow.location.pathname,n):this.ssrUrl&&(this.ssrUrl=""),this.appName=t,this.appUrl=n,(null!==(e=this.shadowRoot)&&void 0!==e?e:this).innerHTML="",t!==this.getAttribute("name")&&this.setAttribute("name",this.appName),o&&o.url===this.appUrl&&o.ssrUrl===this.ssrUrl?this.handleAppMount(o):this.handleCreateApp())}else t!==this.appName&&this.setAttribute("name",this.appName)},this.querySelector("micro-app-head")||this.performWhenFirstCreated()}static get observedAttributes(){return["name","url"]}connectedCallback(){this.hasConnected=!0,ke.has(this)||this.performWhenFirstCreated(),d((()=>Ne(this,this.appName,U.CREATED))),this.initialMount()}disconnectedCallback(){this.hasConnected=!1,ke.delete(this),this.handleUnmount(this.getDisposeResult("destroy")||this.getDisposeResult("destory")),0===ke.size&&Ie()}attributeChangedCallback(e,t,n){if(this.legalAttribute(e,n)&&this[e===D.NAME?"appName":"appUrl"]!==n)if(e!==D.URL||this.appUrl)if(e!==D.NAME||this.appName)this.isWating||(this.isWating=!0,d(this.handleAttributeUpdate));else{const e=f(n);if(!e)return u(`Invalid attribute name ${n}`,this.appName);this.cacheData&&(je.setData(e,this.cacheData),this.cacheData=null),this.appName=e,e!==n&&this.setAttribute("name",this.appName),this.handleInitialNameAndUrl()}else{if(!(n=m(n,this.appName)))return u(`Invalid attribute url ${n}`,this.appName);this.appUrl=n,this.handleInitialNameAndUrl()}}handleInitialNameAndUrl(){this.hasConnected&&this.initialMount()}performWhenFirstCreated(){1===ke.set(this,!0).size&&(Ue(),function(){if(!Te){Te=!0;const e=C("style");e.setAttribute("type","text/css"),e.textContent=`\n${je.tagName}, micro-app-body { display: block; } \nmicro-app-head { display: none; }`,I.rawDocument.head.appendChild(e)}}(),$e(),window.__MICRO_APP_ENVIRONMENT__&&window.addEventListener("unmount",xe,!1))}initialMount(){if(!this.appName||!this.appUrl)return;this.getDisposeResult("shadowDOM")&&!this.shadowRoot&&i(this.attachShadow)&&this.attachShadow({mode:"open"}),this.getDisposeResult("ssr")?this.ssrUrl=w(I.rawWindow.location.pathname,this.appUrl):this.ssrUrl&&(this.ssrUrl="");const e=Ce.get(this.appName);if(e){const t=e.ssrUrl||e.url;t!==(this.ssrUrl||this.appUrl)||!e.isPrefetch&&e.getAppStatus()!==S.UNMOUNT?e.isPrefetch||e.getAppStatus()===S.UNMOUNT?(p(`the ${e.isPrefetch?"prefetch":"unmounted"} app with url: ${t} is replaced by a new app`,this.appName),this.handleCreateApp()):u(`an app named ${this.appName} already exists`,this.appName):this.handleAppMount(e)}else this.handleCreateApp()}legalAttribute(e,t){return!(!r(t)||!t)||(u(`unexpected attribute ${e}, please check again`,this.appName),!1)}handleAppMount(e){e.isPrefetch=!1,d((()=>{var t;return e.mount(null!==(t=this.shadowRoot)&&void 0!==t?t:this,this.getDisposeResult("inline"),this.getBaseRouteCompatible())}))}handleCreateApp(){var e;Ce.has(this.appName)&&Ce.get(this.appName).actionsForCompletelyDestory();const t=new Oe({name:this.appName,url:this.appUrl,ssrUrl:this.ssrUrl,container:null!==(e=this.shadowRoot)&&void 0!==e?e:this,inline:this.getDisposeResult("inline"),scopecss:!(this.getDisposeResult("disableScopecss")||this.getDisposeResult("shadowDOM")),useSandbox:!this.getDisposeResult("disableSandbox"),macro:this.getDisposeResult("macro"),baseroute:this.getBaseRouteCompatible()});Ce.set(this.appName,t)}handleUnmount(e){const t=Ce.get(this.appName);t&&S.UNMOUNT!==t.getAppStatus()&&t.unmount(e)}getDisposeResult(e){return(this.hasAttribute(e)||je[e])&&"false"!==this.getAttribute(e)}getBaseRouteCompatible(){var e,t;return null!==(t=null!==(e=this.getAttribute("baseroute"))&&void 0!==e?e:this.getAttribute("baseurl"))&&void 0!==t?t:""}set data(e){this.appName?je.setData(this.appName,e):this.cacheData=e}get data(){return this.appName?je.getData(this.appName,!0):this.cacheData?this.cacheData:null}}window.customElements.define(e,n)}function Be(e){if(!n)return u("preFetch is only supported in browser environment");b((()=>{i(e)&&(e=e()),function(e){const t=[];return s(e)&&e.forEach((e=>{a(e)&&(e.name=f(e.name),e.url=m(e.url,e.name),e.name&&e.url&&!Ce.has(e.name)&&t.push(e))})),t}(e).forEach((e=>{var t,n,o;const r=new Oe({name:e.name,url:e.url,scopecss:!(null!==(t=e.disableScopecss)&&void 0!==t?t:je.disableScopecss),useSandbox:!(null!==(n=e.disableSandbox)&&void 0!==n?n:je.disableSandbox),macro:null!==(o=e.macro)&&void 0!==o?o:je.macro});r.isPrefetch=!0,Ce.set(e.name,r)}))}))}var je=new class extends _e{constructor(){super(...arguments),this.tagName="micro-app",this.preFetch=Be}start(e){if(!n||!window.customElements)return u("micro-app is not supported in this environment");if(null==e?void 0:e.tagName){if(!/^micro-app(-\S+)?/.test(e.tagName))return u(`${e.tagName} is invalid tagName`);this.tagName=e.tagName}if(window.customElements.get(this.tagName))return p(`element ${this.tagName} is already defined`);if(T(),e&&a(e)&&(this.shadowDOM=e.shadowDOM,this.destroy=e.destroy,this.destory=e.destory,this.inline=e.inline,this.disableScopecss=e.disableScopecss,this.disableSandbox=e.disableSandbox,this.macro=e.macro,i(e.fetch)&&(this.fetch=e.fetch),a(e.lifeCycles)&&(this.lifeCycles=e.lifeCycles),e.preFetchApps&&Be(e.preFetchApps),e.globalAssets&&(a(t=e.globalAssets)&&b((()=>{if(s(t.js)){const e=t.js.filter((e=>r(e)&&e.includes(".js")&&!V.has(e))),n=[];e.forEach((e=>{n.push(L(e))})),y(n,(t=>{const n=e[t.index];V.has(n)||V.set(n,t.data)}),(e=>{u(e)}))}if(s(t.css)){const e=t.css.filter((e=>r(e)&&e.includes(".css")&&!q.has(e))),n=[];e.forEach((e=>{n.push(L(e))})),y(n,(t=>{const n=e[t.index];q.has(n)||q.set(n,t.data)}),(e=>{u(e)}))}}))),a(e.plugins))){const t=e.plugins.modules;if(a(t))for(const e in t){const n=f(e);n&&e!==n&&(t[n]=t[e],delete t[e])}this.plugins=e.plugins}var t;We(this.tagName)}};e.EventCenterForMicroApp=we,e.default=je,e.getActiveApps=function(){const e=[];return Ce.forEach(((t,n)=>{S.UNMOUNT===t.getAppStatus()||t.isPrefetch||e.push(n)})),e},e.getAllApps=function(){return Array.from(Ce.keys())},e.preFetch=Be,e.pureCreateElement=C,e.removeDomScope=v,e.version=t,Object.defineProperty(e,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=index.umd.js.map