Skip to content

Commit e7318fc

Browse files
crisbetothePunderWoman
authored andcommitted
feat(core): add ng generate schematic to remove unnecessary modules (#48832)
Adds a new mode to the `@angular/core:standalone` schematic that automatically deletes modules that may not be necessary after the previous step of converting them to standalone. PR Close #48832
1 parent 1063404 commit e7318fc

File tree

4 files changed

+922
-2
lines changed

4 files changed

+922
-2
lines changed

packages/core/schematics/ng-generate/standalone-migration/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import ts from 'typescript';
1515
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths';
1616
import {canMigrateFile, createProgramOptions} from '../../utils/typescript/compiler_host';
1717

18+
import {pruneNgModules} from './prune-modules';
1819
import {toStandalone} from './to-standalone';
1920
import {ChangesByFile} from './util';
2021

2122
enum MigrationMode {
2223
toStandalone = 'convert-to-standalone',
24+
pruneModules = 'prune-ng-modules',
2325
}
2426

2527
interface Options {
@@ -75,15 +77,25 @@ function standaloneMigration(tree: Tree, tsconfigPath: string, basePath: string,
7577
}
7678

7779
let pendingChanges: ChangesByFile;
80+
let filesToRemove: Set<ts.SourceFile>|null = null;
7881

7982
if (options.mode === MigrationMode.toStandalone) {
8083
pendingChanges = toStandalone(sourceFiles, program, printer);
84+
} else if (options.mode === MigrationMode.pruneModules) {
85+
const result = pruneNgModules(program, host, basePath, rootNames, sourceFiles, printer);
86+
pendingChanges = result.pendingChanges;
87+
filesToRemove = result.filesToRemove;
8188
} else {
8289
throw new SchematicsException(
8390
`Unknown schematic mode ${options.mode}. Cannot run the standalone migration.`);
8491
}
8592

8693
for (const [file, changes] of pendingChanges.entries()) {
94+
// Don't attempt to edit a file if it's going to be deleted.
95+
if (filesToRemove?.has(file)) {
96+
continue;
97+
}
98+
8799
const update = tree.beginUpdate(relative(basePath, file.fileName));
88100

89101
changes.forEach(change => {
@@ -95,4 +107,10 @@ function standaloneMigration(tree: Tree, tsconfigPath: string, basePath: string,
95107

96108
tree.commitUpdate(update);
97109
}
110+
111+
if (filesToRemove) {
112+
for (const file of filesToRemove) {
113+
tree.delete(relative(basePath, file.fileName));
114+
}
115+
}
98116
}

0 commit comments

Comments
 (0)