@@ -61,10 +61,12 @@ export class AotCompilation extends TypeScriptCompilation {
6161 super ( ) ;
6262 }
6363
64+ // eslint-disable-next-line max-lines-per-function
6465 async initialize (
6566 tsconfig : string ,
6667 hostOptions : AngularHostOptions ,
6768 compilerOptionOverrides ?: CompilerOptionOverrides ,
69+ buildType : 'application' | 'library' = 'application' ,
6870 ) : Promise < AngularCompilationResult > {
6971 // Dynamically load the Angular compiler CLI package
7072 const { NgtscProgram, OptimizeFor } = await TypeScriptCompilation . loadCompilerCli ( ) ;
@@ -75,7 +77,7 @@ export class AotCompilation extends TypeScriptCompilation {
7577 rootNames,
7678 errors : configurationDiagnostics ,
7779 warnings,
78- } = await this . loadConfiguration ( tsconfig , compilerOptionOverrides ) ;
80+ } = await this . loadConfiguration ( tsconfig , compilerOptionOverrides , buildType ) ;
7981
8082 const useTypeScriptTranspilation =
8183 ( compilerOptions [ '_useTypeScriptTranspilation' ] as boolean | undefined ) ??
@@ -332,9 +334,11 @@ export class AotCompilation extends TypeScriptCompilation {
332334 useTypeScriptTranspilation,
333335 } = this . #state;
334336 const compilerOptions = typeScriptProgram . getCompilerOptions ( ) ;
337+ const isLibraryEmit = ! ! compilerOptions . declaration ;
335338 const buildInfoFilename = compilerOptions . tsBuildInfoFile ?? '.tsbuildinfo' ;
336339
337- const emittedFiles = new Map < ts . SourceFile , EmitFileResult > ( ) ;
340+ const emittedFiles = new Map < string , EmitFileResult > ( ) ;
341+ const emittedSourceFiles = new Set < ts . SourceFile > ( ) ;
338342 const writeFileCallback : ts . WriteFileCallback = ( filename , contents , _a , _b , sourceFiles ) => {
339343 if ( ! sourceFiles ?. length && filename . endsWith ( buildInfoFilename ) ) {
340344 // Save builder info contents to specified location
@@ -350,17 +354,21 @@ export class AotCompilation extends TypeScriptCompilation {
350354 }
351355
352356 angularCompiler . incrementalCompilation . recordSuccessfulEmit ( sourceFile ) ;
353- emittedFiles . set ( sourceFile , { filename : sourceFile . fileName , contents } ) ;
357+ emittedSourceFiles . add ( sourceFile ) ;
358+ const targetFilename = isLibraryEmit ? filename : sourceFile . fileName ;
359+ emittedFiles . set ( targetFilename , { filename : targetFilename , contents } ) ;
354360 } ;
355361 const transformers = angularCompiler . prepareEmit ( ) . transformers ;
356- transformers . before ??= [ ] ;
357- transformers . before . push (
358- replaceBootstrap ( ( ) => typeScriptProgram . getProgram ( ) . getTypeChecker ( ) ) ,
359- webWorkerTransform ,
360- ) ;
362+ if ( ! isLibraryEmit ) {
363+ transformers . before ??= [ ] ;
364+ transformers . before . push (
365+ replaceBootstrap ( ( ) => typeScriptProgram . getProgram ( ) . getTypeChecker ( ) ) ,
366+ webWorkerTransform ,
367+ ) ;
361368
362- if ( ! this . browserOnlyBuild ) {
363- transformers . before . push ( lazyRoutesTransformer ( compilerOptions , compilerHost ) ) ;
369+ if ( ! this . browserOnlyBuild ) {
370+ transformers . before . push ( lazyRoutesTransformer ( compilerOptions , compilerHost ) ) ;
371+ }
364372 }
365373
366374 // Emit is handled in write file callback when using TypeScript
@@ -394,7 +402,7 @@ export class AotCompilation extends TypeScriptCompilation {
394402
395403 // Angular may have files that must be emitted but TypeScript does not consider affected
396404 for ( const sourceFile of typeScriptProgram . getSourceFiles ( ) ) {
397- if ( emittedFiles . has ( sourceFile ) || angularCompiler . ignoreForEmit . has ( sourceFile ) ) {
405+ if ( emittedSourceFiles . has ( sourceFile ) || angularCompiler . ignoreForEmit . has ( sourceFile ) ) {
398406 continue ;
399407 }
400408
@@ -410,7 +418,8 @@ export class AotCompilation extends TypeScriptCompilation {
410418 }
411419
412420 if ( useTypeScriptTranspilation ) {
413- typeScriptProgram . emit ( sourceFile , writeFileCallback , undefined , undefined , transformers ) ;
421+ const emitOnly = affectedFiles . has ( sourceFile ) ? undefined : false ;
422+ typeScriptProgram . emit ( sourceFile , writeFileCallback , undefined , emitOnly , transformers ) ;
414423 continue ;
415424 }
416425
@@ -451,13 +460,14 @@ export class AotCompilation extends TypeScriptCompilation {
451460 contents += `\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${ base64Map } ` ;
452461 } else if ( compilerOptions . sourceMap ) {
453462 const mapFilename = sourceFile . fileName + '.map' ;
454- emittedFiles . set ( sourceFile , { filename : mapFilename , contents : printResult . map } ) ;
463+ emittedFiles . set ( mapFilename , { filename : mapFilename , contents : printResult . map } ) ;
455464 }
456465 }
457466 }
458467
459468 angularCompiler . incrementalCompilation . recordSuccessfulEmit ( sourceFile ) ;
460- emittedFiles . set ( sourceFile , { filename : sourceFile . fileName , contents } ) ;
469+ emittedSourceFiles . add ( sourceFile ) ;
470+ emittedFiles . set ( sourceFile . fileName , { filename : sourceFile . fileName , contents } ) ;
461471 }
462472
463473 return emittedFiles . values ( ) ;
0 commit comments