Skip to content

Commit 85c5dbd

Browse files
author
Heiner Pöpping
committed
Update module federation initialization
Prepares migration from the deprecated init-function to createInstance
1 parent 50fbb41 commit 85c5dbd

3 files changed

Lines changed: 24 additions & 24 deletions

File tree

src/host/module/utils/loadComponent.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React from "react";
22

33
export const loadModule = (scope: string, module: string, url: string, preventSingleton = false) => {
4-
if (!globalThis.moduleFederationRuntime || !globalThis.moduleFederationScopes) {
4+
if (!globalThis.moduleFederationScopes) {
55
throw new Error('[chayns-api] moduleFederationSharing has not been initialized. Make sure to call initModuleFederationSharing.');
66
}
77

8-
const { loadRemote, registerRemotes } = globalThis.moduleFederationRuntime;
9-
const { registeredScopes, moduleMap, componentMap } = globalThis.moduleFederationScopes;
8+
const { registeredScopes, moduleMap, componentMap, getInstance } = globalThis.moduleFederationScopes;
109
try {
1110
// try simplifying url to avoid force when url is semantically the same, e.g.
1211
// https://example.com/remoteEntry.js and https://example.com/js/../remoteEntry.js
@@ -18,7 +17,7 @@ export const loadModule = (scope: string, module: string, url: string, preventSi
1817
if (scope in registeredScopes) {
1918
console.error(`[chayns-api] call registerRemote with force for scope ${scope}. url: ${url}`);
2019
}
21-
registerRemotes([
20+
getInstance().registerRemotes([
2221
{
2322
shareScope: url.endsWith('v2.remoteEntry.js') || url.endsWith('mf-manifest.json') ? 'chayns-api' : 'default',
2423
name: scope,
@@ -35,7 +34,7 @@ export const loadModule = (scope: string, module: string, url: string, preventSi
3534
if (!(module in moduleMap[scope])) {
3635
const path = `${scope}/${module.replace(/^\.\//, '')}`;
3736

38-
const promise = loadRemote(path);
37+
const promise = getInstance().loadRemote(path);
3938

4039
promise.catch((e) => {
4140
console.error("[chayns-api] Failed to load module", scope, url, e);
@@ -53,12 +52,11 @@ const loadComponent = (scope: string, module: string, url: string, skipCompatMod
5352
console.warn('[chayns-api] skipCompatMode-option is deprecated and is set automatically now');
5453
}
5554

56-
if (!globalThis.moduleFederationRuntime || !globalThis.moduleFederationScopes) {
55+
if (!globalThis.moduleFederationScopes) {
5756
throw new Error('[chayns-api] moduleFederationSharing has not been initialized. Make sure to call initModuleFederationSharing.');
5857
}
5958

60-
const { getInstance } = globalThis.moduleFederationRuntime;
61-
const { componentMap, registeredScopes } = globalThis.moduleFederationScopes;
59+
const { componentMap, registeredScopes, getInstance } = globalThis.moduleFederationScopes;
6260

6361
if (!componentMap[scope]) {
6462
componentMap[scope] = {};
@@ -70,7 +68,7 @@ const loadComponent = (scope: string, module: string, url: string, skipCompatMod
7068
return Module;
7169
}
7270

73-
const shareScopes = getInstance()!.shareScopeMap;
71+
const shareScopes = getInstance().shareScopeMap;
7472

7573
const sharedReact = shareScopes['chayns-api'].react?.[React.version];
7674
const matchReactVersion = sharedReact && sharedReact.useIn.includes(scope) && sharedReact.lib?.() === React;

src/types/global.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ declare var moduleFederationScopes: {
22
registeredScopes: Record<string, string>;
33
componentMap: Record<string, Record<string, React.LazyExoticComponent<React.ComponentType<any>>>>
44
moduleMap: Record<string, any>;
5+
getInstance: () => import('@module-federation/enhanced/runtime').ModuleFederation;
56
};
6-
7-
declare var moduleFederationRuntime: typeof import('@module-federation/enhanced/runtime');
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import { SequentialLoadPlugin } from '../plugins/SequentialLoadPlugin';
4+
45
let ReactDOMClient;
56
try {
67
ReactDOMClient = require('react-dom/client');
@@ -10,18 +11,13 @@ try {
1011

1112
export const initModuleFederationSharing = ({ name, plugins = [] }) => {
1213
// forces single instance of module federation runtime
13-
if(globalThis.moduleFederationRuntime) {
14+
if (globalThis.moduleFederationScopes) {
1415
return;
1516
}
1617

17-
globalThis.moduleFederationRuntime = require('@module-federation/enhanced/runtime');
18-
globalThis.moduleFederationScopes = {
19-
registeredScopes: {},
20-
moduleMap: {},
21-
componentMap: {},
22-
}
23-
24-
const { createInstance } = globalThis.moduleFederationRuntime;
18+
const runtime = require('@module-federation/enhanced/runtime');
19+
globalThis.moduleFederationRuntime = runtime;
20+
const { init } = runtime;
2521

2622
const shared = {
2723
react: {
@@ -39,14 +35,21 @@ export const initModuleFederationSharing = ({ name, plugins = [] }) => {
3935
shared['react-dom/client'] = {
4036
version: React.version,
4137
scope: 'chayns-api',
42-
lib: () => ReactDOMClient
43-
}
38+
lib: () => ReactDOMClient,
39+
};
4440
}
4541

46-
createInstance({
42+
const instance = init({
4743
name: name ?? '',
4844
remotes: [],
4945
shared,
5046
plugins: [SequentialLoadPlugin(), ...plugins],
5147
});
52-
}
48+
49+
globalThis.moduleFederationScopes = {
50+
registeredScopes: {},
51+
moduleMap: {},
52+
componentMap: {},
53+
getInstance: () => instance,
54+
};
55+
};

0 commit comments

Comments
 (0)