UNPKG

@sentre/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.78 kB
import e from"@babel/runtime/helpers/objectWithoutProperties";import r from"@babel/runtime/helpers/asyncToGenerator";import n from"@babel/runtime/regenerator";import{jsx as t}from"react/jsx-runtime";var o=function(e){var r=e.url,n=e.module;return[r,e.scope,n].join()},i=function(e){var r=o(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 i=new Promise((function(e,r){t.onload=function(){return e(t)},t.onerror=function(e){r(e)}}));return document.head.appendChild(t),i},a=function(e){var r=document.getElementById(e);r&&document.head.removeChild(r)},c=function(e,t,o){return r(n.mark((function r(){var i,a,c,u;return n.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,r.next=3,__webpack_init_sharing__("default");case 3:if((i=window[t]).isInitialized){r.next=8;break}return i.isInitialized=!0,r.next=8,i.init(__webpack_share_scopes__.default);case 8:return r.next=10,window[t].get(o);case 10:return a=r.sent,c=a(),r.abrupt("return",c);case 15:throw r.prev=15,r.t0=r.catch(0),(u=new Error("There was a problem loading the remote module. Please check the parameters (url: ".concat(e," scope: ").concat(t," module: ").concat(o,"). Details:\n\n").concat(r.t0.message))).name="RemoteModuleLoadingError",u;case 20:case"end":return r.stop()}}),r,null,[[0,15]])})))},u=["unLoadScriptOnUnmount","exportName"],s=function(e){window.remoteModuleDictionary=window.remoteModuleDictionary||{};var t=o(e),a=window.remoteModuleDictionary[t];return a||(window.remoteModuleDictionary[t]=new Promise(function(){var t=r(n.mark((function r(t,o){var a;return n.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.prev=0,r.next=3,i(e);case 3:return r.next=5,c(e.url,e.scope,e.module)();case 5:a=r.sent,t(a),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,r){return t.apply(this,arguments)}}()),window.remoteModuleDictionary[t])},d=function(e){window.remoteObjectDictionary=window.remoteObjectDictionary||{};var r=o(e),n=window.remoteObjectDictionary[r];if(n)return n();var t,i,a,c,u=(t=s(e),a="pending",c=t.then((function(e){a="success",i=e}),(function(e){a="error",i=e})),function(){switch(a){case"pending":throw c;case"error":throw i;default:return i}});return window.remoteObjectDictionary[r]=u,u()},m=function(e){return d(e)},l=function(r){r.unLoadScriptOnUnmount;var n=r.exportName,o=void 0===n?"default":n,i=e(r,u),a=d(i)[o];return t(a,{})};export{l as RemoteComponent,i as attachScript,a as detachScript,s as getModule,o as getRemoteModuleId,m as useRemoteModule}; //# sourceMappingURL=index.js.map