Skip to content

Commit 2c9d736

Browse files
committedJan 8, 2025
feat(@angular/build): add ng-packagr builder to the package
To support migration to the `@angular/build` package which contains the `application` builder that is used by all new projects, the `ng-packagr` builder used to build Angular libraries is also now available within this package. This removes the need for projects that are using the application builder but also would like to build a library from having to install the Webpack related `@angular-devkit/build-angular` package. This can result in a significant reduction in overall Node.js packages installed within the project.
1 parent bfe9ee3 commit 2c9d736

File tree

10 files changed

+184
-0
lines changed

10 files changed

+184
-0
lines changed
 

‎goldens/public-api/angular/build/index.api.md

+11
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ export function executeDevServerBuilder(options: DevServerBuilderOptions, contex
149149
// @public
150150
export function executeExtractI18nBuilder(options: ExtractI18nBuilderOptions, context: BuilderContext, extensions?: ApplicationBuilderExtensions): Promise<BuilderOutput>;
151151

152+
// @public
153+
export function executeNgPackagrBuilder(options: NgPackagrBuilderOptions, context: BuilderContext): AsyncIterableIterator<BuilderOutput>;
154+
152155
// @public
153156
export interface ExtractI18nBuilderOptions {
154157
buildTarget?: string;
@@ -158,6 +161,14 @@ export interface ExtractI18nBuilderOptions {
158161
progress?: boolean;
159162
}
160163

164+
// @public
165+
export interface NgPackagrBuilderOptions {
166+
poll?: number;
167+
project: string;
168+
tsConfig?: string;
169+
watch?: boolean;
170+
}
171+
161172
// (No @packageDocumentation comment for this package)
162173

163174
```

‎packages/angular/build/BUILD.bazel

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ ts_json_schema(
2323
src = "src/builders/extract-i18n/schema.json",
2424
)
2525

26+
ts_json_schema(
27+
name = "ng_packagr_schema",
28+
src = "src/builders/ng-packagr/schema.json",
29+
)
30+
2631
ts_project(
2732
name = "build",
2833
srcs = glob(
@@ -40,6 +45,7 @@ ts_project(
4045
"//packages/angular/build:src/builders/application/schema.ts",
4146
"//packages/angular/build:src/builders/dev-server/schema.ts",
4247
"//packages/angular/build:src/builders/extract-i18n/schema.ts",
48+
"//packages/angular/build:src/builders/ng-packagr/schema.ts",
4349
],
4450
data = glob(
4551
include = [
@@ -90,6 +96,7 @@ ts_project(
9096
"//:root_modules/lmdb",
9197
"//:root_modules/magic-string",
9298
"//:root_modules/mrmime",
99+
"//:root_modules/ng-packagr",
93100
"//:root_modules/parse5-html-rewriting-stream",
94101
"//:root_modules/picomatch",
95102
"//:root_modules/piscina",
@@ -186,6 +193,7 @@ ts_project(
186193
"//:root_modules/@angular/platform-browser",
187194
"//:root_modules/@angular/platform-browser-dynamic",
188195
"//:root_modules/@angular/router",
196+
"//:root_modules/ng-packagr",
189197
"//:root_modules/rxjs",
190198
"//:root_modules/tslib",
191199
"//:root_modules/typescript",

‎packages/angular/build/builders.json

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"implementation": "./src/builders/extract-i18n/index",
1515
"schema": "./src/builders/extract-i18n/schema.json",
1616
"description": "Extract i18n messages from an application."
17+
},
18+
"ng-packagr": {
19+
"implementation": "./src/builders/ng-packagr/index",
20+
"schema": "./src/builders/ng-packagr/schema.json",
21+
"description": "Build a library with ng-packagr."
1722
}
1823
}
1924
}

‎packages/angular/build/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@angular/service-worker": "^19.0.0 || ^19.1.0-next.0",
5656
"@angular/ssr": "^0.0.0-PLACEHOLDER",
5757
"less": "^4.2.0",
58+
"ng-packagr": "^19.0.0 || ^19.1.0-next.0",
5859
"postcss": "^8.4.0",
5960
"tailwindcss": "^2.0.0 || ^3.0.0",
6061
"typescript": ">=5.5 <5.8"
@@ -75,6 +76,9 @@
7576
"less": {
7677
"optional": true
7778
},
79+
"ng-packagr": {
80+
"optional": true
81+
},
7882
"postcss": {
7983
"optional": true
8084
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import type { BuilderContext, BuilderOutput } from '@angular-devkit/architect';
10+
import type { NgPackagrOptions } from 'ng-packagr';
11+
import { join, resolve } from 'node:path';
12+
import { assertIsError } from '../../utils/error';
13+
import { normalizeCacheOptions } from '../../utils/normalize-cache';
14+
import { purgeStaleBuildCache } from '../../utils/purge-cache';
15+
import type { Schema as NgPackagrBuilderOptions } from './schema';
16+
17+
/**
18+
* A Builder that executes the `ng-packagr` tool to build an Angular library.
19+
*
20+
* @param options The builder options as defined by the JSON schema.
21+
* @param context A BuilderContext instance.
22+
* @returns A BuilderOutput object.
23+
*
24+
* @experimental Direct usage of this function is considered experimental.
25+
*/
26+
export async function* execute(
27+
options: NgPackagrBuilderOptions,
28+
context: BuilderContext,
29+
): AsyncIterableIterator<BuilderOutput> {
30+
// Purge old build disk cache.
31+
await purgeStaleBuildCache(context);
32+
33+
const root = context.workspaceRoot;
34+
let packager;
35+
try {
36+
packager = (await import('ng-packagr')).ngPackagr();
37+
} catch (error) {
38+
assertIsError(error);
39+
if (error.code === 'MODULE_NOT_FOUND') {
40+
return {
41+
success: false,
42+
error:
43+
'The "ng-packagr" package was not found. To correct this error, ensure this package is installed in the project.',
44+
};
45+
}
46+
47+
throw error;
48+
}
49+
50+
packager.forProject(resolve(root, options.project));
51+
52+
if (options.tsConfig) {
53+
packager.withTsConfig(resolve(root, options.tsConfig));
54+
}
55+
56+
const projectName = context.target?.project;
57+
if (!projectName) {
58+
throw new Error('The builder requires a target.');
59+
}
60+
61+
const metadata = await context.getProjectMetadata(projectName);
62+
const { enabled: cacheEnabled, path: cacheDirectory } = normalizeCacheOptions(
63+
metadata,
64+
context.workspaceRoot,
65+
);
66+
67+
const ngPackagrOptions: NgPackagrOptions = {
68+
cacheEnabled,
69+
poll: options.poll,
70+
cacheDirectory: join(cacheDirectory, 'ng-packagr'),
71+
};
72+
73+
try {
74+
if (options.watch) {
75+
await packager.watch(ngPackagrOptions).toPromise();
76+
} else {
77+
await packager.build(ngPackagrOptions);
78+
}
79+
80+
yield { success: true };
81+
} catch (error) {
82+
assertIsError(error);
83+
84+
yield { success: false, error: error.message };
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { createBuilder } from '@angular-devkit/architect';
10+
import { execute } from './builder';
11+
import type { Schema as NgPackagrBuilderOptions } from './schema';
12+
13+
export { type NgPackagrBuilderOptions, execute };
14+
export default createBuilder<NgPackagrBuilderOptions>(execute);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"title": "ng-packagr Target",
4+
"description": "ng-packagr target options for Build Architect. Use to build library projects.",
5+
"type": "object",
6+
"properties": {
7+
"project": {
8+
"type": "string",
9+
"description": "The file path for the ng-packagr configuration file, relative to the current workspace."
10+
},
11+
"tsConfig": {
12+
"type": "string",
13+
"description": "The full path for the TypeScript configuration file, relative to the current workspace."
14+
},
15+
"watch": {
16+
"type": "boolean",
17+
"description": "Run build when files change.",
18+
"default": false
19+
},
20+
"poll": {
21+
"type": "number",
22+
"description": "Enable and define the file watching poll time period in milliseconds."
23+
}
24+
},
25+
"additionalProperties": false,
26+
"required": ["project"]
27+
}

‎packages/angular/build/src/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ export {
2121
execute as executeExtractI18nBuilder,
2222
type ExtractI18nBuilderOptions,
2323
} from './builders/extract-i18n';
24+
25+
export {
26+
execute as executeNgPackagrBuilder,
27+
type NgPackagrBuilderOptions,
28+
} from './builders/ng-packagr';

‎packages/angular/cli/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ CLI_SCHEMA_DATA = [
7878
"//packages/angular/build:src/builders/application/schema.json",
7979
"//packages/angular/build:src/builders/dev-server/schema.json",
8080
"//packages/angular/build:src/builders/extract-i18n/schema.json",
81+
"//packages/angular/build:src/builders/ng-packagr/schema.json",
8182
"//packages/angular_devkit/build_angular:src/builders/app-shell/schema.json",
8283
"//packages/angular_devkit/build_angular:src/builders/browser/schema.json",
8384
"//packages/angular_devkit/build_angular:src/builders/browser-esbuild/schema.json",

‎packages/angular/cli/lib/config/workspace-schema.json

+23
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@
407407
"@angular/build:application",
408408
"@angular/build:dev-server",
409409
"@angular/build:extract-i18n",
410+
"@angular/build:ng-packagr",
410411
"@angular-devkit/build-angular:application",
411412
"@angular-devkit/build-angular:app-shell",
412413
"@angular-devkit/build-angular:browser",
@@ -792,6 +793,28 @@
792793
}
793794
}
794795
}
796+
},
797+
{
798+
"type": "object",
799+
"additionalProperties": false,
800+
"properties": {
801+
"builder": {
802+
"const": "@angular/build:ng-packagr"
803+
},
804+
"defaultConfiguration": {
805+
"type": "string",
806+
"description": "A default named configuration to use when a target configuration is not provided."
807+
},
808+
"options": {
809+
"$ref": "../../../../angular/build/src/builders/ng-packagr/schema.json"
810+
},
811+
"configurations": {
812+
"type": "object",
813+
"additionalProperties": {
814+
"$ref": "../../../../angular/build/src/builders/ng-packagr/schema.json"
815+
}
816+
}
817+
}
795818
}
796819
]
797820
}

0 commit comments

Comments
 (0)
Please sign in to comment.