Files
emaui/node_modules/@module-federation/runtime-core/dist/global.cjs
2026-05-29 15:23:46 +03:00

160 lines
6.6 KiB
JavaScript

const require_logger = require('./utils/logger.cjs');
const require_tool = require('./utils/tool.cjs');
let _module_federation_sdk = require("@module-federation/sdk");
//#region src/global.ts
const CurrentGlobal = typeof globalThis === "object" ? globalThis : window;
const nativeGlobal = (() => {
try {
return document.defaultView;
} catch {
return CurrentGlobal;
}
})();
const Global = nativeGlobal;
function definePropertyGlobalVal(target, key, val) {
Object.defineProperty(target, key, {
value: val,
configurable: false,
writable: true
});
}
function includeOwnProperty(target, key) {
return Object.hasOwnProperty.call(target, key);
}
if (!includeOwnProperty(CurrentGlobal, "__GLOBAL_LOADING_REMOTE_ENTRY__")) definePropertyGlobalVal(CurrentGlobal, "__GLOBAL_LOADING_REMOTE_ENTRY__", {});
const globalLoading = CurrentGlobal.__GLOBAL_LOADING_REMOTE_ENTRY__;
function setGlobalDefaultVal(target) {
if (includeOwnProperty(target, "__VMOK__") && !includeOwnProperty(target, "__FEDERATION__")) definePropertyGlobalVal(target, "__FEDERATION__", target.__VMOK__);
if (!includeOwnProperty(target, "__FEDERATION__")) {
definePropertyGlobalVal(target, "__FEDERATION__", {
__GLOBAL_PLUGIN__: [],
__INSTANCES__: [],
moduleInfo: {},
__SHARE__: {},
__MANIFEST_LOADING__: {},
__PRELOADED_MAP__: /* @__PURE__ */ new Map()
});
definePropertyGlobalVal(target, "__VMOK__", target.__FEDERATION__);
}
target.__FEDERATION__.__GLOBAL_PLUGIN__ ??= [];
target.__FEDERATION__.__INSTANCES__ ??= [];
target.__FEDERATION__.moduleInfo ??= {};
target.__FEDERATION__.__SHARE__ ??= {};
target.__FEDERATION__.__MANIFEST_LOADING__ ??= {};
target.__FEDERATION__.__PRELOADED_MAP__ ??= /* @__PURE__ */ new Map();
}
setGlobalDefaultVal(CurrentGlobal);
setGlobalDefaultVal(nativeGlobal);
function resetFederationGlobalInfo() {
CurrentGlobal.__FEDERATION__.__GLOBAL_PLUGIN__ = [];
CurrentGlobal.__FEDERATION__.__INSTANCES__ = [];
CurrentGlobal.__FEDERATION__.moduleInfo = {};
CurrentGlobal.__FEDERATION__.__SHARE__ = {};
CurrentGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {};
Object.keys(globalLoading).forEach((key) => {
delete globalLoading[key];
});
}
function setGlobalFederationInstance(FederationInstance) {
CurrentGlobal.__FEDERATION__.__INSTANCES__.push(FederationInstance);
}
function getGlobalFederationConstructor() {
return CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
}
function setGlobalFederationConstructor(FederationConstructor, isDebug = (0, _module_federation_sdk.isDebugMode)()) {
if (isDebug) {
CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "2.5.0";
}
}
function getInfoWithoutType(target, key) {
if (typeof key === "string") if (target[key]) return {
value: target[key],
key
};
else {
const targetKeys = Object.keys(target);
for (const targetKey of targetKeys) {
const [targetTypeOrName, _] = targetKey.split(":");
const nKey = `${targetTypeOrName}:${key}`;
const typeWithKeyRes = target[nKey];
if (typeWithKeyRes) return {
value: typeWithKeyRes,
key: nKey
};
}
return {
value: void 0,
key
};
}
else require_logger.error(`getInfoWithoutType: "key" must be a string, got ${typeof key} (${JSON.stringify(key)}).`);
}
const getGlobalSnapshot = () => nativeGlobal.__FEDERATION__.moduleInfo;
const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot) => {
const getModuleInfo = getInfoWithoutType(snapshot, require_tool.getFMId(moduleInfo)).value;
if (getModuleInfo && !getModuleInfo.version && "version" in moduleInfo && moduleInfo["version"]) getModuleInfo.version = moduleInfo["version"];
if (getModuleInfo) return getModuleInfo;
if ("version" in moduleInfo && moduleInfo["version"]) {
const { version, ...resModuleInfo } = moduleInfo;
const moduleKeyWithoutVersion = require_tool.getFMId(resModuleInfo);
const getModuleInfoWithoutVersion = getInfoWithoutType(nativeGlobal.__FEDERATION__.moduleInfo, moduleKeyWithoutVersion).value;
if (getModuleInfoWithoutVersion?.version === version) return getModuleInfoWithoutVersion;
}
};
const getGlobalSnapshotInfoByModuleInfo = (moduleInfo) => getTargetSnapshotInfoByModuleInfo(moduleInfo, nativeGlobal.__FEDERATION__.moduleInfo);
const setGlobalSnapshotInfoByModuleInfo = (remoteInfo, moduleDetailInfo) => {
const moduleKey = require_tool.getFMId(remoteInfo);
nativeGlobal.__FEDERATION__.moduleInfo[moduleKey] = moduleDetailInfo;
return nativeGlobal.__FEDERATION__.moduleInfo;
};
const addGlobalSnapshot = (moduleInfos) => {
nativeGlobal.__FEDERATION__.moduleInfo = {
...nativeGlobal.__FEDERATION__.moduleInfo,
...moduleInfos
};
return () => {
const keys = Object.keys(moduleInfos);
for (const key of keys) delete nativeGlobal.__FEDERATION__.moduleInfo[key];
};
};
const getRemoteEntryExports = (name, globalName) => {
const remoteEntryKey = globalName || `__FEDERATION_${name}:custom__`;
return {
remoteEntryKey,
entryExports: CurrentGlobal[remoteEntryKey]
};
};
const registerGlobalPlugins = (plugins) => {
const { __GLOBAL_PLUGIN__ } = nativeGlobal.__FEDERATION__;
plugins.forEach((plugin) => {
if (__GLOBAL_PLUGIN__.findIndex((p) => p.name === plugin.name) === -1) __GLOBAL_PLUGIN__.push(plugin);
else require_logger.warn(`The plugin ${plugin.name} has been registered.`);
});
};
const getGlobalHostPlugins = () => nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;
const getPreloaded = (id) => CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.get(id);
const setPreloaded = (id) => CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
//#endregion
exports.CurrentGlobal = CurrentGlobal;
exports.Global = Global;
exports.addGlobalSnapshot = addGlobalSnapshot;
exports.getGlobalFederationConstructor = getGlobalFederationConstructor;
exports.getGlobalHostPlugins = getGlobalHostPlugins;
exports.getGlobalSnapshot = getGlobalSnapshot;
exports.getGlobalSnapshotInfoByModuleInfo = getGlobalSnapshotInfoByModuleInfo;
exports.getInfoWithoutType = getInfoWithoutType;
exports.getPreloaded = getPreloaded;
exports.getRemoteEntryExports = getRemoteEntryExports;
exports.getTargetSnapshotInfoByModuleInfo = getTargetSnapshotInfoByModuleInfo;
exports.globalLoading = globalLoading;
exports.nativeGlobal = nativeGlobal;
exports.registerGlobalPlugins = registerGlobalPlugins;
exports.resetFederationGlobalInfo = resetFederationGlobalInfo;
exports.setGlobalFederationConstructor = setGlobalFederationConstructor;
exports.setGlobalFederationInstance = setGlobalFederationInstance;
exports.setGlobalSnapshotInfoByModuleInfo = setGlobalSnapshotInfoByModuleInfo;
exports.setPreloaded = setPreloaded;
//# sourceMappingURL=global.cjs.map