Skip to content

Commit be2a707

Browse files
committed
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 9b57ff0 commit be2a707

File tree

8 files changed

+159
-0
lines changed

8 files changed

+159
-0
lines changed

packages/angular/build/BUILD.bazel

Lines changed: 8 additions & 0 deletions
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",
@@ -156,6 +163,7 @@ ts_project(
156163
"//:root_modules/@angular/platform-browser",
157164
"//:root_modules/@angular/platform-browser-dynamic",
158165
"//:root_modules/@angular/router",
166+
"//:root_modules/ng-packagr",
159167
"//:root_modules/rxjs",
160168
"//:root_modules/tslib",
161169
"//:root_modules/typescript",

packages/angular/build/builders.json

Lines changed: 5 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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
},
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
export async function execute(
18+
options: NgPackagrBuilderOptions,
19+
context: BuilderContext,
20+
): Promise<BuilderOutput> {
21+
// Purge old build disk cache.
22+
await purgeStaleBuildCache(context);
23+
24+
const root = context.workspaceRoot;
25+
let packager;
26+
try {
27+
packager = (await import('ng-packagr')).ngPackagr();
28+
} catch (error) {
29+
assertIsError(error);
30+
if (error.code === 'MODULE_NOT_FOUND') {
31+
return {
32+
success: false,
33+
error:
34+
'The "ng-packagr" package was not found. To correct this error, ensure this package is installed in the project.',
35+
};
36+
}
37+
38+
throw error;
39+
}
40+
41+
packager.forProject(resolve(root, options.project));
42+
43+
if (options.tsConfig) {
44+
packager.withTsConfig(resolve(root, options.tsConfig));
45+
}
46+
47+
const projectName = context.target?.project;
48+
if (!projectName) {
49+
throw new Error('The builder requires a target.');
50+
}
51+
52+
const metadata = await context.getProjectMetadata(projectName);
53+
const { enabled: cacheEnabled, path: cacheDirectory } = normalizeCacheOptions(
54+
metadata,
55+
context.workspaceRoot,
56+
);
57+
58+
const ngPackagrOptions: NgPackagrOptions = {
59+
cacheEnabled,
60+
poll: options.poll,
61+
cacheDirectory: join(cacheDirectory, 'ng-packagr'),
62+
};
63+
64+
try {
65+
if (options.watch) {
66+
await packager.watch(ngPackagrOptions).toPromise();
67+
} else {
68+
await packager.build(ngPackagrOptions);
69+
}
70+
71+
return { success: true };
72+
} catch (error) {
73+
assertIsError(error);
74+
75+
return { success: false, error: error.message };
76+
}
77+
}
Lines changed: 14 additions & 0 deletions
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 };
14+
export default createBuilder<NgPackagrBuilderOptions>(execute);
Lines changed: 27 additions & 0 deletions
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/cli/BUILD.bazel

Lines changed: 1 addition & 0 deletions
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

Lines changed: 23 additions & 0 deletions
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)