react-dynamic-remote-component
Version:
Allows you to dynamically load a component from a remote using webpack 5's module federation.
3 lines (2 loc) • 2.74 kB
JavaScript
import e from"@babel/runtime/helpers/objectSpread2";import r from"@babel/runtime/helpers/objectWithoutProperties";import n from"@babel/runtime/helpers/asyncToGenerator";import t from"@babel/runtime/regenerator";import{jsx as o}from"react/jsx-runtime";var i=function(e){var r=e.url,n=e.module;return[r,e.scope,n].join()},c=function(e){var r=i(e),n=document.getElementById(r);if(n)return window[e.scope]?Promise.resolve(!0):new Promise((function(e){n.onload=function(r){e(!0)}}));var t=document.createElement("script");t.src=e.url,t.type="text/javascript",t.async=!0,t.id=r;var o=new Promise((function(e,r){t.onload=function(){return e(t)},t.onerror=function(e){r(e)}}));return document.head.appendChild(t),o},a=function(e,r,o){return n(t.mark((function n(){var i,c,a,u;return t.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return n.prev=0,n.next=3,__webpack_init_sharing__("default");case 3:if((i=window[r]).isInitialized){n.next=8;break}return i.isInitialized=!0,n.next=8,i.init(__webpack_share_scopes__.default);case 8:return n.next=10,window[r].get(o);case 10:return c=n.sent,a=c(),n.abrupt("return",a);case 15:throw n.prev=15,n.t0=n.catch(0),(u=new Error("There was a problem loading the remote module. Please check the parameters (url: ".concat(e," scope: ").concat(r," module: ").concat(o,")"))).name="RemoteModuleLoadingError",u;case 20:case"end":return n.stop()}}),n,null,[[0,15]])})))},u=["unLoadScriptOnUnmount","exportName","url","scope","module"],s=function(e){window.remoteModuleDictionary=window.remoteModuleDictionary||{};var r=i(e),o=window.remoteModuleDictionary[r];return o||(window.remoteModuleDictionary[r]=new Promise(function(){var r=n(t.mark((function r(n,o){var i;return t.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,r.next=3,c(e);case 3:return r.next=5,a(e.url,e.scope,e.module)();case 5:i=r.sent,n(i),r.next=12;break;case 9:r.prev=9,r.t0=r.catch(0),o(r.t0);case 12:case"end":return r.stop()}}),r,null,[[0,9]])})));return function(e,n){return r.apply(this,arguments)}}()),window.remoteModuleDictionary[r])},d=function(e){window.remoteObjectDictionary=window.remoteObjectDictionary||{};var r=i(e),n=window.remoteObjectDictionary[r];if(n)return n();var t,o,c,a,u=(t=s(e),c="pending",a=t.then((function(e){c="success",o=e}),(function(e){c="error",o=e})),function(){switch(c){case"pending":throw a;case"error":throw o;default:return o}});return window.remoteObjectDictionary[r]=u,u()},m=function(e){return d(e)};function l(n){n.unLoadScriptOnUnmount;var t=n.exportName,i=void 0===t?"default":t,c=n.url,a=n.scope,s=n.module,m=r(n,u),l=d({url:c,scope:a,module:s})[i];return o(l,e({},m))}export{l as RemoteComponent,s as getModule,m as useRemoteModule};
//# sourceMappingURL=index.js.map