-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathinit.ts
More file actions
130 lines (120 loc) · 4.44 KB
/
init.ts
File metadata and controls
130 lines (120 loc) · 4.44 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import type { WebpackRequire } from './types';
import {
getRemoteEntry,
type ModuleFederationRuntimePlugin,
} from '@module-federation/runtime';
import { ShareArgs } from '@module-federation/runtime/types';
import helpers from '@module-federation/runtime/helpers';
export function init({ webpackRequire }: { webpackRequire: WebpackRequire }) {
const { initOptions, runtime, sharedFallback, bundlerRuntime, libraryType } =
webpackRequire.federation;
if (!initOptions) {
throw new Error('initOptions is required!');
}
const treeShakingSharePlugin: () => ModuleFederationRuntimePlugin =
function () {
return {
name: 'tree-shake-plugin',
beforeInit(args) {
const { userOptions, origin, options: registeredOptions } = args;
const version = userOptions.version || registeredOptions.version;
if (!sharedFallback) {
return args;
}
const currentShared = userOptions.shared || {};
const shared: Array<[pkgName: string, ShareArgs]> = [];
Object.keys(currentShared).forEach((sharedName) => {
const sharedArgs = Array.isArray(currentShared[sharedName])
? currentShared[sharedName]
: [currentShared[sharedName]];
sharedArgs.forEach((sharedArg) => {
shared.push([sharedName, sharedArg]);
if ('get' in sharedArg) {
sharedArg.treeShaking ||= {};
sharedArg.treeShaking.get = sharedArg.get;
sharedArg.get = bundlerRuntime!.getSharedFallbackGetter({
shareKey: sharedName,
factory: sharedArg.get,
webpackRequire,
libraryType,
version: sharedArg.version,
});
}
});
});
// read snapshot to override re-shake getter
const hostGlobalSnapshot =
helpers.global.getGlobalSnapshotInfoByModuleInfo({
name: origin.name,
version: version,
});
if (!hostGlobalSnapshot || !('shared' in hostGlobalSnapshot)) {
return args;
}
Object.keys(registeredOptions.shared || {}).forEach((pkgName) => {
const sharedInfo = registeredOptions.shared[pkgName];
sharedInfo.forEach((sharedArg) => {
shared.push([pkgName, sharedArg]);
});
});
const patchShared = (pkgName: string, shared: ShareArgs) => {
const shareSnapshot = hostGlobalSnapshot.shared.find(
(item) => item.sharedName === pkgName,
);
if (!shareSnapshot) {
return;
}
const { treeShaking } = shared;
if (!treeShaking) {
return;
}
const {
secondarySharedTreeShakingName,
secondarySharedTreeShakingEntry,
treeShakingStatus,
} = shareSnapshot;
if (treeShaking.status === treeShakingStatus) {
return;
}
treeShaking.status = treeShakingStatus;
if (
secondarySharedTreeShakingEntry &&
libraryType &&
secondarySharedTreeShakingName
) {
treeShaking.get = async () => {
const shareEntry = await getRemoteEntry({
origin,
remoteInfo: {
name: secondarySharedTreeShakingName,
entry: secondarySharedTreeShakingEntry,
type: libraryType,
entryGlobalName: secondarySharedTreeShakingName,
// current not used
shareScope: 'default',
},
});
// TODO: add errorLoad hook ?
// @ts-ignore
await shareEntry.init(
origin,
// @ts-ignore
webpackRequire.federation.bundlerRuntime,
);
// @ts-ignore
const getter = shareEntry.get();
return getter;
};
}
};
shared.forEach(([pkgName, sharedArg]) => {
patchShared(pkgName, sharedArg);
});
return args;
},
};
};
initOptions.plugins ||= [];
initOptions.plugins.push(treeShakingSharePlugin());
return runtime!.init(initOptions);
}