Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime): fixed an exception in global object processing in the micro-front-end scenario #1859

Merged
merged 10 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/runtime/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
]
}
},
"build-debug": {
"executor": "nx:run-commands",
"options": {
"parallel": false,
"commands": [
{
"command": "FEDERATION_DEBUG=true nx run runtime:build",
"forwardAllArgs": false
}
]
}
},
"pre-release": {
"executor": "nx:run-commands",
"options": {
Expand Down
9 changes: 9 additions & 0 deletions packages/runtime/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ module.exports = (rollupConfig, projectOptions) => {
const project = projectOptions.project;
const pkg = require(project);

if (rollupConfig.output.format === 'esm' && FEDERATION_DEBUG) {
rollupConfig.output.format = 'iife';
rollupConfig.output.inlineDynamicImports = true;
delete rollupConfig.external;
delete rollupConfig.input.type;
delete rollupConfig.input.helpers;
}

rollupConfig.plugins.push(
replace({
preventAssignment: true,
__VERSION__: `'${pkg.version}'`,
FEDERATION_DEBUG: `'${FEDERATION_DEBUG}'`,
}),
Expand Down
111 changes: 60 additions & 51 deletions packages/runtime/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Federation {

// export const nativeGlobal: typeof global = new Function('return this')();
export const nativeGlobal: typeof global = new Function('return this')();
export const Global = nativeGlobal;

declare global {
// eslint-disable-next-line no-var
Expand All @@ -46,53 +47,60 @@ if (

export const globalLoading = globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__;

//
if (nativeGlobal.__VMOK__) {
nativeGlobal.__FEDERATION__ = nativeGlobal.__VMOK__;
} else if (!nativeGlobal.__FEDERATION__) {
nativeGlobal.__FEDERATION__ = {
__GLOBAL_PLUGIN__: [],
__INSTANCES__: [],
moduleInfo: {},
__SHARE__: {},
__MANIFEST_LOADING__: {},
__SHARE_SCOPE_LOADING__: {},
__PRELOADED_MAP__: new Map(),
};
function setGlobalDefaultVal(target: typeof globalThis) {
if (Object.hasOwnProperty.call(target, '__VMOK__')) {
Object.defineProperty(target, '__FEDERATION__', {
value: target.__VMOK__,
configurable: false,
});
}

if (!Object.hasOwnProperty.call(target, '__FEDERATION__')) {
Object.defineProperty(target, '__FEDERATION__', {
value: {
__GLOBAL_PLUGIN__: [],
__INSTANCES__: [],
moduleInfo: {},
__SHARE__: {},
__MANIFEST_LOADING__: {},
__SHARE_SCOPE_LOADING__: {},
__PRELOADED_MAP__: new Map(),
},
configurable: false,
});
Object.defineProperty(target, '__VMOK__', {
value: target.__FEDERATION__,
configurable: false,
});
}

nativeGlobal.__VMOK__ = nativeGlobal.__FEDERATION__;
target.__FEDERATION__.__GLOBAL_PLUGIN__ ??= [];
target.__FEDERATION__.__INSTANCES__ ??= [];
target.__FEDERATION__.moduleInfo ??= {};
target.__FEDERATION__.__SHARE__ ??= {};
target.__FEDERATION__.__MANIFEST_LOADING__ ??= {};
target.__FEDERATION__.__SHARE_SCOPE_LOADING__ ??= {};
target.__FEDERATION__.__PRELOADED_MAP__ ??= new Map();
}

nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__ ??= [];
nativeGlobal.__FEDERATION__.__INSTANCES__ ??= [];
nativeGlobal.__FEDERATION__.moduleInfo ??= {};
nativeGlobal.__FEDERATION__.__SHARE__ ??= {};
nativeGlobal.__FEDERATION__.__MANIFEST_LOADING__ ??= {};
nativeGlobal.__FEDERATION__.__SHARE_SCOPE_LOADING__ ??= {};
nativeGlobal.__FEDERATION__.__PRELOADED_MAP__ ??= new Map();

export const Global = {
get __FEDERATION__(): (typeof nativeGlobal)['__FEDERATION__'] {
const globalThisVal = new Function('return globalThis')();
return globalThisVal.__FEDERATION__;
},
};
setGlobalDefaultVal(globalThis);
setGlobalDefaultVal(nativeGlobal);

export function resetFederationGlobalInfo(): void {
nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__ = [];
nativeGlobal.__FEDERATION__.__INSTANCES__ = [];
nativeGlobal.__FEDERATION__.moduleInfo = {};
nativeGlobal.__FEDERATION__.__SHARE__ = {};
nativeGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {};
nativeGlobal.__FEDERATION__.__SHARE_SCOPE_LOADING__ = {};
globalThis.__FEDERATION__.__GLOBAL_PLUGIN__ = [];
globalThis.__FEDERATION__.__INSTANCES__ = [];
globalThis.__FEDERATION__.moduleInfo = {};
globalThis.__FEDERATION__.__SHARE__ = {};
globalThis.__FEDERATION__.__MANIFEST_LOADING__ = {};
globalThis.__FEDERATION__.__SHARE_SCOPE_LOADING__ = {};
}

export function getGlobalFederationInstance(
name: string,
version: string | undefined,
): FederationHost | undefined {
const buildId = getBuilderId();
return Global.__FEDERATION__.__INSTANCES__.find((GMInstance) => {
return globalThis.__FEDERATION__.__INSTANCES__.find((GMInstance) => {
if (buildId && GMInstance.options.id === getBuilderId()) {
return true;
}
Expand All @@ -115,21 +123,21 @@ export function getGlobalFederationInstance(
export function setGlobalFederationInstance(
FederationInstance: FederationHost,
): void {
Global.__FEDERATION__.__INSTANCES__.push(FederationInstance);
globalThis.__FEDERATION__.__INSTANCES__.push(FederationInstance);
}

export function getGlobalFederationConstructor():
| typeof FederationHost
| undefined {
return Global.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
return globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
}

export function setGlobalFederationConstructor(
FederationConstructor: typeof FederationHost,
): void {
if (isDebugMode()) {
Global.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
Global.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = __VERSION__;
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = __VERSION__;
}
}

Expand All @@ -155,7 +163,7 @@ export function getInfoWithoutType<T extends object>(
}

export const getGlobalSnapshot = (): GlobalModuleInfo =>
Global.__FEDERATION__.moduleInfo;
nativeGlobal.__FEDERATION__.moduleInfo;

export const getTargetSnapshotInfoByModuleInfo = (
moduleInfo: Optional<Remote, 'alias'>,
Expand Down Expand Up @@ -192,7 +200,7 @@ export const getTargetSnapshotInfoByModuleInfo = (
const { version, ...resModuleInfo } = moduleInfo;
const moduleKeyWithoutVersion = getFMId(resModuleInfo);
const getModuleInfoWithoutVersion = getInfoWithoutType(
Global.__FEDERATION__.moduleInfo,
nativeGlobal.__FEDERATION__.moduleInfo,
moduleKeyWithoutVersion,
getModuleInfoHook,
).value;
Expand All @@ -216,7 +224,7 @@ export const getGlobalSnapshotInfoByModuleInfo = (
): GlobalModuleInfo[string] | undefined =>
getTargetSnapshotInfoByModuleInfo(
moduleInfo,
Global.__FEDERATION__.moduleInfo,
nativeGlobal.__FEDERATION__.moduleInfo,
extraOptions?.getModuleInfoHook,
);

Expand All @@ -225,21 +233,21 @@ export const setGlobalSnapshotInfoByModuleInfo = (
moduleDetailInfo: GlobalModuleInfo[string],
): GlobalModuleInfo => {
const moduleKey = getFMId(remoteInfo);
Global.__FEDERATION__.moduleInfo[moduleKey] = moduleDetailInfo;
return Global.__FEDERATION__.moduleInfo;
nativeGlobal.__FEDERATION__.moduleInfo[moduleKey] = moduleDetailInfo;
return nativeGlobal.__FEDERATION__.moduleInfo;
};

export const addGlobalSnapshot = (
moduleInfos: GlobalModuleInfo,
): (() => void) => {
Global.__FEDERATION__.moduleInfo = {
...Global.__FEDERATION__.moduleInfo,
nativeGlobal.__FEDERATION__.moduleInfo = {
...nativeGlobal.__FEDERATION__.moduleInfo,
...moduleInfos,
};
return () => {
const keys = Object.keys(moduleInfos);
for (const key of keys) {
delete Global.__FEDERATION__.moduleInfo[key];
delete nativeGlobal.__FEDERATION__.moduleInfo[key];
}
};
};
Expand All @@ -266,7 +274,7 @@ export const getRemoteEntryExports = (
export const registerGlobalPlugins = (
plugins: Array<FederationRuntimePlugin>,
): void => {
const { __GLOBAL_PLUGIN__ } = Global.__FEDERATION__;
const { __GLOBAL_PLUGIN__ } = nativeGlobal.__FEDERATION__;

plugins.forEach((plugin) => {
if (__GLOBAL_PLUGIN__.findIndex((p) => p.name === plugin.name) === -1) {
Expand All @@ -278,9 +286,10 @@ export const registerGlobalPlugins = (
};

export const getGlobalHostPlugins = (): Array<FederationRuntimePlugin> =>
Global.__FEDERATION__.__GLOBAL_PLUGIN__;
nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;

export const getPreloaded = (id: string) =>
Global.__FEDERATION__.__PRELOADED_MAP__.get(id);
globalThis.__FEDERATION__.__PRELOADED_MAP__.get(id);

export const setPreloaded = (id: string) =>
Global.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
globalThis.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
Loading