Skip to content

Commit 8f47f1e

Browse files
clydindgp1130
authored andcommitted
feat(@angular-devkit/build-angular): provide default and abbreviated build target support for dev-server and extract-i18n
The `buildTarget` options for both the `dev-server` and `extract-i8n` builders now have default values that reflect the recommended and new project generated values. The defaults are as follows where `<current-project>` is the name of the project where the `dev-server` or `extract-i18n` builder target is located: * `dev-server` --> `<current-project>:build:development` * `extract-i18n` --> `<current-project>:build` Additionally, abbreviated target specifiers are now supported for these options. This allows target specifiers such as `::production` which would expand to `<current-project>:build:production` for either builder. Abbreviated target specifiers are only supported for the `buildTarget` option in the `dev-server` and `extract-i18n` builders.
1 parent 6e69123 commit 8f47f1e

File tree

6 files changed

+20
-13
lines changed

6 files changed

+20
-13
lines changed

goldens/public-api/angular_devkit/architect/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ namespace strategy {
503503
export type Target = json.JsonObject & Target_2;
504504

505505
// @public
506-
export function targetFromTargetString(str: string): Target;
506+
export function targetFromTargetString(specifier: string, abbreviatedProjectName?: string, abbreviatedTargetName?: string): Target;
507507

508508
// @public
509509
export function targetStringFromTarget({ project, target, configuration }: Target): string;

packages/angular_devkit/architect/src/api.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,22 @@ export function targetStringFromTarget({ project, target, configuration }: Targe
330330
}
331331

332332
/**
333-
* Return a Target tuple from a string.
333+
* Return a Target tuple from a specifier string.
334+
* Supports abbreviated target specifiers (examples: `::`, `::development`, or `:build:production`).
334335
*/
335-
export function targetFromTargetString(str: string): Target {
336-
const tuple = str.split(/:/, 3);
336+
export function targetFromTargetString(
337+
specifier: string,
338+
abbreviatedProjectName?: string,
339+
abbreviatedTargetName?: string,
340+
): Target {
341+
const tuple = specifier.split(':', 3);
337342
if (tuple.length < 2) {
338-
throw new Error('Invalid target string: ' + JSON.stringify(str));
343+
throw new Error('Invalid target string: ' + JSON.stringify(specifier));
339344
}
340345

341346
return {
342-
project: tuple[0],
343-
target: tuple[1],
347+
project: tuple[0] || abbreviatedProjectName || '',
348+
target: tuple[1] || abbreviatedTargetName || '',
344349
...(tuple[2] !== undefined && { configuration: tuple[2] }),
345350
};
346351
}

packages/angular_devkit/build_angular/src/builders/dev-server/options.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ export async function normalizeOptions(
3434

3535
const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot);
3636

37-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
38-
const buildTarget = targetFromTargetString(options.buildTarget ?? options.browserTarget!);
37+
// Target specifier defaults to the current project's build target using a development configuration
38+
const buildTargetSpecifier = options.buildTarget ?? options.browserTarget ?? `::development`;
39+
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');
3940

4041
// Initial options to keep
4142
const {

packages/angular_devkit/build_angular/src/builders/dev-server/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"buildTarget": {
1414
"type": "string",
1515
"description": "A build builder target to serve in the format of `project:target[:configuration]`. You can also pass in more than one configuration name as a comma-separated list. Example: `project:target:production,staging`.",
16-
"pattern": "^[^:\\s]+:[^:\\s]+(:[^\\s]+)?$"
16+
"pattern": "^[^:\\s]*:[^:\\s]*(:[^\\s]+)?$"
1717
},
1818
"port": {
1919
"type": "number",

packages/angular_devkit/build_angular/src/builders/extract-i18n/options.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ export async function normalizeOptions(
3333
const projectMetadata = await context.getProjectMetadata(projectName);
3434
const projectRoot = path.join(workspaceRoot, (projectMetadata.root as string | undefined) ?? '');
3535

36-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
37-
const buildTarget = targetFromTargetString(options.buildTarget ?? options.browserTarget!);
36+
// Target specifier defaults to the current project's build target with no specified configuration
37+
const buildTargetSpecifier = options.buildTarget ?? options.browserTarget ?? ':';
38+
const buildTarget = targetFromTargetString(buildTargetSpecifier, projectName, 'build');
3839

3940
const i18nOptions = createI18nOptions(projectMetadata);
4041

packages/angular_devkit/build_angular/src/builders/extract-i18n/schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"buildTarget": {
1414
"type": "string",
1515
"description": "A builder target to extract i18n messages in the format of `project:target[:configuration]`. You can also pass in more than one configuration name as a comma-separated list. Example: `project:target:production,staging`.",
16-
"pattern": "^[^:\\s]+:[^:\\s]+(:[^\\s]+)?$"
16+
"pattern": "^[^:\\s]*:[^:\\s]*(:[^\\s]+)?$"
1717
},
1818
"format": {
1919
"type": "string",

0 commit comments

Comments
 (0)