Skip to content

feat(nf): Use es module shims fully instead of global cache not in sync #517

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
6 changes: 1 addition & 5 deletions libs/native-federation-runtime/src/lib/init-federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
} from './model/import-map';
import { getExternalUrl, setExternalUrl } from './model/externals';
import { joinPaths, getDirectory } from './utils/path-utils';
import { addRemote } from './model/remotes';
import { appendImportMap } from './utils/add-import-map';
import { FederationInfo } from './model/federation-info';

export async function initFederation(
Expand All @@ -22,7 +20,7 @@ export async function initFederation(
const remotesImportMap = await processRemoteInfos(remotes);

const importMap = mergeImportMaps(hostImportMap, remotesImportMap);
appendImportMap(importMap);
importShim.addImportMap(importMap);

return importMap;
}
Expand Down Expand Up @@ -66,8 +64,6 @@ export async function processRemoteInfo(
}

const importMap = createRemoteImportMap(remoteInfo, remoteName, baseUrl);
addRemote(remoteName, { ...remoteInfo, baseUrl });

return importMap;
}

Expand Down
54 changes: 20 additions & 34 deletions libs/native-federation-runtime/src/lib/load-remote-module.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,40 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { appendImportMap } from './utils/add-import-map';
import { processRemoteInfo } from './init-federation';
import {
getRemote,
getRemoteNameByBaseUrl,
isRemoteInitialized,
} from './model/remotes';
import { getDirectory, joinPaths } from './utils/path-utils';

declare function importShim<T>(url: string): T;
import { getDirectory } from './utils/path-utils';
import { getRemoteNameByBaseUrl, isRemoteInitialized } from './model/remotes';

export type LoadRemoteModuleOptions = {
remoteEntry?: string;
remoteName?: string;
exposedModule: string;
};

export async function loadRemoteModule<T = any>(
options: LoadRemoteModuleOptions
): Promise<T>;
export async function loadRemoteModule<T = any>(
export async function loadRemoteModule<
Default = any,
Exports extends object = any
>(options: LoadRemoteModuleOptions): Promise<{ default: Default } & Exports>;
export async function loadRemoteModule<
Default = any,
Exports extends object = any
>(
remoteName: string,
exposedModule: string
): Promise<T>;
export async function loadRemoteModule<T = any>(
): Promise<{ default: Default } & Exports>;
export async function loadRemoteModule<
Default = any,
Exports extends object = any
>(
optionsOrRemoteName: LoadRemoteModuleOptions | string,
exposedModule?: string
): Promise<T> {
): Promise<{ default: Default } & Exports> {
const options = normalizeOptions(optionsOrRemoteName, exposedModule);

await ensureRemoteInitialized(options);

const remoteName = getRemoteNameByOptions(options);
const remote = getRemote(remoteName);

if (!remote) {
throw new Error('unknown remote ' + remoteName);
}

const exposed = remote.exposes.find((e) => e.key === options.exposedModule);

if (!exposed) {
throw new Error(
`Unknown exposed module ${options.exposedModule} in remote ${remoteName}`
);
}

const url = joinPaths(remote.baseUrl, exposed.outFileName);
const module = await importShim<T>(url);

const module = await importShim<Default, Exports>(
`${remoteName}/${options.exposedModule}`
);
return module;
}

Expand Down Expand Up @@ -80,7 +66,7 @@ async function ensureRemoteInitialized(
!isRemoteInitialized(getDirectory(options.remoteEntry))
) {
const importMap = await processRemoteInfo(options.remoteEntry);
appendImportMap(importMap);
importShim.addImportMap(importMap);
}
}

Expand Down
4 changes: 0 additions & 4 deletions libs/native-federation-runtime/src/lib/model/global-cache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Remote } from './remotes';

export const nfNamespace = '__NATIVE_FEDERATION__';

export type NfCache = {
externals: Map<string, string>;
remoteNamesToRemote: Map<string, Remote>;
baseUrlToRemoteNames: Map<string, string>;
};

Expand All @@ -16,7 +13,6 @@ const global = globalThis as unknown as Global;

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

Expand Down
19 changes: 0 additions & 19 deletions libs/native-federation-runtime/src/lib/model/remotes.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
import { FederationInfo } from './federation-info';
import { globalCache } from './global-cache';

export type Remote = FederationInfo & {
baseUrl: string;
};

const remoteNamesToRemote = globalCache.remoteNamesToRemote;
const baseUrlToRemoteNames = globalCache.baseUrlToRemoteNames;

export function addRemote(remoteName: string, remote: Remote): void {
remoteNamesToRemote.set(remoteName, remote);
baseUrlToRemoteNames.set(remote.baseUrl, remoteName);
}

export function getRemoteNameByBaseUrl(baseUrl: string): string | undefined {
return baseUrlToRemoteNames.get(baseUrl);
}

export function isRemoteInitialized(baseUrl: string): boolean {
return baseUrlToRemoteNames.has(baseUrl);
}

export function getRemote(remoteName: string): Remote | undefined {
return remoteNamesToRemote.get(remoteName);
}

export function hasRemote(remoteName: string): boolean {
return remoteNamesToRemote.has(remoteName);
}
10 changes: 0 additions & 10 deletions libs/native-federation-runtime/src/lib/utils/add-import-map.ts

This file was deleted.

2 changes: 1 addition & 1 deletion libs/native-federation-runtime/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": [],
"types": ["es-module-shims"],
"target": "ES2022",
"useDefineForClassFields": false
},
Expand Down
3 changes: 2 additions & 1 deletion libs/native-federation-runtime/tsconfig.lib.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"declarationMap": false,
"target": "ES2022",
"useDefineForClassFields": false
"useDefineForClassFields": false,
"types": ["es-module-shims"]
},
"angularCompilerOptions": {
"compilationMode": "partial"
Expand Down
2 changes: 1 addition & 1 deletion libs/native-federation-runtime/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
"types": ["jest", "node", "es-module-shims"]
},
"files": ["src/test-setup.ts"],
"include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"]
Expand Down