diff --git a/libs/native-federation-core/src/lib/config/federation-config.ts b/libs/native-federation-core/src/lib/config/federation-config.ts index 8d61d74..8b2d22b 100644 --- a/libs/native-federation-core/src/lib/config/federation-config.ts +++ b/libs/native-federation-core/src/lib/config/federation-config.ts @@ -4,6 +4,7 @@ import { MappedPath } from '../utils/mapped-paths'; export interface SharedConfig { singleton?: boolean; strictVersion?: boolean; + shareScope?: string; requiredVersion?: string; version?: string; includeSecondaries?: boolean; @@ -34,6 +35,7 @@ export interface NormalizedSharedConfig { singleton: boolean; strictVersion: boolean; requiredVersion: string; + shareScope?: string; version?: string; includeSecondaries?: boolean; platform: 'browser' | 'node'; diff --git a/libs/native-federation-core/src/lib/config/with-native-federation.ts b/libs/native-federation-core/src/lib/config/with-native-federation.ts index 7681625..68d6a13 100644 --- a/libs/native-federation-core/src/lib/config/with-native-federation.ts +++ b/libs/native-federation-core/src/lib/config/with-native-federation.ts @@ -80,6 +80,7 @@ function normalizeShared( requiredVersion: shared[cur].requiredVersion ?? 'auto', singleton: shared[cur].singleton ?? false, strictVersion: shared[cur].strictVersion ?? false, + shareScope: shared[cur].shareScope, version: shared[cur].version, includeSecondaries: shared[cur].includeSecondaries, packageInfo: shared[cur].packageInfo, diff --git a/libs/native-federation-core/src/lib/core/bundle-shared.ts b/libs/native-federation-core/src/lib/core/bundle-shared.ts index 7d11af1..f182a9a 100644 --- a/libs/native-federation-core/src/lib/core/bundle-shared.ts +++ b/libs/native-federation-core/src/lib/core/bundle-shared.ts @@ -260,6 +260,7 @@ function buildResult( singleton: shared.singleton, strictVersion: shared.strictVersion, version: pi.version, + shareScope: shared.shareScope, // TODO: Decide whether/when we need debug infos // dev: !fedOptions.dev // ? undefined diff --git a/libs/native-federation-runtime/src/lib/model/externals.ts b/libs/native-federation-runtime/src/lib/model/externals.ts index 41b56d7..51f4d17 100644 --- a/libs/native-federation-runtime/src/lib/model/externals.ts +++ b/libs/native-federation-runtime/src/lib/model/externals.ts @@ -2,6 +2,7 @@ import { SharedInfo } from './federation-info'; import { globalCache } from './global-cache'; const externals = globalCache.externals; +const externalsByScope = globalCache.externalsByScope; function getExternalKey(shared: SharedInfo) { return `${shared.packageName}@${shared.version}`; @@ -9,10 +10,22 @@ function getExternalKey(shared: SharedInfo) { export function getExternalUrl(shared: SharedInfo): string | undefined { const packageKey = getExternalKey(shared); + if ( + shared.shareScope && + externalsByScope.get(shared.shareScope)?.has(packageKey) + ) { + return externalsByScope.get(shared.shareScope)?.get(packageKey); + } return externals.get(packageKey); } export function setExternalUrl(shared: SharedInfo, url: string): void { const packageKey = getExternalKey(shared); + if (shared.shareScope) { + if (!externalsByScope.has(shared.shareScope)) { + externalsByScope.set(shared.shareScope, new Map()); + } + externalsByScope.get(shared.shareScope)?.set(packageKey, url); + } externals.set(packageKey, url); } 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 f339638..8367a5c 100644 --- a/libs/native-federation-runtime/src/lib/model/federation-info.ts +++ b/libs/native-federation-runtime/src/lib/model/federation-info.ts @@ -4,6 +4,7 @@ export type SharedInfo = { requiredVersion: string; version?: string; packageName: string; + shareScope?: string; outFileName: string; dev?: { entryPoint: string; diff --git a/libs/native-federation-runtime/src/lib/model/global-cache.ts b/libs/native-federation-runtime/src/lib/model/global-cache.ts index 99492ed..f432b6c 100644 --- a/libs/native-federation-runtime/src/lib/model/global-cache.ts +++ b/libs/native-federation-runtime/src/lib/model/global-cache.ts @@ -4,6 +4,7 @@ export const nfNamespace = '__NATIVE_FEDERATION__'; export type NfCache = { externals: Map; + externalsByScope: Map>; remoteNamesToRemote: Map; baseUrlToRemoteNames: Map; }; @@ -16,6 +17,7 @@ const global = globalThis as unknown as Global; global[nfNamespace] ??= { externals: new Map(), + externalsByScope: new Map>(), remoteNamesToRemote: new Map(), baseUrlToRemoteNames: new Map(), };