UNPKG

@moyi.rk/micro-core

Version:

实验版本微前端框架

2 lines (1 loc) 3.99 kB
let t=[];const n=()=>t;let o={};const e=()=>o;let a={};class r{constructor(){this.proxy=null,this.active()}active(){this.proxy=new Proxy(window,{get:(t,n)=>"function"==typeof t[n]?t[n].bind(t):a[n]||t[n],set:(t,n,o)=>(a[n]=o,!0)})}inactive(){a={}}}const i=(t,n)=>{const o=new r;t.proxy||(t.proxy=o),window.__MOYI_MICRO_WEB__=!0,console.log("sandbox");const e=((t,n,o)=>(window.proxy=o,new Function(`\n return (function(window) {\n ${t}\n return window['${n}']\n })(window.proxy)\n `)()))(n,t.name,t.proxy.proxy);(t=>t?.mounted&&t?.bootstrap&&t?.unmounted)(e)&&(t.mounted=e.mounted,t.bootstrap=e.bootstrap,t.unmounted=e.unmounted)},s=async t=>fetch(t).then((t=>t.text())),c={},w=async(t,n)=>{if(c[n])return c[n];const o=await s(t),e=document.createElement("div");e.innerHTML=o;let a=[];const[r,i,w]=await u(e,t),d=await Promise.all(i.map((async t=>await s(t))));return a=w.concat(d),c[n]=[r,a],[r,a]},u=async(t,n)=>{const o=[],e=[],a=t.outerHTML;return function t(a){const r=a.children,i=a.parent;if("script"===a.nodeName.toLowerCase()){const t=a.getAttribute("src");t?t.startsWith("http")?o.push(t):o.push(`http:${n}/${t}`):e.push(a.outerHTML),i&&i.replaceChild(document.createComment("file is replaced"),a)}if("link"===a.nodeName.toLowerCase()){const t=a.getAttribute("href");t.endsWith(".js")&&(t.startsWith("http")?o.push(t):o.push(`http:${n}/${t}`))}for(let n=0;n<r.length;n++)t(r[n])}(t),[a,o,e]},d=(t,n)=>function(){const o=new Event(n);t.apply(this,arguments),window.dispatchEvent(o)},p=(t,o)=>{const e=n().filter((n=>n[t]===o));return e?.length?e[0]:{}},l=t=>p("activeRule",t),m=async t=>{const n=e()[t];n?.length&&await Promise.all(n.map((async t=>await t())))},_=async t=>{await m("bootstrap"),t?.bootstrap?.({props:t.props,entry:t.entry});const n=await(async t=>{let n=t.container,o=t.entry;const[e,a]=await w(o,t.name),r=document.querySelector(n);if(!r)throw new Error(`Could not dom from ${n}`);return r.innerHTML=e,a.forEach((n=>{i(t,n)})),t})(t);return await(n?.bootstrap?.()),n},h=async()=>{const t=l(window.__MOYI_ORIGIN_APP__),n=l(window.__MOYI_CHILD_APP__);if(!n)return;t?.unmounted&&await(async t=>{t?.unmounted&&t.unmounted({props:t.props,entry:t.entry}),await m("mounted")})(t),t?.proxy&&t.proxy.inactive();const o=await _(n);await(async t=>{t?.mounted?.({props:t.props,entry:t.entry}),await m("mounted")})(o)},y=async(...t)=>{const n=(()=>{if(window.__MOYI_ORIGIN_APP__=window.__MOYI_CHILD_APP__,window.__MOYI_CHILD_APP__===window.location.pathname)return!1;const t=window.location.pathname.match(/\/(\/\w+)/);return!!t&&(window.__MOYI_CHILD_APP__=t[0],!0)})();n&&await h()},f=new class{on(t,n){window.addEventListener(t,(t=>{n(t.detail)}))}emit(t,n){const o=new CustomEvent(t,{detail:n});window.dispatchEvent(o)}};window.custom=f,window.history.pushState=d(window.history.pushState,"micro_push"),window.history.replaceState=d(window.history.replaceState,"micro_replace"),window.addEventListener("micro_push",y),window.addEventListener("micro_replace",y),window.onpopstate=function(){y()};const v=(n,e)=>{var a;(n=>{t=n})(n),(a=e).bootstrap&&(o.bootstrap=Array.isArray(a.bootstrap)?a.bootstrap:[a.bootstrap]),a.mounted&&(o.mounted=Array.isArray(a.mounted)?a.mounted:[a.mounted]),a.unmounted&&(o.unmounted=Array.isArray(a.unmounted)?a.unmounted:[a.unmounted])},I=()=>{console.log("start");const t=n();if(0===t?.length)return void console.error("no apps found");const o=(()=>{const t=window.location.pathname;return p("activeRule",t)})();if(o){const{pathname:t,hash:n}=window.location,e=`${t}${n}`;window.history.pushState(null,"",e),window.__MOYI_CHILD_APP__=o.activeRule}(async()=>{const t=n().filter((t=>!window.location.pathname.startsWith(t.activeRule)));await Promise.all(t.map((async t=>await w(t.entry,t.name))))})()},P=(t={})=>(()=>{let n=t;const o=[];return{getStore:()=>n,update:t=>{if(t!==n){const e=n;n=t,o.forEach((async t=>await t(e,n)))}},subscribe:t=>{o.push(t)}}})();export{P as createStore,v as registerMicroApps,I as start};