@@ -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,30 @@ 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 ( context , ngPackagrOptions ) ;
259+ } else {
252260 context . logger . warn (
253261 `The 'buildTarget' is configured to use '${ builderName } ', which is not supported. ` +
254- `The 'unit-test' builder is designed to work with '@angular/build:application'. ` +
262+ `The 'unit-test' builder is designed to work with '@angular/build:application' or '@angular/build:ng-packagr' . ` +
255263 'Unexpected behavior or build failures may occur.' ,
256264 ) ;
257- }
258265
259- buildTargetOptions = ( await context . validateOptions (
260- await context . getTargetOptions ( normalizedOptions . buildTarget ) ,
261- builderName ,
262- ) ) as unknown as ApplicationBuilderInternalOptions ;
266+ buildTargetOptions = ( await context . validateOptions (
267+ await context . getTargetOptions ( normalizedOptions . buildTarget ) ,
268+ builderName ,
269+ ) ) as unknown as ApplicationBuilderInternalOptions ;
270+ }
263271 } catch ( e ) {
264272 assertIsError ( e ) ;
265273 context . logger . error (
@@ -335,3 +343,36 @@ export async function* execute(
335343 yield { success : false } ;
336344 }
337345}
346+
347+ async function transformNgPackagrOptions (
348+ context : BuilderContext ,
349+ options : Record < string , unknown > ,
350+ ) : Promise < ApplicationBuilderInternalOptions > {
351+ const projectPath = options [ 'project' ] ;
352+
353+ if ( typeof projectPath !== 'string' ) {
354+ throw new Error ( 'ng-packagr builder options must have a "project" property.' ) ;
355+ }
356+
357+ const ngPackagePath = path . join ( context . workspaceRoot , projectPath ) ;
358+ let ngPackageJson ;
359+ try {
360+ ngPackageJson = JSON . parse ( await readFile ( ngPackagePath , 'utf-8' ) ) ;
361+ } catch ( e ) {
362+ assertIsError ( e ) ;
363+ throw new Error ( `Could not read ng-package.json at ${ ngPackagePath } : ${ e . message } ` ) ;
364+ }
365+
366+ const lib = ngPackageJson [ 'lib' ] || { } ;
367+ const styleIncludePaths = lib [ 'styleIncludePaths' ] || [ ] ;
368+ const assets = ngPackageJson [ 'assets' ] || [ ] ;
369+ const inlineStyleLanguage = ngPackageJson [ 'inlineStyleLanguage' ] ;
370+
371+ return {
372+ stylePreprocessorOptions : styleIncludePaths . length
373+ ? { includePaths : styleIncludePaths }
374+ : undefined ,
375+ assets : assets . length ? assets : undefined ,
376+ inlineStyleLanguage : typeof inlineStyleLanguage === 'string' ? inlineStyleLanguage : undefined ,
377+ } as ApplicationBuilderInternalOptions ;
378+ }
0 commit comments