/
convert-to-with-mf.ts
55 lines (46 loc) · 2.22 KB
/
convert-to-with-mf.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
import { joinPathFragments, logger, Tree } from '@nrwl/devkit';
import type { Schema } from './schema';
import { readProjectConfiguration, formatFiles } from '@nrwl/devkit';
import { getMfeProjects } from '../../utils/get-mfe-projects';
import {
checkOutputNameMatchesProjectName,
checkSharedNpmPackagesMatchExpected,
getWebpackConfigPath,
isHostRemoteConfig,
parseASTOfWebpackConfig,
writeNewWebpackConfig,
} from './lib';
export default async function convertToWithMF(tree: Tree, schema: Schema) {
const projects = new Set(getMfeProjects(tree, { legacy: true }));
if (!projects.has(schema.project)) {
throw new Error(
`Could not find project "${schema.project}" with a Micro Frontend configuration in your workspace. Please check the name of the project you're wishing to convert exists.`
);
}
const project = readProjectConfiguration(tree, schema.project);
const pathToWebpackConfig = getWebpackConfigPath(project, schema.project);
const webpackAst = parseASTOfWebpackConfig(tree, pathToWebpackConfig);
if (!checkOutputNameMatchesProjectName(webpackAst, schema.project)) {
throw new Error(
`Cannot automatically migrate "${schema.project}" to "withModuleFederation" micro frontend webpack config.
"uniqueName" in webpack config (${pathToWebpackConfig}) does not match project name.`
);
}
if (!checkSharedNpmPackagesMatchExpected(webpackAst)) {
throw new Error(
`Cannot automatically migrate "${schema.project}" to "withModuleFederation" micro frontend webpack config.
There are npm packages being shared with a custom configuration in webpack config (${pathToWebpackConfig}).`
);
}
logger.warn(
`This Micro Frontend configuration conversion will overwrite "${schema.project}"'s current webpack config. If you have anything custom that is not related to Micro Frontends, it will be lost. You should be able to see the changes in your version control system.`
);
const [updatedWebpackConfig, mfeConfig] = writeNewWebpackConfig(
webpackAst,
isHostRemoteConfig(webpackAst),
schema.project
);
tree.write(pathToWebpackConfig, updatedWebpackConfig);
tree.write(joinPathFragments(project.root, 'mfe.config.js'), mfeConfig);
await formatFiles(tree);
}