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

142 lines
5.9 KiB
JavaScript

import { error, warn as warn$1 } from "./utils/logger.js";
import { getFMId } from "./utils/tool.js";
import { isDebugMode } from "@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 = 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 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, 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 = 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 = 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 warn$1(`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
export { CurrentGlobal, Global, addGlobalSnapshot, getGlobalFederationConstructor, getGlobalHostPlugins, getGlobalSnapshot, getGlobalSnapshotInfoByModuleInfo, getInfoWithoutType, getPreloaded, getRemoteEntryExports, getTargetSnapshotInfoByModuleInfo, globalLoading, nativeGlobal, registerGlobalPlugins, resetFederationGlobalInfo, setGlobalFederationConstructor, setGlobalFederationInstance, setGlobalSnapshotInfoByModuleInfo, setPreloaded };
//# sourceMappingURL=global.js.map