Skip to content

Commit

Permalink
feat(nf): Use es module shims fully instead of global cache not in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jogelin committed Mar 16, 2024
1 parent 2cdad2b commit c0b15cb
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 75 deletions.
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,

Check warning on line 12 in libs/native-federation-runtime/src/lib/load-remote-module.ts

View workflow job for this annotation

GitHub Actions / main

Unexpected any. Specify a different type
Exports extends object = any

Check warning on line 13 in libs/native-federation-runtime/src/lib/load-remote-module.ts

View workflow job for this annotation

GitHub Actions / main

Unexpected any. Specify a different type
>(options: LoadRemoteModuleOptions): Promise<{ default: Default } & Exports>;
export async function loadRemoteModule<
Default = any,

Check warning on line 16 in libs/native-federation-runtime/src/lib/load-remote-module.ts

View workflow job for this annotation

GitHub Actions / main

Unexpected any. Specify a different type
Exports extends object = any

Check warning on line 17 in libs/native-federation-runtime/src/lib/load-remote-module.ts

View workflow job for this annotation

GitHub Actions / main

Unexpected any. Specify a different type
>(
remoteName: string,
exposedModule: string
): Promise<T>;
export async function loadRemoteModule<T = any>(
): Promise<{ default: Default } & Exports>;
export async function loadRemoteModule<
Default = any,

Check warning on line 23 in libs/native-federation-runtime/src/lib/load-remote-module.ts

View workflow job for this annotation

GitHub Actions / main

Unexpected any. Specify a different type
Exports extends object = any

Check warning on line 24 in libs/native-federation-runtime/src/lib/load-remote-module.ts

View workflow job for this annotation

GitHub Actions / main

Unexpected any. Specify a different type
>(
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

0 comments on commit c0b15cb

Please sign in to comment.