diff --git a/packages/runtime/__tests__/preload-remote.spec.ts b/packages/runtime/__tests__/preload-remote.spec.ts index 922af1b67db..35623a6e8da 100644 --- a/packages/runtime/__tests__/preload-remote.spec.ts +++ b/packages/runtime/__tests__/preload-remote.spec.ts @@ -237,7 +237,7 @@ describe('preload-remote inBrowser', () => { beforeEach(() => { document.head.innerHTML = ''; document.body.innerHTML = ''; - Global.__FEDERATION__.__PRELOADED_MAP__.clear(); + globalThis.__FEDERATION__.__PRELOADED_MAP__.clear(); }); const FMInstance = init({ @@ -284,12 +284,14 @@ describe('preload-remote inBrowser', () => { ]); expect(getPreloadElInfos()).toMatchSnapshot(); - expect(Global.__FEDERATION__.__PRELOADED_MAP__.size).toBe(2); + expect(globalThis.__FEDERATION__.__PRELOADED_MAP__.size).toBe(2); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub1/button'), + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( + '@federation/sub1/button', + ), ).toBe(true); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get( + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( '@federation/sub1-button/button', ), ).toBe(true); @@ -307,17 +309,21 @@ describe('preload-remote inBrowser', () => { ]); expect(getPreloadElInfos()).toMatchSnapshot(); - expect(Global.__FEDERATION__.__PRELOADED_MAP__.size).toBe(3); + expect(globalThis.__FEDERATION__.__PRELOADED_MAP__.size).toBe(3); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub2/button'), + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( + '@federation/sub2/button', + ), ).toBe(true); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get( + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( '@federation/sub2-button/button', ), ).toBe(true); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub2-add/add'), + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( + '@federation/sub2-add/add', + ), ).toBe(true); reset(); }); @@ -325,7 +331,7 @@ describe('preload-remote inBrowser', () => { it('3 preload with expose config ', async () => { const reset = addGlobalSnapshot(mockSnapshot); - expect(Global.__FEDERATION__.__PRELOADED_MAP__.size).toBe(0); + expect(globalThis.__FEDERATION__.__PRELOADED_MAP__.size).toBe(0); await FMInstance.preloadRemote([ { nameOrAlias: '@federation/sub3', @@ -334,9 +340,10 @@ describe('preload-remote inBrowser', () => { }, ]); expect(getPreloadElInfos()).toMatchSnapshot(); - expect(Global.__FEDERATION__.__PRELOADED_MAP__.size).toBe(1); + + expect(globalThis.__FEDERATION__.__PRELOADED_MAP__.size).toBe(1); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub3/add'), + globalThis.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub3/add'), ).toBe(true); await FMInstance.preloadRemote([ diff --git a/packages/runtime/src/global.ts b/packages/runtime/src/global.ts index 969ecb0d167..c45206ac793 100644 --- a/packages/runtime/src/global.ts +++ b/packages/runtime/src/global.ts @@ -22,8 +22,14 @@ export interface Federation { __PRELOADED_MAP__: Map; } -// export const nativeGlobal: typeof global = new Function('return this')(); -export const nativeGlobal: typeof global = new Function('return this')(); +export const nativeGlobal: typeof global = (() => { + try { + return new Function('return this'); + } catch { + return globalThis; + } +})() as typeof global; + export const Global = nativeGlobal; declare global {