-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathload-remote-module.ts
More file actions
92 lines (81 loc) · 2.52 KB
/
Copy pathload-remote-module.ts
File metadata and controls
92 lines (81 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { processRemoteInfo } from './init-federation';
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<
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<{ default: Default } & Exports>;
export async function loadRemoteModule<
Default = any,
Exports extends object = any
>(
optionsOrRemoteName: LoadRemoteModuleOptions | string,
exposedModule?: string
): Promise<{ default: Default } & Exports> {
const options = normalizeOptions(optionsOrRemoteName, exposedModule);
await ensureRemoteInitialized(options);
const remoteName = getRemoteNameByOptions(options);
const module = await importShim<Default, Exports>(
`${remoteName}/${options.exposedModule}`
);
return module;
}
function getRemoteNameByOptions(options: LoadRemoteModuleOptions) {
let remoteName;
if (options.remoteName) {
remoteName = options.remoteName;
} else if (options.remoteEntry) {
const baseUrl = getDirectory(options.remoteEntry);
remoteName = getRemoteNameByBaseUrl(baseUrl);
} else {
throw new Error(
'unexpcted arguments: Please pass remoteName or remoteEntry'
);
}
if (!remoteName) {
throw new Error('unknown remoteName ' + remoteName);
}
return remoteName;
}
async function ensureRemoteInitialized(
options: LoadRemoteModuleOptions
): Promise<void> {
if (
options.remoteEntry &&
!isRemoteInitialized(getDirectory(options.remoteEntry))
) {
const importMap = await processRemoteInfo(options.remoteEntry);
importShim.addImportMap(importMap);
}
}
function normalizeOptions(
optionsOrRemoteName: string | LoadRemoteModuleOptions,
exposedModule: string | undefined
): LoadRemoteModuleOptions {
let options: LoadRemoteModuleOptions;
if (typeof optionsOrRemoteName === 'string' && exposedModule) {
options = {
remoteName: optionsOrRemoteName,
exposedModule,
};
} else if (typeof optionsOrRemoteName === 'object' && !exposedModule) {
options = optionsOrRemoteName;
} else {
throw new Error(
'unexpected arguments: please pass options or a remoteName/exposedModule-pair'
);
}
return options;
}