-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathResolverPlugin.ts
More file actions
55 lines (48 loc) · 1.69 KB
/
Copy pathResolverPlugin.ts
File metadata and controls
55 lines (48 loc) · 1.69 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
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
import type * as RepackClient from '../ScriptManager';
export type RepackResolverPluginConfiguration =
| Omit<RepackClient.ScriptLocator, 'url'>
| ((url: string) => Promise<RepackClient.ScriptLocator>);
const createScriptLocator = async (
entryUrl: string,
config?: RepackResolverPluginConfiguration
) => {
if (typeof config === 'function') {
const locator = await config(entryUrl);
return locator;
}
if (typeof config === 'object') {
return { url: entryUrl, ...config };
}
return { url: entryUrl };
};
const RepackResolverPlugin: (
config?: RepackResolverPluginConfiguration
) => FederationRuntimePlugin = (config) => ({
name: 'repack-resolver-plugin',
afterResolve(args) {
const { ScriptManager } =
require('../ScriptManager') as typeof RepackClient;
const { remoteInfo } = args;
ScriptManager.shared.addResolver(
async (scriptId, caller, referenceUrl) => {
// entry container
if (scriptId === remoteInfo.entryGlobalName) {
const locator = await createScriptLocator(remoteInfo.entry, config);
return locator;
}
// entry chunks
if (referenceUrl && caller === remoteInfo.entryGlobalName) {
const publicPath = remoteInfo.entry.split('/').slice(0, -1).join('/');
const bundlePath = scriptId + referenceUrl.split(scriptId)[1];
const url = publicPath + '/' + bundlePath;
const locator = await createScriptLocator(url, config);
return locator;
}
},
{ key: remoteInfo.entryGlobalName }
);
return args;
},
});
export default RepackResolverPlugin;