Skip to content

Commit 7dbc25d

Browse files
NstttScriptedAlchemy2heal1
authored
feat: expose setGlobalFederationInstance from @module-federation/enhanced/runtime (#3975)
Co-authored-by: Zack Jackson <[email protected]> Co-authored-by: Hanric <[email protected]>
1 parent cca729a commit 7dbc25d

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

.changeset/two-books-notice.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+
Register globalFederationInstance from createInstance

packages/runtime/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export function createInstance(options: UserOptions) {
3030
// Retrieve debug constructor
3131
const ModuleFederationConstructor =
3232
getGlobalFederationConstructor() || ModuleFederation;
33-
return new ModuleFederationConstructor(options);
33+
const instance = new ModuleFederationConstructor(options);
34+
setGlobalFederationInstance(instance);
35+
return instance;
3436
}
3537

3638
let FederationInstance: ModuleFederation | null = null;

packages/runtime/src/utils.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ModuleFederation } from '@module-federation/runtime-core';
1+
import type { ModuleFederation } from '@module-federation/runtime-core';
22
import { CurrentGlobal } from '@module-federation/runtime-core';
33

44
// injected by bundler, so it can not use runtime-core stuff
@@ -15,26 +15,28 @@ export function getGlobalFederationInstance(
1515
version: string | undefined,
1616
): ModuleFederation | undefined {
1717
const buildId = getBuilderId();
18-
return CurrentGlobal.__FEDERATION__.__INSTANCES__.find((GMInstance) => {
19-
if (buildId && GMInstance.options.id === buildId) {
20-
return true;
21-
}
18+
return CurrentGlobal.__FEDERATION__.__INSTANCES__.find(
19+
(GMInstance: ModuleFederation) => {
20+
if (buildId && GMInstance.options.id === buildId) {
21+
return true;
22+
}
2223

23-
if (
24-
GMInstance.options.name === name &&
25-
!GMInstance.options.version &&
26-
!version
27-
) {
28-
return true;
29-
}
24+
if (
25+
GMInstance.options.name === name &&
26+
!GMInstance.options.version &&
27+
!version
28+
) {
29+
return true;
30+
}
3031

31-
if (
32-
GMInstance.options.name === name &&
33-
version &&
34-
GMInstance.options.version === version
35-
) {
36-
return true;
37-
}
38-
return false;
39-
});
32+
if (
33+
GMInstance.options.name === name &&
34+
version &&
35+
GMInstance.options.version === version
36+
) {
37+
return true;
38+
}
39+
return false;
40+
},
41+
);
4042
}

0 commit comments

Comments
 (0)