Skip to content

refactor(@angular/cli): provide default serve target for applications #28011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
clydin committed Mar 19, 2025
commit ec19736f7381a194eb3c25d62c765d4987aab515
38 changes: 38 additions & 0 deletions packages/angular/cli/src/commands/serve/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string | undefined> {
// 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 {}
}
}
}