Skip to content

Commit 8712967

Browse files
authored
fix(runtime): use wrapper global instead of using globalThis directly (#3179)
1 parent 6672aff commit 8712967

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

.changeset/dry-bats-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/runtime': patch
3+
---
4+
5+
fix(runtime): use wrapper global instead of using globalThis directly

packages/runtime/src/global.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ export interface Federation {
2525
__MANIFEST_LOADING__: Record<string, Promise<ModuleInfo>>;
2626
__PRELOADED_MAP__: Map<string, boolean>;
2727
}
28-
28+
export const CurrentGlobal =
29+
typeof globalThis === 'object' ? globalThis : window;
2930
export const nativeGlobal: typeof global = (() => {
3031
try {
3132
// get real window (incase of sandbox)
3233
return document.defaultView;
3334
} catch {
3435
// node env
35-
return globalThis;
36+
return CurrentGlobal;
3637
}
3738
})() as typeof global;
3839

@@ -50,7 +51,7 @@ declare global {
5051
}
5152

5253
function definePropertyGlobalVal(
53-
target: typeof globalThis,
54+
target: typeof CurrentGlobal,
5455
key: string,
5556
val: any,
5657
) {
@@ -61,20 +62,20 @@ function definePropertyGlobalVal(
6162
});
6263
}
6364

