Skip to content

Commit 094e85c

Browse files
committed
feat(@schematics/angular): allow application migration to use new build package in projects where possible
When using the optional application build system migration, the newly introduced `@angular/build` package which contains only the new build system and no Webpack-related dependencies will be directly used when possible. The migration will check for usage of any other builders from the `@angular-devkit/build-angular` package. if none are present in the `angular.json` file (excluding `dev-server` and `extract-i18n`), the `@angular/build` package will be added as a dependency and used in the `angular.json` file. The `@angular-devkit/build-angular` package will then be removed as a dependency. Project usage of karma and/or protractor will be the most common reasons this part of the migration will not be performed.
1 parent 6e5e232 commit 094e85c

File tree

1 file changed

+59
-1
lines changed
  • packages/schematics/angular/migrations/use-application-builder

1 file changed

+59
-1
lines changed

packages/schematics/angular/migrations/use-application-builder/migration.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,21 @@ import {
1616
externalSchematic,
1717
} from '@angular-devkit/schematics';
1818
import { basename, dirname, extname, join } from 'node:path/posix';
19+
import { removePackageJsonDependency } from '../../utility/dependencies';
20+
import {
21+
DependencyType,
22+
ExistingBehavior,
23+
InstallBehavior,
24+
addDependency,
25+
} from '../../utility/dependency';
1926
import { JSONFile } from '../../utility/json-file';
20-
import { TargetDefinition, allTargetOptions, updateWorkspace } from '../../utility/workspace';
27+
import { latestVersions } from '../../utility/latest-versions';
28+
import {
29+
TargetDefinition,
30+
allTargetOptions,
31+
allWorkspaceTargets,
32+
updateWorkspace,
33+
} from '../../utility/workspace';
2134
import { Builders, ProjectType } from '../../utility/workspace-models';
2235
import { findImports } from './css-import-lexer';
2336

@@ -194,6 +207,51 @@ function updateProjects(tree: Tree, context: SchematicContext) {
194207
updateStyleImports(tree, projectSourceRoot, buildTarget);
195208
}
196209

210+
// Check for @angular-devkit/build-angular Webpack usage
211+
let hasAngularDevkitUsage = false;
212+
for (const [, target] of allWorkspaceTargets(workspace)) {
213+
switch (target.builder) {
214+
case Builders.Application:
215+
case Builders.DevServer:
216+
case Builders.ExtractI18n:
217+
// Ignore application, dev server, and i18n extraction for devkit usage check.
218+
// Both will be replaced if no other usage is found.
219+
continue;
220+
}
221+
222+
if (target.builder.startsWith('@angular-devkit/build-angular:')) {
223+
hasAngularDevkitUsage = true;
224+
break;
225+
}
226+
}
227+
228+
// Use @angular/build directly if there is no devkit package usage
229+
if (!hasAngularDevkitUsage) {
230+
for (const [, target] of allWorkspaceTargets(workspace)) {
231+
switch (target.builder) {
232+
case Builders.Application:
233+
target.builder = '@angular/build:application';
234+
break;
235+
case Builders.DevServer:
236+
target.builder = '@angular/build:dev-server';
237+
break;
238+
case Builders.ExtractI18n:
239+
target.builder = '@angular/build:extract-i18n';
240+
break;
241+
}
242+
}
243+
244+
// Add direct @angular/build dependencies and remove @angular-devkit/build-angular
245+
rules.push(
246+
addDependency('@angular/build', latestVersions.DevkitBuildAngular, {
247+
type: DependencyType.Dev,
248+
install: InstallBehavior.Always,
249+
existing: ExistingBehavior.Replace,
250+
}),
251+
);
252+
removePackageJsonDependency(tree, '@angular-devkit/build-angular');
253+
}
254+
197255
return chain(rules);
198256
});
199257
}

0 commit comments

Comments
 (0)