@@ -20,6 +20,7 @@ export interface Federation {
20
20
21
21
// export const nativeGlobal: typeof global = new Function('return this')();
22
22
export const nativeGlobal : typeof global = new Function ( 'return this' ) ( ) ;
23
+ export const Global = nativeGlobal ;
23
24
24
25
declare global {
25
26
// eslint-disable-next-line no-var
@@ -32,67 +33,80 @@ declare global {
32
33
> ;
33
34
}
34
35
35
- // This section is to prevent encapsulation by certain microfrontend frameworks. Due to reuse policies, sandbox escapes.
36
- // The sandbox in the microfrontend does not replicate the value of 'configurable'.
37
- // If there is no loading content on the global object, this section defines the loading object.
38
- if (
39
- ! Object . hasOwnProperty . call ( globalThis , '__GLOBAL_LOADING_REMOTE_ENTRY__' )
36
+ function definePropertyGlobalVal (
37
+ target : typeof globalThis ,
38
+ key : string ,
39
+ val : any ,
40
40
) {
41
- Object . defineProperty ( globalThis , '__GLOBAL_LOADING_REMOTE_ENTRY__' , {
42
- value : { } ,
41
+ Object . defineProperty ( target , key , {
42
+ value : val ,
43
43
configurable : false ,
44
+ writable : true ,
44
45
} ) ;
45
46
}
46
47
48
+ function includeOwnProperty ( target : typeof globalThis , key : string ) {
49
+ return Object . hasOwnProperty . call ( target , key ) ;
50
+ }
51
+
52
+ // This section is to prevent encapsulation by certain microfrontend frameworks. Due to reuse policies, sandbox escapes.
53
+ // The sandbox in the microfrontend does not replicate the value of 'configurable'.
54
+ // If there is no loading content on the global object, this section defines the loading object.
55
+ if ( ! includeOwnProperty ( globalThis , '__GLOBAL_LOADING_REMOTE_ENTRY__' ) ) {
56
+ definePropertyGlobalVal ( globalThis , '__GLOBAL_LOADING_REMOTE_ENTRY__' , { } ) ;
57
+ }
58
+
47
59
export const globalLoading = globalThis . __GLOBAL_LOADING_REMOTE_ENTRY__ ;
48
60
49
- //
50
- if ( nativeGlobal . __VMOK__ ) {
51
- nativeGlobal . __FEDERATION__ = nativeGlobal . __VMOK__ ;
52
- } else if ( ! nativeGlobal . __FEDERATION__ ) {
53
- nativeGlobal . __FEDERATION__ = {
54
- __GLOBAL_PLUGIN__ : [ ] ,
55
- __INSTANCES__ : [ ] ,
56
- moduleInfo : { } ,
57
- __SHARE__ : { } ,
58
- __MANIFEST_LOADING__ : { } ,
59
- __SHARE_SCOPE_LOADING__ : { } ,
60
- __PRELOADED_MAP__ : new Map ( ) ,
61
- } ;
61
+ function setGlobalDefaultVal ( target : typeof globalThis ) {
62
+ if (
63
+ includeOwnProperty ( target , '__VMOK__' ) &&
64
+ ! includeOwnProperty ( target , '__FEDERATION__' )
65
+ ) {
66
+ definePropertyGlobalVal ( target , '__FEDERATION__' , target . __VMOK__ ) ;
67
+ }
62
68
63
- nativeGlobal . __VMOK__ = nativeGlobal . __FEDERATION__ ;
69
+ if ( ! includeOwnProperty ( target , '__FEDERATION__' ) ) {
70
+ definePropertyGlobalVal ( target , '__FEDERATION__' , {
71
+ __GLOBAL_PLUGIN__ : [ ] ,
72
+ __INSTANCES__ : [ ] ,
73
+ moduleInfo : { } ,
74
+ __SHARE__ : { } ,
75
+ __MANIFEST_LOADING__ : { } ,
76
+ __SHARE_SCOPE_LOADING__ : { } ,
77
+ __PRELOADED_MAP__ : new Map ( ) ,
78
+ } ) ;
79
+
80
+ definePropertyGlobalVal ( target , '__VMOK__' , target . __FEDERATION__ ) ;
81
+ }
82
+
83
+ target . __FEDERATION__ . __GLOBAL_PLUGIN__ ??= [ ] ;
84
+ target . __FEDERATION__ . __INSTANCES__ ??= [ ] ;
85
+ target . __FEDERATION__ . moduleInfo ??= { } ;
86
+ target . __FEDERATION__ . __SHARE__ ??= { } ;
87
+ target . __FEDERATION__ . __MANIFEST_LOADING__ ??= { } ;
88
+ target . __FEDERATION__ . __SHARE_SCOPE_LOADING__ ??= { } ;
89
+ target . __FEDERATION__ . __PRELOADED_MAP__ ??= new Map ( ) ;
64
90
}
65
91
66
- nativeGlobal . __FEDERATION__ . __GLOBAL_PLUGIN__ ??= [ ] ;
67
- nativeGlobal . __FEDERATION__ . __INSTANCES__ ??= [ ] ;
68
- nativeGlobal . __FEDERATION__ . moduleInfo ??= { } ;
69
- nativeGlobal . __FEDERATION__ . __SHARE__ ??= { } ;
70
- nativeGlobal . __FEDERATION__ . __MANIFEST_LOADING__ ??= { } ;
71
- nativeGlobal . __FEDERATION__ . __SHARE_SCOPE_LOADING__ ??= { } ;
72
- nativeGlobal . __FEDERATION__ . __PRELOADED_MAP__ ??= new Map ( ) ;
73
-
74
- export const Global = {
75
- get __FEDERATION__ ( ) : ( typeof nativeGlobal ) [ '__FEDERATION__' ] {
76
- const globalThisVal = new Function ( 'return globalThis' ) ( ) ;
77
- return globalThisVal . __FEDERATION__ ;
78
- } ,
79
- } ;
92
+ setGlobalDefaultVal ( globalThis ) ;
93
+ setGlobalDefaultVal ( nativeGlobal ) ;
80
94
81
95
export function resetFederationGlobalInfo ( ) : void {
82
- nativeGlobal . __FEDERATION__ . __GLOBAL_PLUGIN__ = [ ] ;
83
- nativeGlobal . __FEDERATION__ . __INSTANCES__ = [ ] ;
84
- nativeGlobal . __FEDERATION__ . moduleInfo = { } ;
85
- nativeGlobal . __FEDERATION__ . __SHARE__ = { } ;
86
- nativeGlobal . __FEDERATION__ . __MANIFEST_LOADING__ = { } ;
87
- nativeGlobal . __FEDERATION__ . __SHARE_SCOPE_LOADING__ = { } ;
96
+ globalThis . __FEDERATION__ . __GLOBAL_PLUGIN__ = [ ] ;
97
+ globalThis . __FEDERATION__ . __INSTANCES__ = [ ] ;
98
+ globalThis . __FEDERATION__ . moduleInfo = { } ;
99
+ globalThis . __FEDERATION__ . __SHARE__ = { } ;
100
+ globalThis . __FEDERATION__ . __MANIFEST_LOADING__ = { } ;
101
+ globalThis . __FEDERATION__ . __SHARE_SCOPE_LOADING__ = { } ;
88
102
}
89
103
90
104
export function getGlobalFederationInstance (
91
105
name : string ,
92
106
version : string | undefined ,
93
107
) : FederationHost | undefined {
94
108
const buildId = getBuilderId ( ) ;
95
- return Global . __FEDERATION__ . __INSTANCES__ . find ( ( GMInstance ) => {
109
+ return globalThis . __FEDERATION__ . __INSTANCES__ . find ( ( GMInstance ) => {
96
110
if ( buildId && GMInstance . options . id === getBuilderId ( ) ) {
97
111
return true ;
98
112
}
@@ -119,21 +133,21 @@ export function getGlobalFederationInstance(
119
133
export function setGlobalFederationInstance (
120
134
FederationInstance : FederationHost ,
121
135
) : void {
122
- Global . __FEDERATION__ . __INSTANCES__ . push ( FederationInstance ) ;
136
+ globalThis . __FEDERATION__ . __INSTANCES__ . push ( FederationInstance ) ;
123
137
}
124
138
125
139
export function getGlobalFederationConstructor ( ) :
126
140
| typeof FederationHost
127
141
| undefined {
128
- return Global . __FEDERATION__ . __DEBUG_CONSTRUCTOR__ ;
142
+ return globalThis . __FEDERATION__ . __DEBUG_CONSTRUCTOR__ ;
129
143
}
130
144
131
145
export function setGlobalFederationConstructor (
132
146
FederationConstructor : typeof FederationHost ,
133
147
) : void {
134
148
if ( isDebugMode ( ) ) {
135
- Global . __FEDERATION__ . __DEBUG_CONSTRUCTOR__ = FederationConstructor ;
136
- Global . __FEDERATION__ . __DEBUG_CONSTRUCTOR_VERSION__ = __VERSION__ ;
149
+ globalThis . __FEDERATION__ . __DEBUG_CONSTRUCTOR__ = FederationConstructor ;
150
+ globalThis . __FEDERATION__ . __DEBUG_CONSTRUCTOR_VERSION__ = __VERSION__ ;
137
151
}
138
152
}
139
153
@@ -159,7 +173,7 @@ export function getInfoWithoutType<T extends object>(
159
173
}
160
174
161
175
export const getGlobalSnapshot = ( ) : GlobalModuleInfo =>
162
- Global . __FEDERATION__ . moduleInfo ;
176
+ nativeGlobal . __FEDERATION__ . moduleInfo ;
163
177
164
178
export const getTargetSnapshotInfoByModuleInfo = (
165
179
moduleInfo : Optional < Remote , 'alias' > ,
@@ -196,7 +210,7 @@ export const getTargetSnapshotInfoByModuleInfo = (
196
210
const { version, ...resModuleInfo } = moduleInfo ;
197
211
const moduleKeyWithoutVersion = getFMId ( resModuleInfo ) ;
198
212
const getModuleInfoWithoutVersion = getInfoWithoutType (
199
- Global . __FEDERATION__ . moduleInfo ,
213
+ nativeGlobal . __FEDERATION__ . moduleInfo ,
200
214
moduleKeyWithoutVersion ,
201
215
getModuleInfoHook ,
202
216
) . value ;
@@ -220,7 +234,7 @@ export const getGlobalSnapshotInfoByModuleInfo = (
220
234
) : GlobalModuleInfo [ string ] | undefined =>
221
235
getTargetSnapshotInfoByModuleInfo (
222
236
moduleInfo ,
223
- Global . __FEDERATION__ . moduleInfo ,
237
+ nativeGlobal . __FEDERATION__ . moduleInfo ,
224
238
extraOptions ?. getModuleInfoHook ,
225
239
) ;
226
240
@@ -229,21 +243,21 @@ export const setGlobalSnapshotInfoByModuleInfo = (
229
243
moduleDetailInfo : GlobalModuleInfo [ string ] ,
230
244
) : GlobalModuleInfo => {
231
245
const moduleKey = getFMId ( remoteInfo ) ;
232
- Global . __FEDERATION__ . moduleInfo [ moduleKey ] = moduleDetailInfo ;
233
- return Global . __FEDERATION__ . moduleInfo ;
246
+ nativeGlobal . __FEDERATION__ . moduleInfo [ moduleKey ] = moduleDetailInfo ;
247
+ return nativeGlobal . __FEDERATION__ . moduleInfo ;
234
248
} ;
235
249
236
250
export const addGlobalSnapshot = (
237
251
moduleInfos : GlobalModuleInfo ,
238
252
) : ( ( ) => void ) => {
239
- Global . __FEDERATION__ . moduleInfo = {
240
- ...Global . __FEDERATION__ . moduleInfo ,
253
+ nativeGlobal . __FEDERATION__ . moduleInfo = {
254
+ ...nativeGlobal . __FEDERATION__ . moduleInfo ,
241
255
...moduleInfos ,
242
256
} ;
243
257
return ( ) => {
244
258
const keys = Object . keys ( moduleInfos ) ;
245
259
for ( const key of keys ) {
246
- delete Global . __FEDERATION__ . moduleInfo [ key ] ;
260
+ delete nativeGlobal . __FEDERATION__ . moduleInfo [ key ] ;
247
261
}
248
262
} ;
249
263
} ;
@@ -270,7 +284,7 @@ export const getRemoteEntryExports = (
270
284
export const registerGlobalPlugins = (
271
285
plugins : Array < FederationRuntimePlugin > ,
272
286
) : void => {
273
- const { __GLOBAL_PLUGIN__ } = Global . __FEDERATION__ ;
287
+ const { __GLOBAL_PLUGIN__ } = nativeGlobal . __FEDERATION__ ;
274
288
275
289
plugins . forEach ( ( plugin ) => {
276
290
if ( __GLOBAL_PLUGIN__ . findIndex ( ( p ) => p . name === plugin . name ) === - 1 ) {
@@ -282,9 +296,10 @@ export const registerGlobalPlugins = (
282
296
} ;
283
297
284
298
export const getGlobalHostPlugins = ( ) : Array < FederationRuntimePlugin > =>
285
- Global . __FEDERATION__ . __GLOBAL_PLUGIN__ ;
299
+ nativeGlobal . __FEDERATION__ . __GLOBAL_PLUGIN__ ;
286
300
287
301
export const getPreloaded = ( id : string ) =>
288
- Global . __FEDERATION__ . __PRELOADED_MAP__ . get ( id ) ;
302
+ globalThis . __FEDERATION__ . __PRELOADED_MAP__ . get ( id ) ;
303
+
289
304
export const setPreloaded = ( id : string ) =>
290
- Global . __FEDERATION__ . __PRELOADED_MAP__ . set ( id , true ) ;
305
+ globalThis . __FEDERATION__ . __PRELOADED_MAP__ . set ( id , true ) ;
0 commit comments