-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
10 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import type { BuilderContext, BuilderOutput } from '@angular-devkit/architect'; | ||
import type { NgPackagrOptions } from 'ng-packagr'; | ||
import { join, resolve } from 'node:path'; | ||
import { assertIsError } from '../../utils/error'; | ||
import { normalizeCacheOptions } from '../../utils/normalize-cache'; | ||
import { purgeStaleBuildCache } from '../../utils/purge-cache'; | ||
import type { Schema as NgPackagrBuilderOptions } from './schema'; | ||
|
||
/** | ||
* A Builder that executes the `ng-packagr` tool to build an Angular library. | ||
* | ||
* @param options The builder options as defined by the JSON schema. | ||
* @param context A BuilderContext instance. | ||
* @returns A BuilderOutput object. | ||
* | ||
* @experimental Direct usage of this function is considered experimental. | ||
*/ | ||
export async function* execute( | ||
options: NgPackagrBuilderOptions, | ||
context: BuilderContext, | ||
): AsyncIterableIterator<BuilderOutput> { | ||
// Purge old build disk cache. | ||
await purgeStaleBuildCache(context); | ||
|
||
const root = context.workspaceRoot; | ||
let packager; | ||
try { | ||
packager = (await import('ng-packagr')).ngPackagr(); | ||
} catch (error) { | ||
assertIsError(error); | ||
if (error.code === 'MODULE_NOT_FOUND') { | ||
return { | ||
success: false, | ||
error: | ||
'The "ng-packagr" package was not found. To correct this error, ensure this package is installed in the project.', | ||
}; | ||
} | ||
|
||
throw error; | ||
} | ||
|
||
packager.forProject(resolve(root, options.project)); | ||
|
||
if (options.tsConfig) { | ||
packager.withTsConfig(resolve(root, options.tsConfig)); | ||
} | ||
|
||
const projectName = context.target?.project; | ||
if (!projectName) { | ||
throw new Error('The builder requires a target.'); | ||
} | ||
|
||
const metadata = await context.getProjectMetadata(projectName); | ||
const { enabled: cacheEnabled, path: cacheDirectory } = normalizeCacheOptions( | ||
metadata, | ||
context.workspaceRoot, | ||
); | ||
|
||
const ngPackagrOptions: NgPackagrOptions = { | ||
cacheEnabled, | ||
poll: options.poll, | ||
cacheDirectory: join(cacheDirectory, 'ng-packagr'), | ||
}; | ||
|
||
try { | ||
if (options.watch) { | ||
await packager.watch(ngPackagrOptions).toPromise(); | ||
} else { | ||
await packager.build(ngPackagrOptions); | ||
} | ||
|
||
yield { success: true }; | ||
} catch (error) { | ||
assertIsError(error); | ||
|
||
yield { success: false, error: error.message }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { createBuilder } from '@angular-devkit/architect'; | ||
import { execute } from './builder'; | ||
import type { Schema as NgPackagrBuilderOptions } from './schema'; | ||
|
||
export { type NgPackagrBuilderOptions, execute }; | ||
export default createBuilder<NgPackagrBuilderOptions>(execute); |
27 changes: 27 additions & 0 deletions
27
packages/angular/build/src/builders/ng-packagr/schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"title": "ng-packagr Target", | ||
"description": "ng-packagr target options for Build Architect. Use to build library projects.", | ||
"type": "object", | ||
"properties": { | ||
"project": { | ||
"type": "string", | ||
"description": "The file path for the ng-packagr configuration file, relative to the current workspace." | ||
}, | ||
"tsConfig": { | ||
"type": "string", | ||
"description": "The full path for the TypeScript configuration file, relative to the current workspace." | ||
}, | ||
"watch": { | ||
"type": "boolean", | ||
"description": "Run build when files change.", | ||
"default": false | ||
}, | ||
"poll": { | ||
"type": "number", | ||
"description": "Enable and define the file watching poll time period in milliseconds." | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"required": ["project"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters