@micro-zoe/micro-app 0.3.3 → 0.4.0

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