react-micro-frontends-bridge
Version:
A simple react-micro-frontends implement
89 lines (71 loc) • 2.22 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ENTRY = 'index.js';
var MicroResource = function MicroResource() {
var _this = this;
_classCallCheck(this, MicroResource);
this.modules = {};
this.components = {};
this.inited = true;
this.define = function (module) {
var name = module.name,
context = module.context,
entry = module.entry;
var keys = context.keys();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
var parts = (name + key.slice(1)).split('/');
var dir = _this.modules;
for (var i = 0; i < parts.length - 1; i++) {
var part = parts[i];
if (!dir.hasOwnProperty(part)) {
dir[part] = {};
}
dir = dir[part];
}
dir[parts[parts.length - 1]] = context.bind(context, key);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator["return"] != null) {
_iterator["return"]();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
if (entry != null) {
_this.modules[name][ENTRY] = _this.modules[name][entry];
}
};
this.require = function (name) {
return _this.modules[name][ENTRY]();
};
this.containsComponent = function (name) {
return name in _this.components;
};
this.registerComponent = function (name, render) {
if (!_this.components[name]) {
_this.components[name] = render;
}
};
this.render = function (name, element) {
if (_this.components[name]) {
_this.components[name](element);
}
};
};
exports["default"] = MicroResource;
;