UNPKG

@kookapp/mf-runtime-sdk

Version:

模块联邦运行时SDK

2 lines (1 loc) 12.2 kB
import{init as e,registerRemotes as t,loadRemote as r}from"@module-federation/runtime";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t,r){return t&&function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,a(n.key),n)}}(e.prototype,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function i(){i=function(){return t};var e,t={},r=Object.prototype,n=r.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",c=o.asyncIterator||"@@asyncIterator",u=o.toStringTag||"@@toStringTag";function s(e,t,r,n){Object.defineProperty(e,t,{value:r,enumerable:!n,configurable:!n,writable:!n})}try{s({},"")}catch(e){s=function(e,t,r){return e[t]=r}}function h(t,r,n,o){var i=r&&r.prototype instanceof p?r:p,a=Object.create(i.prototype);return s(a,"_invoke",function(t,r,n){var o=1;return function(i,a){if(3===o)throw Error("Generator is already running");if(4===o){if("throw"===i)throw a;return{value:e,done:!0}}for(n.method=i,n.arg=a;;){var c=n.delegate;if(c){var u=x(c,n);if(u){if(u===f)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(1===o)throw o=4,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=3;var s=l(t,r,n);if("normal"===s.type){if(o=n.done?4:2,s.arg===f)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=4,n.method="throw",n.arg=s.arg)}}}(t,n,new E(o||[])),!0),a}function l(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}t.wrap=h;var f={};function p(){}function v(){}function d(){}var y={};s(y,a,(function(){return this}));var g=Object.getPrototypeOf,m=g&&g(g(M([])));m&&m!==r&&n.call(m,a)&&(y=m);var w=d.prototype=p.prototype=Object.create(y);function _(e){["next","throw","return"].forEach((function(t){s(e,t,(function(e){return this._invoke(t,e)}))}))}function b(e,t){function r(o,i,a,c){var u=l(e[o],e,i);if("throw"!==u.type){var s=u.arg,h=s.value;return h&&"object"==typeof h&&n.call(h,"__await")?t.resolve(h.__await).then((function(e){r("next",e,a,c)}),(function(e){r("throw",e,a,c)})):t.resolve(h).then((function(e){s.value=e,a(s)}),(function(e){return r("throw",e,a,c)}))}c(u.arg)}var o;s(this,"_invoke",(function(e,n){function i(){return new t((function(t,o){r(e,n,t,o)}))}return o=o?o.then(i,i):i()}),!0)}function x(t,r){var n=r.method,o=t.i[n];if(o===e)return r.delegate=null,"throw"===n&&t.i.return&&(r.method="return",r.arg=e,x(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),f;var i=l(o,t.i,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,f;var a=i.arg;return a?a.done?(r[t.r]=a.value,r.next=t.n,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,f):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,f)}function k(e){this.tryEntries.push(e)}function C(t){var r=t[4]||{};r.type="normal",r.arg=e,t[4]=r}function E(e){this.tryEntries=[[-1]],e.forEach(k,this),this.reset(!0)}function M(t){if(null!=t){var r=t[a];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,i=function r(){for(;++o<t.length;)if(n.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return i.next=i}}throw new TypeError(typeof t+" is not iterable")}return v.prototype=d,s(w,"constructor",d),s(d,"constructor",v),s(d,u,v.displayName="GeneratorFunction"),t.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===v||"GeneratorFunction"===(t.displayName||t.name))},t.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,d):(e.__proto__=d,s(e,u,"GeneratorFunction")),e.prototype=Object.create(w),e},t.awrap=function(e){return{__await:e}},_(b.prototype),s(b.prototype,c,(function(){return this})),t.AsyncIterator=b,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new b(h(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},_(w),s(w,u,"Generator"),s(w,a,(function(){return this})),s(w,"toString",(function(){return"[object Generator]"})),t.keys=function(e){var t=Object(e),r=[];for(var n in t)r.unshift(n);return function e(){for(;r.length;)if((n=r.pop())in t)return e.value=n,e.done=!1,e;return e.done=!0,e}},t.values=M,E.prototype={constructor:E,reset:function(t){if(this.prev=this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(C),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var e=this.tryEntries[0][4];if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(e){a.type="throw",a.arg=t,r.next=e}for(var o=r.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i[4],c=this.prev,u=i[1],s=i[2];if(-1===i[0])return n("end"),!1;if(!u&&!s)throw Error("try statement without catch or finally");if(null!=i[0]&&i[0]<=c){if(c<u)return this.method="next",this.arg=e,n(u),!0;if(c<s)return n(s),!1}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n[0]>-1&&n[0]<=this.prev&&this.prev<n[2]){var o=n;break}}o&&("break"===e||"continue"===e)&&o[0]<=t&&t<=o[2]&&(o=null);var i=o?o[4]:{};return i.type=e,i.arg=t,o?(this.method="next",this.next=o[2],f):this.complete(i)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),f},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r[2]===e)return this.complete(r[4],r[3]),C(r),f}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r[0]===e){var n=r[4];if("throw"===n.type){var o=n.arg;C(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={i:M(t),r:r,n:n},"next"===this.method&&(this.arg=e),f}},t}function a(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e,"string");return"symbol"==typeof t?t:t+""}function c(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function c(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,c)}u((n=n.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;var u=function(){return o((function e(){n(this,e),this._cache=new Map}),[{key:"_getCacheKey",value:function(e,t){return"".concat(e,"/").concat(t)}},{key:"getCacheItem",value:function(e,t){return this._cache.get(this._getCacheKey(e,t))}},{key:"setModuleCache",value:function(e,t,r){this._cache.set(this._getCacheKey(e,t),r)}},{key:"getModuleCache",value:function(e,t){if(!e)return null;var r=this.getCacheItem(e,t);return r&&!r.error?r.export:null}},{key:"clearModuleCache",value:function(e,t){this._cache.delete(this._getCacheKey(e,t))}},{key:"clearAllCache",value:function(){this._cache.clear()}},{key:"getCacheStatus",value:function(){var e={totalModules:this._cache.size,successModules:0,errorModules:0,modules:[]};return this._cache.forEach((function(t,r){t.error?e.errorModules++:e.successModules++,e.modules.push({key:r,hasError:!!t.error,providerName:t.providerName})})),e}}])}(),s=new(function(){return o((function e(){n(this,e),this._cache=new u,this._initialized=!1,this._remotesConfig={},this._localModules={},this._isDev=!0}),[{key:"init",value:function(t){var r;this._initialized?console.warn("MFManager already initialized."):(this._localModules=t.modules||{},this._remotesConfig=t.remotes||{},this._isDev=null===(r=t.isDev)||void 0===r||r,e({name:"hostApp",shared:t.share,remotes:[]}),this._initialized=!0)}},{key:"add",value:function(e){e&&e.key?this._remotesConfig[e.key]=e:console.error("无效的远程配置对象",e)}},{key:"get",value:function(e){return c(this,void 0,void 0,i().mark((function t(){var r,n,o,a,c,u,s,h,l,f,p;return i().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(this._initialized){t.next=3;break}return console.error("MFManager not initialized. Call MFInit first."),t.abrupt("return",null);case 3:if(r=e.split("/"),n=r[0],o=r.slice(1).join("/"),n){t.next=9;break}return console.error("无效的模块路径格式: ".concat(e)),t.abrupt("return",null);case 9:if("host"!==n){t.next=11;break}return t.abrupt("return",o?this._localModules[o]:this._localModules);case 11:if(!(a=this._cache.getCacheItem(n,o))){t.next=15;break}if(a.error){t.next=15;break}return t.abrupt("return",a.export);case 15:if("manual"===n){t.next=40;break}if(c=this._remotesConfig[n]){t.next=20;break}return console.error("未在配置中找到提供者: ".concat(n)),t.abrupt("return",null);case 20:if(t.prev=20,u=this._isDev?c.provider.dev:c.provider.prod,(s=u(null==a?void 0:a.error))&&s.name&&s.entry){t.next=27;break}return h=new Error("Provider ".concat(n,".provider.").concat(this._isDev?"dev":"prod"," 配置无效或缺少 name/entry")),console.error(h.message),t.abrupt("return",null);case 27:return l=s.name,f=s.entry,t.next=30,this._registerRemote(n,l,f,o);case 30:return t.next=32,this._loadRemote(n,l,o);case 32:return p=t.sent,t.abrupt("return",p);case 36:return t.prev=36,t.t0=t.catch(20),console.error("加载远程模块 ".concat(e," 失败:"),t.t0),t.abrupt("return",null);case 40:return t.abrupt("return",null);case 41:case"end":return t.stop()}}),t,this,[[20,36]])})))}},{key:"getSync",value:function(e){var t=e.split("/"),r=t[0],n=t.slice(1).join("/");if(!r)return console.error("无效的模块路径格式: ".concat(e)),null;if("host"===r)return n?this._localModules[n]:this._localModules;var o=this._cache.getCacheItem(r,n);return o?o.error?null:o.export:void 0}},{key:"set",value:function(e,t){if(this._initialized){var r=e.split("/"),n=r[0],o=r.slice(1).join("/");if(n){var i="host"!==n?n:"manual";this._cache.setModuleCache(i,o,{error:null,export:t,providerName:"manual"===i?"manual":i})}else console.error("无效的模块路径格式: ".concat(e))}else console.error("MFManager not initialized. Call MFInit first.")}},{key:"_registerRemote",value:function(e,r,n,o){return c(this,void 0,void 0,i().mark((function a(){return i().wrap((function(i){for(;;)switch(i.prev=i.next){case 0:return i.prev=0,i.next=3,t([{name:r,entry:n,alias:r}],{force:!0});case 3:i.next=10;break;case 5:throw i.prev=5,i.t0=i.catch(0),console.error("注册远程模块失败: ".concat(e,"/").concat(o),i.t0),this._cache.setModuleCache(e,o,{error:i.t0,export:null,providerName:r}),i.t0;case 10:case"end":return i.stop()}}),a,this,[[0,5]])})))}},{key:"_loadRemote",value:function(e,t,n){return c(this,void 0,void 0,i().mark((function o(){var a,c,u;return i().wrap((function(o){for(;;)switch(o.prev=o.next){case 0:return o.prev=0,a=n?"".concat(t,"/").concat(n):t,o.next=4,r(a,{from:"runtime"});case 4:if(c=o.sent){o.next=9;break}throw u=new Error("".concat(t,"/").concat(n," has no export!")),this._cache.setModuleCache(e,n,{error:u,export:null,providerName:t}),u;case 9:return this._cache.setModuleCache(e,n,{error:null,export:c,providerName:t}),o.abrupt("return",c);case 13:throw o.prev=13,o.t0=o.catch(0),console.error("加载远程组件失败: ".concat(t,"/").concat(n),o.t0),this._cache.setModuleCache(e,n,{error:o.t0,export:null,providerName:t}),o.t0;case 18:case"end":return o.stop()}}),o,this,[[0,13]])})))}},{key:"getCacheStatus",value:function(){return this._cache.getCacheStatus()}},{key:"clearCache",value:function(e,t){this._cache.clearModuleCache(e,t)}},{key:"clearAllCache",value:function(){this._cache.clearAllCache()}}])}()),h={init:function(e){return s.init(e)},add:function(e){return s.add(e)},get:function(e){return s.get(e)},getSync:function(e){return s.getSync(e)},set:function(e,t){return s.set(e,t)}};export{h as default};