/
mfe-remote.ts
59 lines (49 loc) · 1.63 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
54
55
56
57
58
59
import { join } from 'path';
import {
formatFiles,
generateFiles,
getProjects,
names,
Tree,
} from '@nrwl/devkit';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
import { Schema } from './schema';
import { normalizeOptions } from '../application/lib/normalize-options';
import applicationGenerator from '../application/application';
import { updateMfeProject } from '../mfe-host/lib/update-mfe-project';
import { NormalizedSchema } from '../application/schema';
import { updateHostWithRemote } from '@nrwl/react/src/generators/mfe-host/lib/update-host-with-remote';
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;