From 15b71d08036811eca4aa4c81953cd45d9851fd2f Mon Sep 17 00:00:00 2001 From: Guilherme Prezzi Date: Wed, 24 Jul 2024 05:38:20 -0300 Subject: [PATCH] feat(module-federation): support setremotedefinition api (#27051) ## Current Behavior The only method available to manage remotes is `setRemoteDefinitions` method, that overwrite the previous state, as expected. The problem is that in some cases, when it needed to add more remotes dynamically, this API replace it current state, removing the previous remotes. ## Expected Behavior Expose a new API called `setRemoteDefinition(remoteName: string, remoteUrl: string)` that adds or replace a new remote definition in the remotes map, without overwrite the remotes map. ## Related Issue(s) Fixes #27050 --------- Co-authored-by: Guilherme Prezzi --- packages/angular/mf/mf.ts | 6 +++++- packages/react/mf/dynamic-federation.ts | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/angular/mf/mf.ts b/packages/angular/mf/mf.ts index 7c3a9e48b7305..a1d65b1926f48 100644 --- a/packages/angular/mf/mf.ts +++ b/packages/angular/mf/mf.ts @@ -12,11 +12,15 @@ export function setRemoteUrlResolver( resolveRemoteUrl = _resolveRemoteUrl; } -let remoteUrlDefinitions: Record; +let remoteUrlDefinitions: Record = {}; export function setRemoteDefinitions(definitions: Record) { remoteUrlDefinitions = definitions; } +export function setRemoteDefinition(remoteName: string, remoteUrl: string) { + remoteUrlDefinitions[remoteName] = remoteUrl; +} + let remoteModuleMap = new Map(); let remoteContainerMap = new Map(); export async function loadRemoteModule(remoteName: string, moduleName: string) { diff --git a/packages/react/mf/dynamic-federation.ts b/packages/react/mf/dynamic-federation.ts index 4584452c2d171..5f5e60eeec7e2 100644 --- a/packages/react/mf/dynamic-federation.ts +++ b/packages/react/mf/dynamic-federation.ts @@ -15,7 +15,7 @@ declare const document: { declare const __webpack_init_sharing__: (scope: 'default') => Promise; declare const __webpack_share_scopes__: { default: unknown }; -let remoteUrlDefinitions: Record; +let remoteUrlDefinitions: Record = {}; let resolveRemoteUrl: ResolveRemoteUrlFunction; const remoteModuleMap = new Map(); const remoteContainerMap = new Map(); @@ -31,6 +31,10 @@ export function setRemoteDefinitions(definitions: Record) { remoteUrlDefinitions = definitions; } +export function setRemoteDefinition(remoteName: string, remoteUrl: string) { + remoteUrlDefinitions[remoteName] = remoteUrl; +} + export async function loadRemoteModule(remoteName: string, moduleName: string) { const remoteModuleKey = `${remoteName}:${moduleName}`; if (remoteModuleMap.has(remoteModuleKey)) {