@@ -24,6 +24,7 @@ import {
2424import { JSONFile } from '../../utility/json-file' ;
2525import { latestVersions } from '../../utility/latest-versions' ;
2626import {
27+ type ProjectDefinition ,
2728 type TargetDefinition ,
2829 allTargetOptions ,
2930 allWorkspaceTargets ,
@@ -365,20 +366,22 @@ function migrateTargetOptions(
365366/**
366367 * Migrates a single library project's `build` target from `ng-packagr` to `@angular/build:library`,
367368 * transferring configuration options to `angular.json`, writing entry points to `package.json#exports`,
368- * and cleaning up obsolete `ng-packagr` configuration files.
369+ * aligning `sourceRoot` and TypeScript `include` globs, and cleaning up obsolete `ng-packagr`
370+ * configuration files.
369371 *
370- * @param projectRoot The root directory of the library project .
372+ * @param project The library project definition in `angular.json` .
371373 * @param buildTarget The library's `build` target definition in `angular.json`.
372374 * @param tree The virtual file system tree.
373375 * @param context The schematic execution context.
374376 * @returns `true` if the library target was migrated, or `false` if it was skipped.
375377 */
376378function updateLibraryTarget (
377- projectRoot : string ,
379+ project : ProjectDefinition ,
378380 buildTarget : TargetDefinition ,
379381 tree : Tree ,
380382 context : SchematicContext ,
381383) : boolean {
384+ const projectRoot = project . root ;
382385 const normalizedProjectRoot = normalizeRelativePath ( projectRoot ) ;
383386 let primaryProjectFile =
384387 typeof buildTarget . options ?. [ 'project' ] === 'string'
@@ -470,6 +473,18 @@ function updateLibraryTarget(
470473
471474 migrateTargetOptions ( normalizedProjectRoot , projectDir , buildTarget , ngPackageConfig , tree ) ;
472475
476+ const defaultSourceRoot = join ( normalizedProjectRoot , 'src' ) ;
477+ if (
478+ project . sourceRoot === undefined ||
479+ normalizeRelativePath ( project . sourceRoot ) === defaultSourceRoot
480+ ) {
481+ project . sourceRoot = project . root ;
482+ }
483+
484+ if ( normalizeRelativePath ( project . sourceRoot ) === normalizedProjectRoot ) {
485+ updateLibraryTsConfigs ( project , normalizedProjectRoot , buildTarget , tree ) ;
486+ }
487+
473488 const packageJsonPath = join ( normalizedProjectRoot , 'package.json' ) ;
474489
475490 // Delete primary ng-package.json if it exists
@@ -582,6 +597,104 @@ function updateLibraryPackageJson(
582597 json . modify ( [ 'exports' ] , updatedExports ) ;
583598}
584599
600+ /**
601+ * Updates `include` globs in the library's TypeScript configuration files (such as
602+ * `tsconfig.lib.json` and `tsconfig.spec.json`) from `src/**` to `**` so secondary
603+ * entry points outside `src/` are covered by TypeScript and test discovery.
604+ *
605+ * @param project The library project definition in `angular.json`.
606+ * @param normalizedProjectRoot The normalized root directory of the library project.
607+ * @param buildTarget The library's `build` target definition in `angular.json`.
608+ * @param tree The virtual file system tree.
609+ */
610+ function updateLibraryTsConfigs (
611+ project : ProjectDefinition ,
612+ normalizedProjectRoot : string ,
613+ buildTarget : TargetDefinition ,
614+ tree : Tree ,
615+ ) : void {
616+ const tsConfigPaths = new Set < string > ( [
617+ join ( normalizedProjectRoot , 'tsconfig.lib.json' ) ,
618+ join ( normalizedProjectRoot , 'tsconfig.spec.json' ) ,
619+ ] ) ;
620+
621+ for ( const targetName of [ 'build' , 'test' ] ) {
622+ const target = targetName === 'build' ? buildTarget : project . targets . get ( targetName ) ;
623+ if ( ! target ) {
624+ continue ;
625+ }
626+
627+ for ( const [ , options ] of allTargetOptions ( target , false ) ) {
628+ if ( typeof options [ 'tsConfig' ] === 'string' ) {
629+ tsConfigPaths . add ( normalizeRelativePath ( options [ 'tsConfig' ] ) ) ;
630+ }
631+ }
632+ }
633+
634+ const outputPath =
635+ typeof buildTarget . options ?. [ 'outputPath' ] === 'string'
636+ ? normalizeRelativePath ( buildTarget . options [ 'outputPath' ] )
637+ : undefined ;
638+ const relativeOutputPath =
639+ outputPath !== undefined ? relative ( normalizedProjectRoot , outputPath ) : undefined ;
640+ const internalOutputDir =
641+ relativeOutputPath && ! relativeOutputPath . startsWith ( '..' ) ? relativeOutputPath : undefined ;
642+
643+ for ( const tsConfigPath of tsConfigPaths ) {
644+ if (
645+ ! tree . exists ( tsConfigPath ) ||
646+ normalizeRelativePath ( dirname ( tsConfigPath ) ) !== normalizedProjectRoot
647+ ) {
648+ continue ;
649+ }
650+
651+ let json : JSONFile ;
652+ try {
653+ json = new JSONFile ( tree , tsConfigPath ) ;
654+ } catch {
655+ continue ;
656+ }
657+
658+ const include = json . get ( [ 'include' ] ) ;
659+ if ( ! Array . isArray ( include ) ) {
660+ continue ;
661+ }
662+
663+ let updated = false ;
664+ const newInclude : JsonValue [ ] = [ ] ;
665+ for ( const entry of include ) {
666+ if ( typeof entry === 'string' ) {
667+ const replaced = entry . replace ( / ^ (?: \. \/ ) ? s r c \/ (? = \* \* (?: \/ | $ ) ) / , '' ) ;
668+ if ( replaced !== entry ) {
669+ updated = true ;
670+ }
671+ if ( ! newInclude . includes ( replaced ) ) {
672+ newInclude . push ( replaced ) ;
673+ }
674+ } else {
675+ newInclude . push ( entry ) ;
676+ }
677+ }
678+
679+ if ( ! updated ) {
680+ continue ;
681+ }
682+
683+ json . modify ( [ 'include' ] , newInclude ) ;
684+
685+ if ( internalOutputDir ) {
686+ const exclude = json . get ( [ 'exclude' ] ) ;
687+ if ( Array . isArray ( exclude ) ) {
688+ if ( ! exclude . includes ( internalOutputDir ) ) {
689+ json . modify ( [ 'exclude' ] , [ ...exclude , internalOutputDir ] ) ;
690+ }
691+ } else if ( exclude === undefined ) {
692+ json . modify ( [ 'exclude' ] , [ internalOutputDir ] ) ;
693+ }
694+ }
695+ }
696+ }
697+
585698/**
586699 * Migration main entrypoint for use-library-builder.
587700 */
@@ -605,7 +718,7 @@ export default function (): Rule {
605718 continue ;
606719 }
607720
608- if ( updateLibraryTarget ( project . root , buildTarget , tree , context ) ) {
721+ if ( updateLibraryTarget ( project , buildTarget , tree , context ) ) {
609722 hasMigratedLibrary = true ;
610723 }
611724 }
0 commit comments