UNPKG

@kookapp/mf-runtime-sdk

Version:

模块联邦运行时SDK

793 lines (786 loc) 28.4 kB
'use strict'; var runtime = require('@module-federation/runtime'); function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _regeneratorRuntime() { _regeneratorRuntime = function () { return r; }; var t, r = {}, e = Object.prototype, n = e.hasOwnProperty, o = "function" == typeof Symbol ? Symbol : {}, i = o.iterator || "@@iterator", a = o.asyncIterator || "@@asyncIterator", u = o.toStringTag || "@@toStringTag"; function c(t, r, e, n) { Object.defineProperty(t, r, { value: e, enumerable: !n, configurable: !n, writable: !n }); } try { c({}, ""); } catch (t) { c = function (t, r, e) { return t[r] = e; }; } function h(r, e, n, o) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype); return c(a, "_invoke", function (r, e, n) { var o = 1; return function (i, a) { if (3 === o) throw Error("Generator is already running"); if (4 === o) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var u = n.delegate; if (u) { var c = d(u, n); if (c) { if (c === f) continue; return c; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (1 === o) throw o = 4, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = 3; var h = s(r, e, n); if ("normal" === h.type) { if (o = n.done ? 4 : 2, h.arg === f) continue; return { value: h.arg, done: n.done }; } "throw" === h.type && (o = 4, n.method = "throw", n.arg = h.arg); } }; }(r, n, new Context(o || [])), !0), a; } function s(t, r, e) { try { return { type: "normal", arg: t.call(r, e) }; } catch (t) { return { type: "throw", arg: t }; } } r.wrap = h; var f = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var l = {}; c(l, i, function () { return this; }); var p = Object.getPrototypeOf, y = p && p(p(x([]))); y && y !== e && n.call(y, i) && (l = y); var v = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(l); function g(t) { ["next", "throw", "return"].forEach(function (r) { c(t, r, function (t) { return this._invoke(r, t); }); }); } function AsyncIterator(t, r) { function e(o, i, a, u) { var c = s(t[o], t, i); if ("throw" !== c.type) { var h = c.arg, f = h.value; return f && "object" == typeof f && n.call(f, "__await") ? r.resolve(f.__await).then(function (t) { e("next", t, a, u); }, function (t) { e("throw", t, a, u); }) : r.resolve(f).then(function (t) { h.value = t, a(h); }, function (t) { return e("throw", t, a, u); }); } u(c.arg); } var o; c(this, "_invoke", function (t, n) { function i() { return new r(function (r, o) { e(t, n, r, o); }); } return o = o ? o.then(i, i) : i(); }, !0); } function d(r, e) { var n = e.method, o = r.i[n]; if (o === t) return e.delegate = null, "throw" === n && r.i.return && (e.method = "return", e.arg = t, d(r, e), "throw" === e.method) || "return" !== n && (e.method = "throw", e.arg = new TypeError("The iterator does not provide a '" + n + "' method")), f; var i = s(o, r.i, e.arg); if ("throw" === i.type) return e.method = "throw", e.arg = i.arg, e.delegate = null, f; var a = i.arg; return a ? a.done ? (e[r.r] = a.value, e.next = r.n, "return" !== e.method && (e.method = "next", e.arg = t), e.delegate = null, f) : a : (e.method = "throw", e.arg = new TypeError("iterator result is not an object"), e.delegate = null, f); } function w(t) { this.tryEntries.push(t); } function m(r) { var e = r[4] || {}; e.type = "normal", e.arg = t, r[4] = e; } function Context(t) { this.tryEntries = [[-1]], t.forEach(w, this), this.reset(!0); } function x(r) { if (null != r) { var e = r[i]; if (e) return e.call(r); if ("function" == typeof r.next) return r; if (!isNaN(r.length)) { var o = -1, a = function e() { for (; ++o < r.length;) if (n.call(r, o)) return e.value = r[o], e.done = !1, e; return e.value = t, e.done = !0, e; }; return a.next = a; } } throw new TypeError(typeof r + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, c(v, "constructor", GeneratorFunctionPrototype), c(GeneratorFunctionPrototype, "constructor", GeneratorFunction), c(GeneratorFunctionPrototype, u, GeneratorFunction.displayName = "GeneratorFunction"), r.isGeneratorFunction = function (t) { var r = "function" == typeof t && t.constructor; return !!r && (r === GeneratorFunction || "GeneratorFunction" === (r.displayName || r.name)); }, r.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, c(t, u, "GeneratorFunction")), t.prototype = Object.create(v), t; }, r.awrap = function (t) { return { __await: t }; }, g(AsyncIterator.prototype), c(AsyncIterator.prototype, a, function () { return this; }), r.AsyncIterator = AsyncIterator, r.async = function (t, e, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(h(t, e, n, o), i); return r.isGeneratorFunction(e) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, g(v), c(v, u, "Generator"), c(v, i, function () { return this; }), c(v, "toString", function () { return "[object Generator]"; }), r.keys = function (t) { var r = Object(t), e = []; for (var n in r) e.unshift(n); return function t() { for (; e.length;) if ((n = e.pop()) in r) return t.value = n, t.done = !1, t; return t.done = !0, t; }; }, r.values = x, Context.prototype = { constructor: Context, reset: function (r) { if (this.prev = this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(m), !r) for (var e in this) "t" === e.charAt(0) && n.call(this, e) && !isNaN(+e.slice(1)) && (this[e] = t); }, stop: function () { this.done = !0; var t = this.tryEntries[0][4]; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function (r) { if (this.done) throw r; var e = this; function n(t) { a.type = "throw", a.arg = r, e.next = t; } for (var o = e.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i[4], u = this.prev, c = i[1], h = i[2]; if (-1 === i[0]) return n("end"), !1; if (!c && !h) throw Error("try statement without catch or finally"); if (null != i[0] && i[0] <= u) { if (u < c) return this.method = "next", this.arg = t, n(c), !0; if (u < h) return n(h), !1; } } }, abrupt: function (t, r) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var n = this.tryEntries[e]; if (n[0] > -1 && n[0] <= this.prev && this.prev < n[2]) { var o = n; break; } } o && ("break" === t || "continue" === t) && o[0] <= r && r <= o[2] && (o = null); var i = o ? o[4] : {}; return i.type = t, i.arg = r, o ? (this.method = "next", this.next = o[2], f) : this.complete(i); }, complete: function (t, r) { 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 && r && (this.next = r), f; }, finish: function (t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[2] === t) return this.complete(e[4], e[3]), m(e), f; } }, catch: function (t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[0] === t) { var n = e[4]; if ("throw" === n.type) { var o = n.arg; m(e); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function (r, e, n) { return this.delegate = { i: x(r), r: e, n: n }, "next" === this.method && (this.arg = t), f; } }, r; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r ); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return (String )(t); } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; /** * 模块联邦缓存管理类 (TypeScript) * 用于管理远程模块和本地模块的缓存 */ var MFCache = /*#__PURE__*/function () { function MFCache() { _classCallCheck(this, MFCache); // 模块缓存,key: `${providerKey}/${moduleName}`, value: CachedModule this._cache = new Map(); } /** * 生成模块缓存键 * @param providerKey - 提供者标识符 * @param moduleName - 模块名称 (可以是空字符串) * @returns 缓存键 */ return _createClass(MFCache, [{ key: "_getCacheKey", value: function _getCacheKey(providerKey, moduleName) { return "".concat(providerKey, "/").concat(moduleName); } /** * 获取模块缓存项 * @param providerKey - 提供者标识符 * @param moduleName - 模块名称 (可以是空字符串) * @returns 缓存的模块数据或 undefined */ }, { key: "getCacheItem", value: function getCacheItem(providerKey, moduleName) { return this._cache.get(this._getCacheKey(providerKey, moduleName)); } /** * 设置模块缓存 * @param providerKey - 提供者标识符 * @param moduleName - 模块名称 (可以是空字符串) * @param value - 缓存值 { error, export, providerName } */ }, { key: "setModuleCache", value: function setModuleCache(providerKey, moduleName, value) { this._cache.set(this._getCacheKey(providerKey, moduleName), value); } /** * 获取模块导出 * @param providerKey - 提供者标识符 * @param moduleName - 模块名称 (可以是空字符串) * @returns 模块导出对象或 null (如果未缓存或有错误) */ }, { key: "getModuleCache", value: function getModuleCache(providerKey, moduleName) { if (!providerKey) return null; // providerKey cannot be empty var item = this.getCacheItem(providerKey, moduleName); if (item && !item.error) return item["export"]; return null; } /** * 清除模块缓存 * @param providerKey - 提供者标识符 * @param moduleName - 模块名称 (可以是空字符串) */ }, { key: "clearModuleCache", value: function clearModuleCache(providerKey, moduleName) { this._cache["delete"](this._getCacheKey(providerKey, moduleName)); } /** * 清除所有缓存 */ }, { key: "clearAllCache", value: function clearAllCache() { this._cache.clear(); } /** * 获取缓存状态 * @returns 缓存状态对象 */ }, { key: "getCacheStatus", value: function getCacheStatus() { var status = { totalModules: this._cache.size, successModules: 0, errorModules: 0, modules: [] }; this._cache.forEach(function (value, key) { if (value.error) { status.errorModules++; } else { status.successModules++; } status.modules.push({ key: key, hasError: !!value.error, providerName: value.providerName }); }); return status; } }]); }(); /** * Module Federation SDK 核心管理器类 (TypeScript) * 用于管理 Module Federation 的初始化、模块的加载、注册和访问 */ var MFManager = /*#__PURE__*/function () { function MFManager() { _classCallCheck(this, MFManager); this._cache = new MFCache(); this._initialized = false; this._remotesConfig = {}; this._localModules = {}; this._isDev = true; // 是否是开发环境,默认 true } /** * 初始化 SDK * @param config - 初始化配置 */ return _createClass(MFManager, [{ key: "init", value: function init(config) { var _a; if (this._initialized) { console.warn('MFManager already initialized.'); return; } this._localModules = config.modules || {}; this._remotesConfig = config.remotes || {}; this._isDev = (_a = config.isDev) !== null && _a !== void 0 ? _a : true; // 初始化 Module Federation runtime runtime.init({ name: 'hostApp', // 主机应用名称,目前没有什么意义 shared: config.share, remotes: [] // 当调用 get 方法时,会根据 remotes 配置自动注册远程模块 }); this._initialized = true; } /** * 动态添加或更新远程模块配置 * @param remoteConfig - 单个远程模块配置对象 */ }, { key: "add", value: function add(remoteConfig) { if (!remoteConfig || !remoteConfig.key) { console.error('无效的远程配置对象', remoteConfig); return; } this._remotesConfig[remoteConfig.key] = remoteConfig; } /** * 获取模块(本地或远程) * @param modulePath - 模块路径,格式为 'providerKey/moduleName' 或 'providerKey' (本地模块) * @returns Promise resolves with the module export object or null */ }, { key: "get", value: function get(modulePath) { return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime().mark(function _callee() { var parts, providerKey, moduleName, item, providerConfig, providerFn, providerInfo, error, providerName, entry, moduleExport; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (this._initialized) { _context.next = 3; break; } console.error('MFManager not initialized. Call MFInit first.'); return _context.abrupt("return", null); case 3: parts = modulePath.split('/'); providerKey = parts[0]; // If providerKey/moduleName is given, moduleName is the rest. moduleName = parts.slice(1).join('/'); if (providerKey) { _context.next = 9; break; } console.error("\u65E0\u6548\u7684\u6A21\u5757\u8DEF\u5F84\u683C\u5F0F: ".concat(modulePath)); return _context.abrupt("return", null); case 9: if (!(providerKey === 'host')) { _context.next = 11; break; } return _context.abrupt("return", moduleName ? this._localModules[moduleName] : this._localModules); case 11: // 检查缓存中是否存在特定模块 item = this._cache.getCacheItem(providerKey, moduleName); // Use public getter if (!item) { _context.next = 15; break; } if (item.error) { _context.next = 15; break; } return _context.abrupt("return", item["export"]); case 15: if (!(providerKey !== 'manual')) { _context.next = 40; break; } providerConfig = this._remotesConfig[providerKey]; if (providerConfig) { _context.next = 20; break; } console.error("\u672A\u5728\u914D\u7F6E\u4E2D\u627E\u5230\u63D0\u4F9B\u8005: ".concat(providerKey)); return _context.abrupt("return", null); case 20: _context.prev = 20; // 确定使用哪个提供者函数 (dev 或 prod) providerFn = this._isDev ? providerConfig.provider.dev : providerConfig.provider.prod; providerInfo = providerFn(item === null || item === void 0 ? void 0 : item.error); // 调用提供者函数 if (!(!providerInfo || !providerInfo.name || !providerInfo.entry)) { _context.next = 27; break; } error = new Error("Provider ".concat(providerKey, ".provider.").concat(this._isDev ? 'dev' : 'prod', " \u914D\u7F6E\u65E0\u6548\u6216\u7F3A\u5C11 name/entry")); console.error(error.message); return _context.abrupt("return", null); case 27: providerName = providerInfo.name, entry = providerInfo.entry; // 'name' 是实际的 MF 名称,包含版本 // 注册远程模块 _context.next = 30; return this._registerRemote(providerKey, providerName, entry, moduleName); case 30: _context.next = 32; return this._loadRemote(providerKey, providerName, moduleName); case 32: moduleExport = _context.sent; return _context.abrupt("return", moduleExport); case 36: _context.prev = 36; _context.t0 = _context["catch"](20); console.error("\u52A0\u8F7D\u8FDC\u7A0B\u6A21\u5757 ".concat(modulePath, " \u5931\u8D25:"), _context.t0); // 错误已经在 _registerRemote 或 _loadRemote 中缓存 return _context.abrupt("return", null); case 40: return _context.abrupt("return", null); case 41: case "end": return _context.stop(); } }, _callee, this, [[20, 36]]); })); } // 同步获取模块,只能获取本地或者缓存中的模块 }, { key: "getSync", value: function getSync(modulePath) { var parts = modulePath.split('/'); var providerKey = parts[0]; // If providerKey/moduleName is given, moduleName is the rest. var moduleName = parts.slice(1).join('/'); if (!providerKey) { console.error("\u65E0\u6548\u7684\u6A21\u5757\u8DEF\u5F84\u683C\u5F0F: ".concat(modulePath)); return null; } // 如果 providerKey 为 'host',则返回本地模块 if (providerKey === 'host') { return moduleName ? this._localModules[moduleName] : this._localModules; } // 检查缓存中是否存在特定模块 var item = this._cache.getCacheItem(providerKey, moduleName); // Use public getter if (item) { if (!item.error) return item["export"]; // 如果缓存中有错误,不要尝试重新加载,直接返回 null // TODO: 重试机制 return null; } } /** * 动态设置或更新模块的缓存值 * @param modulePath - 模块路径,格式为 'providerKey/moduleName' 或 'providerKey' * @param value - 要保存的值 */ }, { key: "set", value: function set(modulePath, value) { if (!this._initialized) { console.error('MFManager not initialized. Call MFInit first.'); return; } var parts = modulePath.split('/'); var providerKey = parts[0]; var moduleName = parts.slice(1).join('/'); // Handle 'providerKey' case (moduleName = '') if (!providerKey) { console.error("\u65E0\u6548\u7684\u6A21\u5757\u8DEF\u5F84\u683C\u5F0F: ".concat(modulePath)); return; } // 使用 'manual' providerKey 如果指定的 providerKey 不是远程模块 var finalProviderKey = providerKey === 'host' ? providerKey : 'manual'; // 在缓存中存储值,如果 providerKey 是 'manual',则标记为手动设置 this._cache.setModuleCache(finalProviderKey, moduleName, { error: null, "export": value, providerName: finalProviderKey === 'manual' ? 'manual' : finalProviderKey // Use 'manual' if manually set, otherwise keep original/host }); } /** * 注册远程模块到 Module Federation runtime * @param providerKey - 提供者标识符 (用于缓存) * @param providerName - 模块联邦名称 (MF Name) * @param entry - 远程入口URL * @param moduleName - 模块名称 (可以是空字符串) * @returns Promise void */ }, { key: "_registerRemote", value: function _registerRemote(providerKey, providerName, entry, moduleName) { return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() { return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.prev = 0; _context2.next = 3; return runtime.registerRemotes([{ name: providerName, entry: entry, alias: providerName // 保持 alias 与 name 相同,目前简单处理 }], { force: true // 强制重新加载 remoteEntry 文件,无论之前是否成功或失败, (其实只有在失败后重新加载时force 才有意义) }); case 3: _context2.next = 10; break; case 5: _context2.prev = 5; _context2.t0 = _context2["catch"](0); console.error("\u6CE8\u518C\u8FDC\u7A0B\u6A21\u5757\u5931\u8D25: ".concat(providerKey, "/").concat(moduleName), _context2.t0); // Cache registration error this._cache.setModuleCache(providerKey, moduleName, { error: _context2.t0, "export": null, providerName: providerName }); throw _context2.t0; case 10: case "end": return _context2.stop(); } }, _callee2, this, [[0, 5]]); })); } /** * 加载远程模块组件 * @param providerKey - 提供者标识符 * @param providerName - 模块联邦名称 (MF Name) * @param moduleName - 模块名称 (可以是空字符串) * @returns Promise resolves with the module export or rejects on error */ }, { key: "_loadRemote", value: function _loadRemote(providerKey, providerName, moduleName) { return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() { var _path, moduleExport, error; return _regeneratorRuntime().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.prev = 0; // 没有 moduleName 时,应该为 rspack插件 exposes . 的情况 _path = moduleName ? "".concat(providerName, "/").concat(moduleName) : providerName; _context3.next = 4; return runtime.loadRemote(_path, { from: 'runtime' }); case 4: moduleExport = _context3.sent; if (moduleExport) { _context3.next = 9; break; } error = new Error("".concat(providerName, "/").concat(moduleName, " has no export!")); this._cache.setModuleCache(providerKey, moduleName, { error: error, "export": null, providerName: providerName }); throw error; case 9: // 缓存成功加载的模块 (或远程入口如果 moduleName 为空且导出可用) this._cache.setModuleCache(providerKey, moduleName, { error: null, "export": moduleExport, // 这可能是模块导出或远程入口内容 providerName: providerName }); return _context3.abrupt("return", moduleExport); case 13: _context3.prev = 13; _context3.t0 = _context3["catch"](0); console.error("\u52A0\u8F7D\u8FDC\u7A0B\u7EC4\u4EF6\u5931\u8D25: ".concat(providerName, "/").concat(moduleName), _context3.t0); // 缓存加载错误 this._cache.setModuleCache(providerKey, moduleName, { error: _context3.t0, "export": null, providerName: providerName }); throw _context3.t0; case 18: case "end": return _context3.stop(); } }, _callee3, this, [[0, 13]]); })); } // Optional: Expose cache management methods if needed }, { key: "getCacheStatus", value: function getCacheStatus() { return this._cache.getCacheStatus(); } }, { key: "clearCache", value: function clearCache(providerKey, moduleName) { this._cache.clearModuleCache(providerKey, moduleName); } }, { key: "clearAllCache", value: function clearAllCache() { this._cache.clearAllCache(); } }]); }(); var mfManagerInstance = new MFManager(); // import { useRemote, useRemoteComponent } from './useRemote' /** * 访问已加载的模块导出对象 * @param modulePath - 模块路径,格式为 'providerKey/moduleName' 或 'moduleName' (本地模块) * @returns 模块导出对象的 Promise */ var MF = { init: function init(config) { return mfManagerInstance.init(config); }, add: function add(remoteConfig) { return mfManagerInstance.add(remoteConfig); }, get: function get(modulePath) { return mfManagerInstance.get(modulePath); }, getSync: function getSync(modulePath) { return mfManagerInstance.getSync(modulePath); }, set: function set(modulePath, value) { return mfManagerInstance.set(modulePath, value); } // 添加 set 方法 // useRemote, // useRemoteComponent, }; module.exports = MF;