@@ -434,8 +434,12 @@ namespace ts {
434
434
let readFileWithCache = ( f : string ) => host . readFile ( f ) ;
435
435
let projectCompilerOptions = baseCompilerOptions ;
436
436
const compilerHost = createCompilerHostFromProgramHost ( host , ( ) => projectCompilerOptions ) ;
437
+ const originalGetSourceFile = compilerHost . getSourceFile ;
438
+ const computeHash = host . createHash || generateDjb2Hash ;
439
+ updateGetSourceFile ( ) ;
437
440
438
441
// Watch state
442
+ const builderPrograms = createFileMap < T > ( toPath ) ;
439
443
const diagnostics = createFileMap < ReadonlyArray < Diagnostic > > ( toPath ) ;
440
444
const projectPendingBuild = createFileMap < ConfigFileProgramReloadLevel > ( toPath ) ;
441
445
const projectErrorsReported = createFileMap < true > ( toPath ) ;
@@ -493,6 +497,29 @@ namespace ts {
493
497
clearMap ( allWatchedWildcardDirectories , wildCardWatches => clearMap ( wildCardWatches , closeFileWatcherOf ) ) ;
494
498
clearMap ( allWatchedInputFiles , inputFileWatches => clearMap ( inputFileWatches , closeFileWatcher ) ) ;
495
499
clearMap ( allWatchedConfigFiles , closeFileWatcher ) ;
500
+ if ( ! options . watch ) {
501
+ builderPrograms . clear ( ) ;
502
+ }
503
+ updateGetSourceFile ( ) ;
504
+ }
505
+
506
+ function updateGetSourceFile ( ) {
507
+ if ( options . watch ) {
508
+ if ( compilerHost . getSourceFile === originalGetSourceFile ) {
509
+ compilerHost . getSourceFile = ( ...args ) => {
510
+ const result = originalGetSourceFile . call ( compilerHost , ...args ) ;
511
+ if ( result && options . watch ) {
512
+ result . version = computeHash . call ( host , result . text ) ;
513
+ }
514
+ return result ;
515
+ } ;
516
+ }
517
+ }
518
+ else {
519
+ if ( compilerHost . getSourceFile !== originalGetSourceFile ) {
520
+ compilerHost . getSourceFile = originalGetSourceFile ;
521
+ }
522
+ }
496
523
}
497
524
498
525
function isParsedCommandLine ( entry : ConfigFileCacheEntry ) : entry is ParsedCommandLine {
@@ -1057,7 +1084,7 @@ namespace ts {
1057
1084
configFile . fileNames ,
1058
1085
configFile . options ,
1059
1086
compilerHost ,
1060
- /*oldProgram*/ undefined ,
1087
+ builderPrograms . getValue ( proj ) ,
1061
1088
configFile . errors ,
1062
1089
configFile . projectReferences
1063
1090
) ;
@@ -1123,22 +1150,28 @@ namespace ts {
1123
1150
} ;
1124
1151
diagnostics . removeKey ( proj ) ;
1125
1152
projectStatus . setValue ( proj , status ) ;
1126
- if ( host . afterProgramEmitAndDiagnostics ) {
1127
- host . afterProgramEmitAndDiagnostics ( program ) ;
1128
- }
1153
+ afterProgramCreate ( proj , program ) ;
1129
1154
return resultFlags ;
1130
1155
1131
1156
function buildErrors ( diagnostics : ReadonlyArray < Diagnostic > , errorFlags : BuildResultFlags , errorType : string ) {
1132
1157
resultFlags |= errorFlags ;
1133
1158
reportAndStoreErrors ( proj , diagnostics ) ;
1134
1159
projectStatus . setValue ( proj , { type : UpToDateStatusType . Unbuildable , reason : `${ errorType } errors` } ) ;
1135
- if ( host . afterProgramEmitAndDiagnostics ) {
1136
- host . afterProgramEmitAndDiagnostics ( program ) ;
1137
- }
1160
+ afterProgramCreate ( proj , program ) ;
1138
1161
return resultFlags ;
1139
1162
}
1140
1163
}
1141
1164
1165
+ function afterProgramCreate ( proj : ResolvedConfigFileName , program : T ) {
1166
+ if ( host . afterProgramEmitAndDiagnostics ) {
1167
+ host . afterProgramEmitAndDiagnostics ( program ) ;
1168
+ }
1169
+ if ( options . watch ) {
1170
+ program . releaseProgram ( ) ;
1171
+ builderPrograms . setValue ( proj , program ) ;
1172
+ }
1173
+ }
1174
+
1142
1175
function updateOutputTimestamps ( proj : ParsedCommandLine ) {
1143
1176
if ( options . dry ) {
1144
1177
return reportStatus ( Diagnostics . A_non_dry_build_would_build_project_0 , proj . options . configFilePath ! ) ;
0 commit comments