@@ -25,14 +25,15 @@ export interface Federation {
25
25
__MANIFEST_LOADING__ : Record < string , Promise < ModuleInfo > > ;
26
26
__PRELOADED_MAP__ : Map < string , boolean > ;
27
27
}
28
-
28
+ export const CurrentGlobal =
29
+ typeof globalThis === 'object' ? globalThis : window ;
29
30
export const nativeGlobal : typeof global = ( ( ) => {
30
31
try {
31
32
// get real window (incase of sandbox)
32
33
return document . defaultView ;
33
34
} catch {
34
35
// node env
35
- return globalThis ;
36
+ return CurrentGlobal ;
36
37
}
37
38
} ) ( ) as typeof global ;
38
39
@@ -50,7 +51,7 @@ declare global {
50
51
}
51
52
52
53
function definePropertyGlobalVal (
53
- target : typeof globalThis ,
54
+ target : typeof CurrentGlobal ,
54
55
key : string ,
55
56
val : any ,
56
57
) {
@@ -61,20 +62,20 @@ function definePropertyGlobalVal(
61
62
} ) ;
62
63
}
63
64
64
- function includeOwnProperty ( target : typeof globalThis , key : string ) {
65
+ function includeOwnProperty ( target : typeof CurrentGlobal , key : string ) {
65
66
return Object . hasOwnProperty . call ( target , key ) ;
66
67
}
67
68
68
69
// This section is to prevent encapsulation by certain microfrontend frameworks. Due to reuse policies, sandbox escapes.
69
70
// The sandbox in the microfrontend does not replicate the value of 'configurable'.
70
71
// 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__' , { } ) ;
73
74
}
74
75
75
- export const globalLoading = globalThis . __GLOBAL_LOADING_REMOTE_ENTRY__ ;
76
+ export const globalLoading = CurrentGlobal . __GLOBAL_LOADING_REMOTE_ENTRY__ ;
76
77
77
- function setGlobalDefaultVal ( target : typeof globalThis ) {
78
+ function setGlobalDefaultVal ( target : typeof CurrentGlobal ) {
78
79
if (
79
80
includeOwnProperty ( target , '__VMOK__' ) &&
80
81
! includeOwnProperty ( target , '__FEDERATION__' )
@@ -103,23 +104,23 @@ function setGlobalDefaultVal(target: typeof globalThis) {
103
104
target . __FEDERATION__ . __PRELOADED_MAP__ ??= new Map ( ) ;
104
105
}
105
106
106
- setGlobalDefaultVal ( globalThis ) ;
107
+ setGlobalDefaultVal ( CurrentGlobal ) ;
107
108
setGlobalDefaultVal ( nativeGlobal ) ;
108
109
109
110
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__ = { } ;
115
116
}
116
117
117
118
export function getGlobalFederationInstance (
118
119
name : string ,
119
120
version : string | undefined ,
120
121
) : FederationHost | undefined {
121
122
const buildId = getBuilderId ( ) ;
122
- return globalThis . __FEDERATION__ . __INSTANCES__ . find ( ( GMInstance ) => {
123
+ return CurrentGlobal . __FEDERATION__ . __INSTANCES__ . find ( ( GMInstance ) => {
123
124
if ( buildId && GMInstance . options . id === getBuilderId ( ) ) {
124
125
return true ;
125
126
}
@@ -146,22 +147,22 @@ export function getGlobalFederationInstance(
146
147
export function setGlobalFederationInstance (
147
148
FederationInstance : FederationHost ,
148
149
) : void {
149
- globalThis . __FEDERATION__ . __INSTANCES__ . push ( FederationInstance ) ;
150
+ CurrentGlobal . __FEDERATION__ . __INSTANCES__ . push ( FederationInstance ) ;
150
151
}
151
152
152
153
export function getGlobalFederationConstructor ( ) :
153
154
| typeof FederationHost
154
155
| undefined {
155
- return globalThis . __FEDERATION__ . __DEBUG_CONSTRUCTOR__ ;
156
+ return CurrentGlobal . __FEDERATION__ . __DEBUG_CONSTRUCTOR__ ;
156
157
}
157
158
158
159
export function setGlobalFederationConstructor (
159
160
FederationConstructor : typeof FederationHost | undefined ,
160
161
isDebug = isDebugMode ( ) ,
161
162
) : void {
162
163
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__ ;
165
166
}
166
167
}
167
168
@@ -282,7 +283,7 @@ export const getRemoteEntryExports = (
282
283
entryExports : RemoteEntryExports | undefined ;
283
284
} => {
284
285
const remoteEntryKey = globalName || `__FEDERATION_${ name } :custom__` ;
285
- const entryExports = ( globalThis as any ) [ remoteEntryKey ] ;
286
+ const entryExports = ( CurrentGlobal as any ) [ remoteEntryKey ] ;
286
287
return {
287
288
remoteEntryKey,
288
289
entryExports,
@@ -311,7 +312,7 @@ export const getGlobalHostPlugins = (): Array<FederationRuntimePlugin> =>
311
312
nativeGlobal . __FEDERATION__ . __GLOBAL_PLUGIN__ ;
312
313
313
314
export const getPreloaded = ( id : string ) =>
314
- globalThis . __FEDERATION__ . __PRELOADED_MAP__ . get ( id ) ;
315
+ CurrentGlobal . __FEDERATION__ . __PRELOADED_MAP__ . get ( id ) ;
315
316
316
317
export const setPreloaded = ( id : string ) =>
317
- globalThis . __FEDERATION__ . __PRELOADED_MAP__ . set ( id , true ) ;
318
+ CurrentGlobal . __FEDERATION__ . __PRELOADED_MAP__ . set ( id , true ) ;
0 commit comments