/
write-new-webpack-config.ts
59 lines (55 loc) · 1.63 KB
/
write-new-webpack-config.ts
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
import type { SourceFile } from 'typescript';
import {
getExposedModulesFromRemote,
getRemotesFromHost,
IsHostRemoteConfigResult,
} from './is-host-remote-config';
export function writeNewWebpackConfig(
ast: SourceFile,
mfType: IsHostRemoteConfigResult,
projectName: string
) {
const webpackConfig = `const { withModuleFederation } = require('@nrwl/angular/module-federation');
const config = require('./mfe.config');
module.exports = withModuleFederation(config);`;
let mfeConfig = '';
if (!mfType) {
mfeConfig = `
module.exports = {
name: '${projectName}',
};`;
} else if (mfType === 'host') {
const remotes = hostRemotesToString(ast);
mfeConfig = `
module.exports = {
name: '${projectName}',
remotes: ${remotes},
};`;
} else if (mfType === 'remote') {
const exposedModules = getExposedModulesFromRemote(ast);
mfeConfig = `
module.exports = {
name: '${projectName}',
exposes: ${exposedModules},
};`;
} else if (mfType === 'both') {
const remotes = hostRemotesToString(ast);
const exposedModules = getExposedModulesFromRemote(ast);
mfeConfig = `
module.exports = {
name: '${projectName}',
remotes: ${remotes},
exposes: ${exposedModules},
};`;
}
return [webpackConfig, mfeConfig];
}
function hostRemotesToString(ast: SourceFile) {
const remotes: string = getRemotesFromHost(ast)
.reduce(
(acc, remotePair) => `['${remotePair[0]}', '${remotePair[1]}'], ${acc}`,
''
)
.trim();
return `[${remotes.endsWith(',') ? remotes.slice(0, -1) : remotes}]`;
}