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