64-
function includeOwnProperty(target: typeof globalThis, key: string) {
65+
function includeOwnProperty(target: typeof CurrentGlobal, key: string) {
6566
return Object.hasOwnProperty.call(target, key);
6667
}
6768

6869
// This section is to prevent encapsulation by certain microfrontend frameworks. Due to reuse policies, sandbox escapes.
6970
// The sandbox in the microfrontend does not replicate the value of 'configurable'.
7071
// If there is no loading content on the global object, this section defines the loading object.
71-
if (!includeOwnProperty(globalThis, '__GLOBAL_LOADING_REMOTE_ENTRY__')) {
72-
definePropertyGlobalVal(globalThis, '__GLOBAL_LOADING_REMOTE_ENTRY__', {});
72+
if (!includeOwnProperty(CurrentGlobal, '__GLOBAL_LOADING_REMOTE_ENTRY__')) {
73+
definePropertyGlobalVal(CurrentGlobal, '__GLOBAL_LOADING_REMOTE_ENTRY__', {});
7374
}
7475

75-
export const globalLoading = globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__;
76+
export const globalLoading = CurrentGlobal.__GLOBAL_LOADING_REMOTE_ENTRY__;
7677

77-
function setGlobalDefaultVal(target: typeof globalThis) {
78+
function setGlobalDefaultVal(target: typeof CurrentGlobal) {
7879
if (
7980
includeOwnProperty(target, '__VMOK__') &&
8081
!includeOwnProperty(target, '__FEDERATION__')
@@ -103,23 +104,23 @@ function setGlobalDefaultVal(target: typeof globalThis) {
103104
target.__FEDERATION__.__PRELOADED_MAP__ ??= new Map();
104105
}
105106

106-
setGlobalDefaultVal(globalThis);
107+
setGlobalDefaultVal(CurrentGlobal);
107108
setGlobalDefaultVal(nativeGlobal);
108109

109110
export function resetFederationGlobalInfo(): void {
110-
globalThis.__FEDERATION__.__GLOBAL_PLUGIN__ = [];
111-
globalThis.__FEDERATION__.__INSTANCES__ = [];
112-
globalThis.__FEDERATION__.moduleInfo = {};
113-
globalThis.__FEDERATION__.__SHARE__ = {};
114-
globalThis.__FEDERATION__.__MANIFEST_LOADING__ = {};
111+
CurrentGlobal.__FEDERATION__.__GLOBAL_PLUGIN__ = [];
112+
CurrentGlobal.__FEDERATION__.__INSTANCES__ = [];
113+
CurrentGlobal.__FEDERATION__.moduleInfo = {};
114+
CurrentGlobal.__FEDERATION__.__SHARE__ = {};
115+
CurrentGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {};
115116
}
116117

117118
export function getGlobalFederationInstance(
118119
name: string,
119120
version: string | undefined,
120121
): FederationHost | undefined {
121122
const buildId = getBuilderId();
122-
return globalThis.__FEDERATION__.__INSTANCES__.find((GMInstance) => {
123+
return CurrentGlobal.__FEDERATION__.__INSTANCES__.find((GMInstance) => {
123124
if (buildId && GMInstance.options.id === getBuilderId()) {
124125
return true;
125126
}
@@ -146,22 +147,22 @@ export function getGlobalFederationInstance(
146147
export function setGlobalFederationInstance(
147148
FederationInstance: FederationHost,
148149
): void {
149-
globalThis.__FEDERATION__.__INSTANCES__.push(FederationInstance);
150+
CurrentGlobal.__FEDERATION__.__INSTANCES__.push(FederationInstance);
150151
}
151152

152153
export function getGlobalFederationConstructor():
153154
| typeof FederationHost
154155
| undefined {
155-
return globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
156+
return CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
156157
}
157158

158159
export function setGlobalFederationConstructor(
159160
FederationConstructor: typeof FederationHost | undefined,
160161
isDebug = isDebugMode(),
161162
): void {
162163
if (isDebug) {
163-
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
164-
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = __VERSION__;
164+
CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
165+
CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = __VERSION__;
165166
}
166167
}
167168

@@ -282,7 +283,7 @@ export const getRemoteEntryExports = (
282283
entryExports: RemoteEntryExports | undefined;
283284
} => {
284285
const remoteEntryKey = globalName || `__FEDERATION_${name}:custom__`;
285-
const entryExports = (globalThis as any)[remoteEntryKey];
286+
const entryExports = (CurrentGlobal as any)[remoteEntryKey];
286287
return {
287288
remoteEntryKey,
288289
entryExports,
@@ -311,7 +312,7 @@ export const getGlobalHostPlugins = (): Array<FederationRuntimePlugin> =>
311312
nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;
312313

313314
export const getPreloaded = (id: string) =>
314-
globalThis.__FEDERATION__.__PRELOADED_MAP__.get(id);
315+
CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.get(id);
315316

316317
export const setPreloaded = (id: string) =>
317-
globalThis.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
318+
CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.set(id, true);

packages/runtime/src/remote/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010
RUNTIME_004,
1111
runtimeDescMap,
1212
} from '@module-federation/error-codes';
13-
import { Global, getInfoWithoutType, globalLoading } from '../global';
13+
import {
14+
Global,
15+
getInfoWithoutType,
16+
globalLoading,
17+
CurrentGlobal,
18+
} from '../global';
1419
import {
1520
Options,
1621
UserOptions,
@@ -462,14 +467,16 @@ export class RemoteHandler {
462467
const loadedModule = host.moduleCache.get(remote.name);
463468
if (loadedModule) {
464469
const remoteInfo = loadedModule.remoteInfo;
465-
const key = remoteInfo.entryGlobalName as keyof typeof globalThis;
470+
const key = remoteInfo.entryGlobalName as keyof typeof CurrentGlobal;
466471

467-
if (globalThis[key]) {
468-
if (Object.getOwnPropertyDescriptor(globalThis, key)?.configurable) {
469-
delete globalThis[key];
472+
if (CurrentGlobal[key]) {
473+
if (
474+
Object.getOwnPropertyDescriptor(CurrentGlobal, key)?.configurable
475+
) {
476+
delete CurrentGlobal[key];
470477
} else {
471478
// @ts-ignore
472-
globalThis[key] = undefined;
479+
CurrentGlobal[key] = undefined;
473480
}
474481
}
475482
const remoteEntryUniqueKey = getRemoteEntryUniqueKey(
@@ -487,7 +494,7 @@ export class RemoteHandler {
487494
? composeKeyWithSeparator(remoteInfo.name, remoteInfo.buildVersion)
488495
: remoteInfo.name;
489496
const remoteInsIndex =
490-
globalThis.__FEDERATION__.__INSTANCES__.findIndex((ins) => {
497+
CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {
491498
if (remoteInfo.buildVersion) {
492499
return ins.options.id === remoteInsId;
493500
} else {
@@ -496,7 +503,7 @@ export class RemoteHandler {
496503
});
497504
if (remoteInsIndex !== -1) {
498505
const remoteIns =
499-
globalThis.__FEDERATION__.__INSTANCES__[remoteInsIndex];
506+
CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];
500507
remoteInsId = remoteIns.options.id || remoteInsId;
501508
const globalShareScopeMap = getGlobalShareScope();
502509

@@ -558,7 +565,7 @@ export class RemoteHandler {
558565
];
559566
},
560567
);
561-
globalThis.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
568+
CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
562569
}
563570

564571
const { hostGlobalSnapshot } = getGlobalRemoteInfo(remote, host);

0 commit comments

Comments
 (0)