From 71fbfa91d240aebf19d21b7b466980d2d1e29566 Mon Sep 17 00:00:00 2001 From: Aukevanoost Date: Fri, 30 May 2025 11:03:11 +0200 Subject: [PATCH] feat(native-federation-runtime): Allow for custom remoteEntry.json files or no host remoteEntry.json --- .../src/lib/init-federation.ts | 45 +++++++++++-------- .../src/lib/model/federation-info.ts | 6 +-- .../src/lib/utils/set-options.ts | 9 ++++ 3 files changed, 37 insertions(+), 23 deletions(-) create mode 100644 libs/native-federation-runtime/src/lib/utils/set-options.ts diff --git a/libs/native-federation-runtime/src/lib/init-federation.ts b/libs/native-federation-runtime/src/lib/init-federation.ts index 611a2d04..97478dea 100644 --- a/libs/native-federation-runtime/src/lib/init-federation.ts +++ b/libs/native-federation-runtime/src/lib/init-federation.ts @@ -11,8 +11,8 @@ import { appendImportMap } from './utils/add-import-map'; import { FederationInfo, InitFederationOptions, - ProcessRemoteInfoOptions, } from './model/federation-info'; +import { setOptions } from './utils/set-options'; /** * Initialize the federation runtime @@ -21,25 +21,30 @@ import { */ export async function initFederation( remotesOrManifestUrl: Record | string = {}, - options?: InitFederationOptions + optionsOverride?: Partial ): Promise { - const cacheOption = options?.cacheTag ? `?t=${options.cacheTag}` : ''; - const remotes = - typeof remotesOrManifestUrl === 'string' - ? await loadManifest(remotesOrManifestUrl + cacheOption) - : remotesOrManifestUrl; - - const url = './remoteEntry.json' + cacheOption; - const hostInfo = await loadFederationInfo(url); - const hostImportMap = await processHostInfo(hostInfo); - const remotesImportMap = await processRemoteInfos(remotes, { - throwIfRemoteNotFound: false, - ...options, - }); - - const importMap = mergeImportMaps(hostImportMap, remotesImportMap); - appendImportMap(importMap); + const options = setOptions(optionsOverride); + + const cacheOption = options.cacheTag ? `?t=${options.cacheTag}` : ''; + + const remotesPromise = typeof remotesOrManifestUrl === 'string' + ? loadManifest(remotesOrManifestUrl + cacheOption) + : Promise.resolve(remotesOrManifestUrl); + + const remotes = await remotesPromise; + const remotesImportMap = await processRemoteInfos(remotes, options); + + let importMap: ImportMap; + if (options.hostRemoteEntry) { + const hostInfoPromise = loadFederationInfo(options.hostRemoteEntry + cacheOption); + const hostInfo = await hostInfoPromise; + const hostImportMap = await processHostInfo(hostInfo); + importMap = mergeImportMaps(hostImportMap, remotesImportMap); + } else { + importMap = remotesImportMap; + } + appendImportMap(importMap); return importMap; } @@ -49,8 +54,10 @@ async function loadManifest(remotes: string): Promise> { export async function processRemoteInfos( remotes: Record, - options: ProcessRemoteInfoOptions = { throwIfRemoteNotFound: false } + optionsOverride?: Partial ): Promise { + const options = setOptions(optionsOverride); + const processRemoteInfoPromises = Object.keys(remotes).map( async (remoteName) => { try { diff --git a/libs/native-federation-runtime/src/lib/model/federation-info.ts b/libs/native-federation-runtime/src/lib/model/federation-info.ts index 090938e6..08c62e76 100644 --- a/libs/native-federation-runtime/src/lib/model/federation-info.ts +++ b/libs/native-federation-runtime/src/lib/model/federation-info.ts @@ -26,8 +26,6 @@ export interface FederationInfo { export interface InitFederationOptions { cacheTag?: string; -} - -export interface ProcessRemoteInfoOptions extends InitFederationOptions { + hostRemoteEntry: string|false; throwIfRemoteNotFound: boolean; -} +} \ No newline at end of file diff --git a/libs/native-federation-runtime/src/lib/utils/set-options.ts b/libs/native-federation-runtime/src/lib/utils/set-options.ts new file mode 100644 index 00000000..4830bb88 --- /dev/null +++ b/libs/native-federation-runtime/src/lib/utils/set-options.ts @@ -0,0 +1,9 @@ +import { InitFederationOptions } from "../model/federation-info"; + +export function setOptions(overrides: Partial|undefined) { + return { + hostRemoteEntry: "./remoteEntry.json", + throwIfRemoteNotFound: false, + ...overrides + } as InitFederationOptions; +} \ No newline at end of file