-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathgetFederationGlobal.ts
More file actions
61 lines (57 loc) · 2.18 KB
/
getFederationGlobal.ts
File metadata and controls
61 lines (57 loc) · 2.18 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
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
import { getFederationGlobalScope } from './utils';
import type RuntimeGlobals from 'webpack/lib/RuntimeGlobals';
import { NormalizedRuntimeInitOptionsWithOutShared } from '../../../types/runtime';
import type { RemoteInfos } from '@module-federation/webpack-bundler-runtime';
const { Template } = require(
normalizeWebpackPath('webpack'),
) as typeof import('webpack');
function getFederationGlobal(
template: typeof Template,
runtimeGlobals: typeof RuntimeGlobals,
matcher: string | boolean,
rootOutputDir: string | undefined,
initOptionsWithoutShared: NormalizedRuntimeInitOptionsWithOutShared,
): string {
const federationGlobal = getFederationGlobalScope(runtimeGlobals);
const initOptionsStrWithoutShared = JSON.stringify({
...initOptionsWithoutShared,
remotes: initOptionsWithoutShared.remotes.filter(
(remote) => remote.externalType === 'script',
),
});
const remoteInfos = JSON.stringify(
initOptionsWithoutShared.remotes.reduce((acc, remote) => {
const item: RemoteInfos[string][0] = {
alias: remote.alias || '',
name: remote.name,
// @ts-ignore
entry: remote.entry || '',
// @ts-ignore
shareScope: remote.shareScope,
externalType: remote.externalType,
};
const key = remote.name || remote.alias || '';
acc[key] ||= [];
acc[key].push(item);
return acc;
}, {} as RemoteInfos),
);
return template.asString([
`if(!${federationGlobal}){`,
template.indent([
`${federationGlobal} = {`,
template.indent([
`initOptions: ${initOptionsStrWithoutShared},`,
`chunkMatcher: function(chunkId) {return ${matcher}},`,
`rootOutputDir: ${JSON.stringify(rootOutputDir || '')},`,
`bundlerRuntimeOptions: { remotes: { remoteInfos: ${remoteInfos}, webpackRequire: ${runtimeGlobals.require},idToRemoteMap: {}, chunkMapping: {},idToExternalAndNameMapping: {} } }`,
]),
'};',
]),
`${runtimeGlobals.require}.consumesLoadingData = {}`,
`${runtimeGlobals.require}.remotesLoadingData = {}`,
'}',
]);
}
export default getFederationGlobal;