react-micro-frontends-bridge
Version:
A simple react-micro-frontends implement
10 lines • 14.1 kB
JavaScript
/*!
*
* react-micro-frontends-bridge v0.0.3-beta
*
* Copyright 2015-present, Alipay, Inc.
* All rights reserved.
*
*/
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["react","react-dom"],e):"object"==typeof exports?exports["react-micro-frontends-bridge"]=e(require("react"),require("react-dom")):t["react-micro-frontends-bridge"]=e(t.React,t.ReactDOM)}(window,function(r,n){return function(r){var n={};function o(t){if(n[t])return n[t].exports;var e=n[t]={i:t,l:!1,exports:{}};return r[t].call(e.exports,e,e.exports,o),e.l=!0,e.exports}return o.m=r,o.c=n,o.d=function(t,e,r){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(o.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)o.d(r,n,function(t){return e[t]}.bind(null,n));return r},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},o.p="",o(o.s=3)}([function(t,e){t.exports=r},function(t,e,r){t.exports=r(5)},function(t,e){t.exports=n},function(t,e,r){t.exports=r(4)},function(t,e,r){t.exports=r(7)},function(t,e,r){var n=function(){return this||"object"==typeof self&&self}()||Function("return this")(),o=n.regeneratorRuntime&&0<=Object.getOwnPropertyNames(n).indexOf("regeneratorRuntime"),i=o&&n.regeneratorRuntime;if(n.regeneratorRuntime=void 0,t.exports=r(6),o)n.regeneratorRuntime=i;else try{delete n.regeneratorRuntime}catch(t){n.regeneratorRuntime=void 0}},function(k,t){!function(t){"use strict";var a,e=Object.prototype,f=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},o=r.iterator||"@@iterator",n=r.asyncIterator||"@@asyncIterator",i=r.toStringTag||"@@toStringTag",c="object"==typeof k,u=t.regeneratorRuntime;if(u)c&&(k.exports=u);else{(u=t.regeneratorRuntime=c?k.exports:{}).wrap=b;var l="suspendedStart",p="suspendedYield",h="executing",y="completed",d={},s={};s[o]=function(){return this};var m=Object.getPrototypeOf,v=m&&m(m(R([])));v&&v!==e&&f.call(v,o)&&(s=v);var g=j.prototype=x.prototype=Object.create(s);O.prototype=g.constructor=j,j.constructor=O,j[i]=O.displayName="GeneratorFunction",u.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===O||"GeneratorFunction"===(e.displayName||e.name))},u.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,j):(t.__proto__=j,i in t||(t[i]="GeneratorFunction")),t.prototype=Object.create(g),t},u.awrap=function(t){return{__await:t}},_(E.prototype),E.prototype[n]=function(){return this},u.AsyncIterator=E,u.async=function(t,e,r,n){var o=new E(b(t,e,r,n));return u.isGeneratorFunction(e)?o:o.next().then(function(t){return t.done?t.value:o.next()})},_(g),g[i]="Generator",g[o]=function(){return this},g.toString=function(){return"[object Generator]"},u.keys=function(r){var n=[];for(var t in r)n.push(t);return n.reverse(),function t(){for(;n.length;){var e=n.pop();if(e in r)return t.value=e,t.done=!1,t}return t.done=!0,t}},u.values=R,M.prototype={constructor:M,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=a,this.done=!1,this.delegate=null,this.method="next",this.arg=a,this.tryEntries.forEach(S),!t)for(var e in this)"t"===e.charAt(0)&&f.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=a)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var n=this;function t(t,e){return i.type="throw",i.arg=r,n.next=t,e&&(n.method="next",n.arg=a),!!e}for(var e=this.tryEntries.length-1;0<=e;--e){var o=this.tryEntries[e],i=o.completion;if("root"===o.tryLoc)return t("end");if(o.tryLoc<=this.prev){var c=f.call(o,"catchLoc"),u=f.call(o,"finallyLoc");if(c&&u){if(this.prev<o.catchLoc)return t(o.catchLoc,!0);if(this.prev<o.finallyLoc)return t(o.finallyLoc)}else if(c){if(this.prev<o.catchLoc)return t(o.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return t(o.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;0<=r;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&f.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var i=o?o.completion:{};return i.type=t,i.arg=e,o?(this.method="next",this.next=o.finallyLoc,d):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),d},finish:function(t){for(var e=this.tryEntries.length-1;0<=e;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),S(r),d}},catch:function(t){for(var e=this.tryEntries.length-1;0<=e;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;S(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:R(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=a),d}}}function b(t,e,r,n){var i,c,u,a,o=e&&e.prototype instanceof x?e:x,f=Object.create(o.prototype),s=new M(n||[]);return f._invoke=(i=t,c=r,u=s,a=l,function(t,e){if(a===h)throw new Error("Generator is already running");if(a===y){if("throw"===t)throw e;return C()}for(u.method=t,u.arg=e;;){var r=u.delegate;if(r){var n=P(r,u);if(n){if(n===d)continue;return n}}if("next"===u.method)u.sent=u._sent=u.arg;else if("throw"===u.method){if(a===l)throw a=y,u.arg;u.dispatchException(u.arg)}else"return"===u.method&&u.abrupt("return",u.arg);a=h;var o=w(i,c,u);if("normal"===o.type){if(a=u.done?y:p,o.arg===d)continue;return{value:o.arg,done:u.done}}"throw"===o.type&&(a=y,u.method="throw",u.arg=o.arg)}}),f}function w(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}function x(){}function O(){}function j(){}function _(t){["next","throw","return"].forEach(function(e){t[e]=function(t){return this._invoke(e,t)}})}function E(a){var e;this._invoke=function(r,n){function t(){return new Promise(function(t,e){!function e(t,r,n,o){var i=w(a[t],a,r);if("throw"!==i.type){var c=i.arg,u=c.value;return u&&"object"==typeof u&&f.call(u,"__await")?Promise.resolve(u.__await).then(function(t){e("next",t,n,o)},function(t){e("throw",t,n,o)}):Promise.resolve(u).then(function(t){c.value=t,n(c)},function(t){return e("throw",t,n,o)})}o(i.arg)}(r,n,t,e)})}return e=e?e.then(t,t):t()}}function P(t,e){var r=t.iterator[e.method];if(r===a){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=a,P(t,e),"throw"===e.method))return d;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return d}var n=w(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,d;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=a),e.delegate=null,d):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,d)}function L(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function S(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function M(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(L,this),this.reset(!0)}function R(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,n=function t(){for(;++r<e.length;)if(f.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=a,t.done=!0,t};return n.next=n}}return{next:C}}function C(){return{value:a,done:!0}}}(function(){return this||"object"==typeof self&&self}()||Function("return this")())},function(t,e,r){"use strict";r.r(e);var m="index.js",n=r(1),i=r.n(n),c=r(0),u=Object(c.createContext)({MicroComponents:{}});function o(t){return(o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function a(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function f(t,e){return!e||"object"!==o(e)&&"function"!=typeof e?function(t){if(void 0!==t)return t;throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}(t):e}function s(t){return(s=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function l(t,e){return(l=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}var p=function(i,c,u,a){return new(u||(u=Promise))(function(t,e){function r(t){try{o(a.next(t))}catch(t){e(t)}}function n(t){try{o(a.throw(t))}catch(t){e(t)}}function o(e){e.done?t(e.value):new u(function(t){t(e.value)}).then(r,n)}o((a=a.apply(i,c||[])).next())})},h=function(t){function r(t){var e;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,r),(e=f(this,s(r).call(this,t))).state={components:{}},e}var e,n,o;return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&l(t,e)}(r,c["PureComponent"]),e=r,(n=[{key:"componentDidMount",value:function(){return p(this,void 0,void 0,i.a.mark(function t(){var e,r,n,o;return i.a.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return e=this.props,r=e.getMicroResources,(n=e.commonModules)&&n.forEach(function(t){return window.MicroResource.define(t)}),t.next=4,r();case 4:o=t.sent,this.setState({components:o});case 6:case"end":return t.stop()}},t,this)}))}},{key:"render",value:function(){return c.createElement(u.Provider,{value:{MicroComponents:this.state.components}},this.props.children)}}])&&a(e.prototype,n),o&&a(e,o),r}();function y(){return(y=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function d(t){return(d="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function v(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function g(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function b(t,e,r){return e&&g(t.prototype,e),r&&g(t,r),t}function w(t,e){return!e||"object"!==d(e)&&"function"!=typeof e?function(t){if(void 0!==t)return t;throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}(t):e}function x(t){return(x=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function O(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&j(t,e)}function j(t,e){return(j=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}var _=function(t){function e(){var t;return v(this,e),(t=w(this,x(e).apply(this,arguments))).loadModule=function(t,e){if(t){var r=document.createElement("script");r.src=t,r.onload=e,document.body.appendChild(r)}},t}return O(e,c["PureComponent"]),b(e,[{key:"componentDidMount",value:function(){var t=this,e=this.props,r=e.name,n=e.components,o=window.MicroResource;o.containsComponent(r)?o.render(r,this.node):this.loadModule(n[r],function(){o.containsComponent(r)&&o.render(r,t.node)})}},{key:"render",value:function(){var e=this,t=this.props,r=t.className,n=t.style,o=t.loading;return c.createElement("div",{ref:function(t){return e.node=t},className:r,style:n},o)}}]),e}(),E=function(t){function e(){return v(this,e),w(this,x(e).apply(this,arguments))}return O(e,c["PureComponent"]),b(e,[{key:"render",value:function(){var e=this;return c.createElement(u.Consumer,null,function(t){return c.createElement(_,y({key:e.props.name+"@"+t.MicroComponents[e.props.name]},e.props,{components:t.MicroComponents}))})}}]),e}(),P=r(2);function L(t,e){window.MicroResource.registerComponent(t,function(t){return P.render(e,t)})}r.d(e,"MFBridgeProvider",function(){return h}),r.d(e,"MFBridgeConsumer",function(){return E}),r.d(e,"register",function(){return L}),window.MicroResource&&window.MicroResource.inited||(window.MicroResource=new function t(){var d=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.modules={},this.components={},this.inited=!0,this.define=function(t){var e=t.name,r=t.context,n=t.entry,o=r.keys(),i=!0,c=!1,u=void 0;try{for(var a,f=o[Symbol.iterator]();!(i=(a=f.next()).done);i=!0){for(var s=a.value,l=(e+s.slice(1)).split("/"),p=d.modules,h=0;h<l.length-1;h++){var y=l[h];p.hasOwnProperty(y)||(p[y]={}),p=p[y]}p[l[l.length-1]]=r.bind(r,s)}}catch(t){c=!0,u=t}finally{try{i||null==f.return||f.return()}finally{if(c)throw u}}null!=n&&(d.modules[e][m]=d.modules[e][n])},this.require=function(t){return d.modules[t][m]()},this.containsComponent=function(t){return t in d.components},this.registerComponent=function(t,e){d.components[t]||(d.components[t]=e)},this.render=function(t,e){d.components[t]&&d.components[t](e)}})}])});
//# sourceMappingURL=react-micro-frontends-bridge.min.js.map