Skip to content
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

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion goldens/public-api/angular/build/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export enum BuildOutputFileType {

// @public
export interface DevServerBuilderOptions {
buildTarget: string;
buildTarget?: string;
headers?: {
[key: string]: string;
};
Expand Down
6 changes: 4 additions & 2 deletions packages/angular/build/src/builders/dev-server/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/build/src/builders/dev-server/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,5 @@
}
},
"additionalProperties": false,
"required": ["buildTarget"]
"required": []
}
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 {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,5 @@
]
}
},
"additionalProperties": false,
"anyOf": [{ "required": ["buildTarget"] }, { "required": ["browserTarget"] }]
"additionalProperties": false
}
Loading