@moyi.rk/micro-core
Version:
实验版本微前端框架
2 lines (1 loc) • 4.2 kB
JavaScript
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).micro={})}(this,(function(t){"use strict";let n=[];const o=()=>n;let e={};const a=()=>e;let r={};class i{constructor(){this.proxy=null,this.active()}active(){this.proxy=new Proxy(window,{get:(t,n)=>"function"==typeof t[n]?t[n].bind(t):r[n]||t[n],set:(t,n,o)=>(r[n]=o,!0)})}inactive(){r={}}}const s=(t,n)=>{const o=new i;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)},c=async t=>fetch(t).then((t=>t.text())),u={},w=async(t,n)=>{if(u[n])return u[n];const o=await c(t),e=document.createElement("div");e.innerHTML=o;let a=[];const[r,i,s]=await d(e,t),w=await Promise.all(i.map((async t=>await c(t))));return a=s.concat(w),u[n]=[r,a],[r,a]},d=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]},p=(t,n)=>function(){const o=new Event(n);t.apply(this,arguments),window.dispatchEvent(o)},l=(t,n)=>{const e=o().filter((o=>o[t]===n));return e?.length?e[0]:{}},m=t=>l("activeRule",t),h=async t=>{const n=a()[t];n?.length&&await Promise.all(n.map((async t=>await t())))},y=async t=>{await h("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=>{s(t,n)})),t})(t);return await(n?.bootstrap?.()),n},_=async()=>{const t=m(window.__MOYI_ORIGIN_APP__),n=m(window.__MOYI_CHILD_APP__);if(!n)return;t?.unmounted&&await(async t=>{t?.unmounted&&t.unmounted({props:t.props,entry:t.entry}),await h("mounted")})(t),t?.proxy&&t.proxy.inactive();const o=await y(n);await(async t=>{t?.mounted?.({props:t.props,entry:t.entry}),await h("mounted")})(o)},f=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 _()},b=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=b,window.history.pushState=p(window.history.pushState,"micro_push"),window.history.replaceState=p(window.history.replaceState,"micro_replace"),window.addEventListener("micro_push",f),window.addEventListener("micro_replace",f),window.onpopstate=function(){f()};t.createStore=(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)}}})(),t.registerMicroApps=(t,o)=>{var a;(t=>{n=t})(t),(a=o).bootstrap&&(e.bootstrap=Array.isArray(a.bootstrap)?a.bootstrap:[a.bootstrap]),a.mounted&&(e.mounted=Array.isArray(a.mounted)?a.mounted:[a.mounted]),a.unmounted&&(e.unmounted=Array.isArray(a.unmounted)?a.unmounted:[a.unmounted])},t.start=()=>{console.log("start");const t=o();if(0===t?.length)return void console.error("no apps found");const n=(()=>{const t=window.location.pathname;return l("activeRule",t)})();if(n){const{pathname:t,hash:o}=window.location,e=`${t}${o}`;window.history.pushState(null,"",e),window.__MOYI_CHILD_APP__=n.activeRule}(async()=>{const t=o().filter((t=>!window.location.pathname.startsWith(t.activeRule)));await Promise.all(t.map((async t=>await w(t.entry,t.name))))})()}}));