/
mfe-remote.ts
53 lines (43 loc) · 1.57 KB
/
mfe-remote.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
import { join } from 'path';
import { formatFiles, generateFiles, names, Tree } from '@nrwl/devkit';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
import { normalizeOptions } from '../application/lib/normalize-options';
import applicationGenerator from '../application/application';
import { NormalizedSchema } from '../application/schema';
import { updateHostWithRemote } from './lib/update-host-with-remote';
import { updateMfeProject } from '../../rules/update-mfe-project';
import { Schema } from './schema';
export function addMfeFiles(host: Tree, options: NormalizedSchema) {
const templateVariables = {
...names(options.name),
...options,
tmpl: '',
};
generateFiles(
host,
join(__dirname, `./files/mfe`),
options.appProjectRoot,
templateVariables
);
}
export async function mfeRemoteGenerator(host: Tree, schema: Schema) {
const options = normalizeOptions(host, schema);
const initApp = await applicationGenerator(host, options);
if (schema.host) {
updateHostWithRemote(host, schema.host, options.name);
}
// Module federation requires bootstrap code to be dynamically imported.
// Renaming original entry file so we can use `import(./bootstrap)` in
// new entry file.
host.rename(
join(options.appProjectRoot, 'src/main.tsx'),
join(options.appProjectRoot, 'src/bootstrap.tsx')
);
addMfeFiles(host, options);
updateMfeProject(host, options);
if (!options.skipFormat) {
await formatFiles(host);
}
return runTasksInSerial(initApp);
}
export default mfeRemoteGenerator;