Skip to content

feat(nf): Added shareScope for clustering externals #906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,6 +35,7 @@ export interface NormalizedSharedConfig {
singleton: boolean;
strictVersion: boolean;
requiredVersion: string;
shareScope?: string;
version?: string;
includeSecondaries?: boolean;
platform: 'browser' | 'node';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions libs/native-federation-core/src/lib/core/bundle-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions libs/native-federation-runtime/src/lib/model/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@ 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}`;
}

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<string, string>());
}
externalsByScope.get(shared.shareScope)?.set(packageKey, url);
}
externals.set(packageKey, url);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type SharedInfo = {
requiredVersion: string;
version?: string;
packageName: string;
shareScope?: string;
outFileName: string;
dev?: {
entryPoint: string;
Expand Down
2 changes: 2 additions & 0 deletions libs/native-federation-runtime/src/lib/model/global-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const nfNamespace = '__NATIVE_FEDERATION__';

export type NfCache = {
externals: Map<string, string>;
externalsByScope: Map<string, Map<string, string>>;
remoteNamesToRemote: Map<string, Remote>;
baseUrlToRemoteNames: Map<string, string>;
};
Expand All @@ -16,6 +17,7 @@ const global = globalThis as unknown as Global;

global[nfNamespace] ??= {
externals: new Map<string, string>(),
externalsByScope: new Map<string, Map<string, string>>(),
remoteNamesToRemote: new Map<string, Remote>(),
baseUrlToRemoteNames: new Map<string, string>(),
};
Expand Down