From b55c5458fea712973663bb0faeaa21d19104543c 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 7701ee62dad939655e26d7dd935c9122c1ce0b07 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 c3efe5dda318..dcd654fbca46 100644 --- a/goldens/public-api/angular/build/index.api.md +++ b/goldens/public-api/angular/build/index.api.md @@ -110,7 +110,7 @@ export enum BuildOutputFileType { // @public export interface DevServerBuilderOptions { - 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 080e168699bc..e342416e0695 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 3adce45eb71a..8bc0fc4dc4ff 100644 --- a/packages/angular/build/src/builders/dev-server/schema.json +++ b/packages/angular/build/src/builders/dev-server/schema.json @@ -111,5 +111,5 @@ } }, "additionalProperties": false, - "required": ["buildTarget"] + "required": [] } From c677e2641403ed02bd15073faccfbcd4e39f2831 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 f8c7d8a0566b..6e6c6206ffe9 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 ?? options.browserTarget ?? `::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 5796dd04e895..ab98263bde46 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 @@ -137,6 +137,5 @@ ] } }, - "additionalProperties": false, - "anyOf": [{ "required": ["buildTarget"] }, { "required": ["browserTarget"] }] + "additionalProperties": false }