@@ -12,7 +12,7 @@ import {
1212 targetStringFromTarget ,
1313} from '@angular-devkit/architect' ;
1414import assert from 'node:assert' ;
15- import { rm } from 'node:fs/promises' ;
15+ import { readFile , rm } from 'node:fs/promises' ;
1616import path from 'node:path' ;
1717import { createVirtualModulePlugin } from '../../tools/esbuild/virtual-module-plugin' ;
1818import { assertIsError } from '../../utils/error' ;
@@ -244,22 +244,34 @@ export async function* execute(
244244 let buildTargetOptions : ApplicationBuilderInternalOptions ;
245245 try {
246246 const builderName = await context . getBuilderNameForTarget ( normalizedOptions . buildTarget ) ;
247- if (
248- builderName !== '@angular/build:application' &&
249- // TODO: Add comprehensive support for ng-packagr.
250- builderName !== '@angular/build:ng-packagr'
251- ) {
247+ if ( builderName === '@angular/build:application' ) {
248+ buildTargetOptions = ( await context . validateOptions (
249+ await context . getTargetOptions ( normalizedOptions . buildTarget ) ,
250+ builderName ,
251+ ) ) as unknown as ApplicationBuilderInternalOptions ;
252+ } else if ( builderName === '@angular/build:ng-packagr' ) {
253+ const ngPackagrOptions = await context . validateOptions (
254+ await context . getTargetOptions ( normalizedOptions . buildTarget ) ,
255+ builderName ,
256+ ) ;
257+
258+ buildTargetOptions = await transformNgPackagrOptions (
259+ context ,
260+ ngPackagrOptions ,
261+ normalizedOptions . projectRoot ,
262+ ) ;
263+ } else {
252264 context . logger . warn (
253265 `The 'buildTarget' is configured to use '${ builderName } ', which is not supported. ` +
254- `The 'unit-test' builder is designed to work with '@angular/build:application'. ` +
266+ `The 'unit-test' builder is designed to work with '@angular/build:application' or '@angular/build:ng-packagr' . ` +
255267 'Unexpected behavior or build failures may occur.' ,
256268 ) ;
257- }
258269
259- buildTargetOptions = ( await context . validateOptions (
260- await context . getTargetOptions ( normalizedOptions . buildTarget ) ,
261- builderName ,
262- ) ) as unknown as ApplicationBuilderInternalOptions ;
270+ buildTargetOptions = ( await context . validateOptions (
271+ await context . getTargetOptions ( normalizedOptions . buildTarget ) ,
272+ builderName ,
273+ ) ) as unknown as ApplicationBuilderInternalOptions ;
274+ }
263275 } catch ( e ) {
264276 assertIsError ( e ) ;
265277 context . logger . error (
@@ -335,3 +347,42 @@ export async function* execute(
335347 yield { success : false } ;
336348 }
337349}
350+
351+ async function transformNgPackagrOptions (
352+ context : BuilderContext ,
353+ options : Record < string , unknown > ,
354+ projectRoot : string ,
355+ ) : Promise < ApplicationBuilderInternalOptions > {
356+ const projectPath = options [ 'project' ] ;
357+
358+ let ngPackagePath : string ;
359+ if ( projectPath ) {
360+ if ( typeof projectPath !== 'string' ) {
361+ throw new Error ( 'ng-packagr builder options "project" property must be a string.' ) ;
362+ }
363+ ngPackagePath = path . join ( context . workspaceRoot , projectPath ) ;
364+ } else {
365+ ngPackagePath = path . join ( projectRoot , 'ng-package.json' ) ;
366+ }
367+
368+ let ngPackageJson ;
369+ try {
370+ ngPackageJson = JSON . parse ( await readFile ( ngPackagePath , 'utf-8' ) ) ;
371+ } catch ( e ) {
372+ assertIsError ( e ) ;
373+ throw new Error ( `Could not read ng-package.json at ${ ngPackagePath } : ${ e . message } ` ) ;
374+ }
375+
376+ const lib = ngPackageJson [ 'lib' ] || { } ;
377+ const styleIncludePaths = lib [ 'styleIncludePaths' ] || [ ] ;
378+ const assets = ngPackageJson [ 'assets' ] || [ ] ;
379+ const inlineStyleLanguage = ngPackageJson [ 'inlineStyleLanguage' ] ;
380+
381+ return {
382+ stylePreprocessorOptions : styleIncludePaths . length
383+ ? { includePaths : styleIncludePaths }
384+ : undefined ,
385+ assets : assets . length ? assets : undefined ,
386+ inlineStyleLanguage : typeof inlineStyleLanguage === 'string' ? inlineStyleLanguage : undefined ,
387+ } as ApplicationBuilderInternalOptions ;
388+ }
0 commit comments