From ec19736f7381a194eb3c25d62c765d4987aab515 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:56:42 -0400 Subject: [PATCH 1/3] refactor(@angular/cli): provide default serve target for applications The `serve` command will now use a default project target and builder name if the target entry is not explicitly defined. This allows the removal of additional configuration from an `angular.json` file. If the target is already present than it will take priority over any default builder behavior. The default logic will use the appropriate development server builder for officially supported packages (`@angular/build`/`@angular-devkit/build-angular`). The `dev-server` builder from these packages will currently assume a `development` configuration is present within the `build` target. The default behavior may be expanded in the future to support more arbitrary `build` target configurations. If there is third-party package usage within the `build` target, the CLI will attempt to discover a `dev-server` builder within the same package used by the `build` target. If none is found, no default will be added and the `serve` command will issue an error when no explicit target is present. --- .../angular/cli/src/commands/serve/cli.ts | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/angular/cli/src/commands/serve/cli.ts b/packages/angular/cli/src/commands/serve/cli.ts index 3b38fa122acd..9324487946e8 100644 --- a/packages/angular/cli/src/commands/serve/cli.ts +++ b/packages/angular/cli/src/commands/serve/cli.ts @@ -6,6 +6,7 @@ * found in the LICENSE file at https://angular.dev/license */ +import { workspaces } from '@angular-devkit/core'; import { ArchitectCommandModule } from '../../command-builder/architect-command-module'; import { CommandModuleImplementation } from '../../command-builder/command-module'; import { RootCommands } from '../command-config'; @@ -19,4 +20,41 @@ export default class ServeCommandModule aliases = RootCommands['serve'].aliases; describe = 'Builds and serves your application, rebuilding on file changes.'; longDescriptionPath?: string | undefined; + + override async findDefaultBuilderName( + project: workspaces.ProjectDefinition, + ): Promise { + // Only application type projects have a dev server target + if (project.extensions['projectType'] !== 'application') { + return; + } + + const buildTarget = project.targets.get('build'); + if (!buildTarget) { + // No default if there is no build target + return; + } + + // Provide a default based on the defined builder for the 'build' target + switch (buildTarget.builder) { + case '@angular-devkit/build-angular:application': + case '@angular-devkit/build-angular:browser-esbuild': + case '@angular-devkit/build-angular:browser': + return '@angular-devkit/build-angular:dev-server'; + case '@angular/build:application': + return '@angular/build:dev-server'; + } + + // For other builders, attempt to resolve a 'dev-server' builder from the 'build' target package name + const [buildPackageName] = buildTarget.builder.split(':', 1); + if (buildPackageName) { + try { + const qualifiedBuilderName = `${buildPackageName}:dev-server`; + await this.getArchitectHost().resolveBuilder(qualifiedBuilderName); + + // Use builder if it resolves successfully + return qualifiedBuilderName; + } catch {} + } + } } From aa848cf1c7fc4cb500391ece03615ab445d0e960 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Tue, 9 Jul 2024 12:43:50 -0400 Subject: [PATCH 2/3] refactor(@angular/build): allow development server `buildTarget` option to be optional The development server (`dev-server`) within `@angular/build` no longer requires a `buildTarget` option to be specified. If not present, the value will default to the current project's 'build' target. The configuration used for the build target will be the same as the development server configuration if specified or, if not specified, it will default to `development`. --- goldens/public-api/angular/build/index.api.md | 2 +- packages/angular/build/src/builders/dev-server/options.ts | 6 ++++-- packages/angular/build/src/builders/dev-server/schema.json | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/goldens/public-api/angular/build/index.api.md b/goldens/public-api/angular/build/index.api.md index 79240b301679..482e152629ad 100644 --- a/goldens/public-api/angular/build/index.api.md +++ b/goldens/public-api/angular/build/index.api.md @@ -109,7 +109,7 @@ export enum BuildOutputFileType { // @public export type DevServerBuilderOptions = { allowedHosts?: AllowedHosts; - buildTarget: string; + buildTarget?: string; headers?: { [key: string]: string; }; diff --git a/packages/angular/build/src/builders/dev-server/options.ts b/packages/angular/build/src/builders/dev-server/options.ts index 3e0f59319117..0f953d036fc0 100644 --- a/packages/angular/build/src/builders/dev-server/options.ts +++ b/packages/angular/build/src/builders/dev-server/options.ts @@ -36,8 +36,10 @@ export async function normalizeOptions( const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot); - // Target specifier defaults to the current project's build target using a development configuration - const buildTargetSpecifier = options.buildTarget ?? `::development`; + // Target specifier defaults to the current project's build target using the provided dev-server + // configuration if a configuration is present or the 'development' configuration if not. + const buildTargetSpecifier = + options.buildTarget ?? `::${context.target?.configuration || 'development'}`; const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build'); // Get the application builder options. diff --git a/packages/angular/build/src/builders/dev-server/schema.json b/packages/angular/build/src/builders/dev-server/schema.json index 41902e43d8d0..09d30c2ca84e 100644 --- a/packages/angular/build/src/builders/dev-server/schema.json +++ b/packages/angular/build/src/builders/dev-server/schema.json @@ -127,5 +127,5 @@ } }, "additionalProperties": false, - "required": ["buildTarget"] + "required": [] } From 7c488704a3befa4992cba84ac6ed0ec8f25e30f3 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:37:55 -0400 Subject: [PATCH 3/3] refactor(@angular-devkit/build-angular): allow development server `buildTarget` option to be optional The development server (`dev-server`) within `@angular-devkit/build-angular` no longer requires a `buildTarget` option to be specified. If not present, the value will default to the current project's 'build' target. The configuration used for the build target will be the same as the development server configuration if specified or, if not specified, it will default to `development`. --- .../build_angular/src/builders/dev-server/options.ts | 6 ++++-- .../build_angular/src/builders/dev-server/schema.json | 3 +-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/options.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/options.ts index 6fa23123a236..88844b3e51eb 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/options.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/options.ts @@ -36,8 +36,10 @@ export async function normalizeOptions( const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot); - // Target specifier defaults to the current project's build target using a development configuration - const buildTargetSpecifier = options.buildTarget ?? `::development`; + // Target specifier defaults to the current project's build target using the provided dev-server + // configuration if a configuration is present or the 'development' configuration if not. + const buildTargetSpecifier = + options.buildTarget ?? `::${context.target?.configuration || 'development'}`; const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build'); // Get the application builder options. diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/schema.json b/packages/angular_devkit/build_angular/src/builders/dev-server/schema.json index 495f244b1722..38d91d5dd2d1 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/schema.json +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/schema.json @@ -130,6 +130,5 @@ ] } }, - "additionalProperties": false, - "required": ["buildTarget"] + "additionalProperties": false }