@@ -161,6 +161,8 @@ namespace ts {
161161 * true if program has been emitted
162162 */
163163 programEmitComplete ?: true ;
164+ /** Stores list of files that change signature during emit - test only */
165+ filesChangingSignature ?: Set < Path > ;
164166 }
165167
166168 function hasSameKeys ( map1 : ReadonlyCollection < string > | undefined , map2 : ReadonlyCollection < string > | undefined ) : boolean {
@@ -1150,7 +1152,9 @@ namespace ts {
11501152 // Otherwise just affected file
11511153 Debug . checkDefined ( state . program ) . emit (
11521154 affected === state . program ? undefined : affected as SourceFile ,
1153- writeFile || maybeBind ( host , host . writeFile ) ,
1155+ affected !== state . program && getEmitDeclarations ( state . compilerOptions ) && ! customTransformers ?
1156+ getWriteFileUpdatingSignatureCallback ( writeFile ) :
1157+ writeFile || maybeBind ( host , host . writeFile ) ,
11541158 cancellationToken ,
11551159 emitOnlyDtsFiles || emitKind === BuilderFileEmit . DtsOnly ,
11561160 customTransformers
@@ -1161,6 +1165,34 @@ namespace ts {
11611165 ) ;
11621166 }
11631167
1168+ function getWriteFileUpdatingSignatureCallback ( writeFile : WriteFileCallback | undefined ) : WriteFileCallback {
1169+ return ( fileName , text , writeByteOrderMark , onError , sourceFiles , data ) => {
1170+ if ( isDeclarationFileName ( fileName ) ) {
1171+ Debug . assert ( sourceFiles ?. length === 1 ) ;
1172+ const file = sourceFiles [ 0 ] ;
1173+ const info = state . fileInfos . get ( file . resolvedPath ) ! ;
1174+ const signature = state . currentAffectedFilesSignatures ?. get ( file . resolvedPath ) || info . signature ;
1175+ if ( signature === file . version ) {
1176+ const newSignature = ( computeHash || generateDjb2Hash ) ( data ?. sourceMapUrlPos !== undefined ? text . substring ( 0 , data . sourceMapUrlPos ) : text ) ;
1177+ if ( newSignature !== file . version ) { // Update it
1178+ if ( host . storeFilesChangingSignatureDuringEmit ) ( state . filesChangingSignature ||= new Set ( ) ) . add ( file . resolvedPath ) ;
1179+ if ( state . exportedModulesMap ) BuilderState . updateExportedModules ( file , file . exportedModulesFromDeclarationEmit , state . currentAffectedFilesExportedModulesMap ||= BuilderState . createManyToManyPathMap ( ) ) ;
1180+ if ( state . affectedFiles && state . affectedFilesIndex ! < state . affectedFiles . length ) {
1181+ state . currentAffectedFilesSignatures ! . set ( file . resolvedPath , newSignature ) ;
1182+ }
1183+ else {
1184+ info . signature = newSignature ;
1185+ if ( state . exportedModulesMap ) BuilderState . updateExportedFilesMapFromCache ( state , state . currentAffectedFilesExportedModulesMap ) ;
1186+ }
1187+ }
1188+ }
1189+ }
1190+ if ( writeFile ) writeFile ( fileName , text , writeByteOrderMark , onError , sourceFiles , data ) ;
1191+ else if ( host . writeFile ) host . writeFile ( fileName , text , writeByteOrderMark , onError , sourceFiles , data ) ;
1192+ else state . program ! . writeFile ( fileName , text , writeByteOrderMark , onError , sourceFiles , data ) ;
1193+ } ;
1194+ }
1195+
11641196 /**
11651197 * Emits the JavaScript and declaration files.
11661198 * When targetSource file is specified, emits the files corresponding to that source file,
@@ -1215,7 +1247,15 @@ namespace ts {
12151247 }
12161248 }
12171249 }
1218- return Debug . checkDefined ( state . program ) . emit ( targetSourceFile , writeFile || maybeBind ( host , host . writeFile ) , cancellationToken , emitOnlyDtsFiles , customTransformers ) ;
1250+ return Debug . checkDefined ( state . program ) . emit (
1251+ targetSourceFile ,
1252+ ! outFile ( state . compilerOptions ) && getEmitDeclarations ( state . compilerOptions ) && ! customTransformers ?
1253+ getWriteFileUpdatingSignatureCallback ( writeFile ) :
1254+ writeFile || maybeBind ( host , host . writeFile ) ,
1255+ cancellationToken ,
1256+ emitOnlyDtsFiles ,
1257+ customTransformers
1258+ ) ;
12191259 }
12201260
12211261 /**
0 commit comments