@@ -45,6 +45,12 @@ namespace ts {
4545 * Otherwise undefined
4646 */
4747 readonly exportedModulesMap : ESMap < Path , BuilderState . ReferencedSet > | undefined ;
48+
49+ /**
50+ * true if file version is used as signature
51+ * This helps in delaying the calculation of the d.ts hash as version for the file till reasonable time
52+ */
53+ useFileVersionAsSignature : boolean ;
4854 /**
4955 * Map of files that have already called update signature.
5056 * That means hence forth these files are assumed to have
@@ -202,7 +208,7 @@ namespace ts {
202208 /**
203209 * Creates the state of file references and signature for the new program from oldState if it is safe
204210 */
205- export function create ( newProgram : Program , getCanonicalFileName : GetCanonicalFileName , oldState ?: Readonly < ReusableBuilderState > ) : BuilderState {
211+ export function create ( newProgram : Program , getCanonicalFileName : GetCanonicalFileName , oldState ?: Readonly < ReusableBuilderState > , disableUseFileVersionAsSignature ?: boolean ) : BuilderState {
206212 const fileInfos = new Map < Path , FileInfo > ( ) ;
207213 const referencedMap = newProgram . getCompilerOptions ( ) . module !== ModuleKind . None ? new Map < Path , ReferencedSet > ( ) : undefined ;
208214 const exportedModulesMap = referencedMap ? new Map < Path , ReferencedSet > ( ) : undefined ;
@@ -236,7 +242,8 @@ namespace ts {
236242 fileInfos,
237243 referencedMap,
238244 exportedModulesMap,
239- hasCalledUpdateShapeSignature
245+ hasCalledUpdateShapeSignature,
246+ useFileVersionAsSignature : ! disableUseFileVersionAsSignature && ! useOldState
240247 } ;
241248 }
242249
@@ -258,6 +265,7 @@ namespace ts {
258265 referencedMap : state . referencedMap && new Map ( state . referencedMap ) ,
259266 exportedModulesMap : state . exportedModulesMap && new Map ( state . exportedModulesMap ) ,
260267 hasCalledUpdateShapeSignature : new Set ( state . hasCalledUpdateShapeSignature ) ,
268+ useFileVersionAsSignature : state . useFileVersionAsSignature ,
261269 } ;
262270 }
263271
@@ -316,16 +324,8 @@ namespace ts {
316324 if ( ! info ) return Debug . fail ( ) ;
317325
318326 const prevSignature = info . signature ;
319- let latestSignature : string ;
320- if ( sourceFile . isDeclarationFile ) {
321- latestSignature = sourceFile . version ;
322- if ( exportedModulesMapCache && latestSignature !== prevSignature ) {
323- // All the references in this file are exported
324- const references = state . referencedMap ? state . referencedMap . get ( sourceFile . resolvedPath ) : undefined ;
325- exportedModulesMapCache . set ( sourceFile . resolvedPath , references || false ) ;
326- }
327- }
328- else {
327+ let latestSignature : string | undefined ;
328+ if ( ! sourceFile . isDeclarationFile && ! state . useFileVersionAsSignature ) {
329329 const emitOutput = getFileEmitOutput (
330330 programOfThisState ,
331331 sourceFile ,
@@ -334,25 +334,26 @@ namespace ts {
334334 /*customTransformers*/ undefined ,
335335 /*forceDtsEmit*/ true
336336 ) ;
337- const firstDts = emitOutput . outputFiles &&
338- programOfThisState . getCompilerOptions ( ) . declarationMap ?
339- emitOutput . outputFiles . length > 1 ? emitOutput . outputFiles [ 1 ] : undefined :
340- emitOutput . outputFiles . length > 0 ? emitOutput . outputFiles [ 0 ] : undefined ;
337+ const firstDts = firstOrUndefined ( emitOutput . outputFiles ) ;
341338 if ( firstDts ) {
342339 Debug . assert ( fileExtensionIs ( firstDts . name , Extension . Dts ) , "File extension for signature expected to be dts" , ( ) => `Found: ${ getAnyExtensionFromPath ( firstDts . name ) } for ${ firstDts . name } :: All output files: ${ JSON . stringify ( emitOutput . outputFiles . map ( f => f . name ) ) } ` ) ;
343340 latestSignature = ( computeHash || generateDjb2Hash ) ( firstDts . text ) ;
344341 if ( exportedModulesMapCache && latestSignature !== prevSignature ) {
345342 updateExportedModules ( sourceFile , emitOutput . exportedModulesFromDeclarationEmit , exportedModulesMapCache ) ;
346343 }
347344 }
348- else {
349- latestSignature = prevSignature ! ; // TODO: GH#18217
345+ }
346+ // Default is to use file version as signature
347+ if ( latestSignature === undefined ) {
348+ latestSignature = sourceFile . version ;
349+ if ( exportedModulesMapCache && latestSignature !== prevSignature ) {
350+ // All the references in this file are exported
351+ const references = state . referencedMap ? state . referencedMap . get ( sourceFile . resolvedPath ) : undefined ;
352+ exportedModulesMapCache . set ( sourceFile . resolvedPath , references || false ) ;
350353 }
351-
352354 }
353355 cacheToUpdateSignature . set ( sourceFile . resolvedPath , latestSignature ) ;
354-
355- return ! prevSignature || latestSignature !== prevSignature ;
356+ return latestSignature !== prevSignature ;
356357 }
357358
358359 /**
@@ -479,7 +480,7 @@ namespace ts {
479480 */
480481 function isFileAffectingGlobalScope ( sourceFile : SourceFile ) {
481482 return containsGlobalScopeAugmentation ( sourceFile ) ||
482- ! isExternalModule ( sourceFile ) && ! containsOnlyAmbientModules ( sourceFile ) ;
483+ ! isExternalOrCommonJsModule ( sourceFile ) && ! containsOnlyAmbientModules ( sourceFile ) ;
483484 }
484485
485486 /**
0 commit comments