@@ -17,6 +17,7 @@ import {
17
17
} from './TypeScriptConfigurationParser' ;
18
18
import { createPerformance } from '../../profile/Performance' ;
19
19
import { connectTypeScriptPerformance } from '../profile/TypeScriptPerformance' ;
20
+ import { createControlledCompilerHost } from './ControlledCompilerHost' ;
20
21
21
22
// write this type as it's available only in the newest TypeScript versions (^4.1.0)
22
23
interface Tracing {
@@ -30,12 +31,14 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
30
31
let parseConfigurationDiagnostics : ts . Diagnostic [ ] = [ ] ;
31
32
let dependencies : Dependencies | undefined ;
32
33
let configurationChanged = false ;
34
+ let compilerHost : ts . CompilerHost | undefined ;
33
35
let watchCompilerHost :
34
36
| ts . WatchCompilerHostOfFilesAndCompilerOptions < ts . SemanticDiagnosticsBuilderProgram >
35
37
| undefined ;
36
38
let watchSolutionBuilderHost :
37
39
| ts . SolutionBuilderWithWatchHost < ts . SemanticDiagnosticsBuilderProgram >
38
40
| undefined ;
41
+ let program : ts . Program | undefined ;
39
42
let watchProgram :
40
43
| ts . WatchOfFilesAndCompilerOptions < ts . SemanticDiagnosticsBuilderProgram >
41
44
| undefined ;
@@ -69,27 +72,27 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
69
72
return ( typescript as any ) . tracing ;
70
73
}
71
74
72
- function getDiagnosticsOfBuilderProgram ( builderProgram : ts . BuilderProgram ) {
75
+ function getDiagnosticsOfProgram ( program : ts . Program | ts . BuilderProgram ) {
73
76
const diagnostics : ts . Diagnostic [ ] = [ ] ;
74
77
75
78
if ( configuration . diagnosticOptions . syntactic ) {
76
79
performance . markStart ( 'Syntactic Diagnostics' ) ;
77
- diagnostics . push ( ...builderProgram . getSyntacticDiagnostics ( ) ) ;
80
+ diagnostics . push ( ...program . getSyntacticDiagnostics ( ) ) ;
78
81
performance . markEnd ( 'Syntactic Diagnostics' ) ;
79
82
}
80
83
if ( configuration . diagnosticOptions . global ) {
81
84
performance . markStart ( 'Global Diagnostics' ) ;
82
- diagnostics . push ( ...builderProgram . getGlobalDiagnostics ( ) ) ;
85
+ diagnostics . push ( ...program . getGlobalDiagnostics ( ) ) ;
83
86
performance . markEnd ( 'Global Diagnostics' ) ;
84
87
}
85
88
if ( configuration . diagnosticOptions . semantic ) {
86
89
performance . markStart ( 'Semantic Diagnostics' ) ;
87
- diagnostics . push ( ...builderProgram . getSemanticDiagnostics ( ) ) ;
90
+ diagnostics . push ( ...program . getSemanticDiagnostics ( ) ) ;
88
91
performance . markEnd ( 'Semantic Diagnostics' ) ;
89
92
}
90
93
if ( configuration . diagnosticOptions . declaration ) {
91
94
performance . markStart ( 'Declaration Diagnostics' ) ;
92
- diagnostics . push ( ...builderProgram . getDeclarationDiagnostics ( ) ) ;
95
+ diagnostics . push ( ...program . getDeclarationDiagnostics ( ) ) ;
93
96
performance . markEnd ( 'Declaration Diagnostics' ) ;
94
97
}
95
98
@@ -221,7 +224,7 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
221
224
}
222
225
223
226
return {
224
- getReport : async ( { changedFiles = [ ] , deletedFiles = [ ] } ) => {
227
+ getReport : async ( { changedFiles = [ ] , deletedFiles = [ ] } , watching ) => {
225
228
// clear cache to be ready for next iteration and to free memory
226
229
system . clearCache ( ) ;
227
230
@@ -233,8 +236,10 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
233
236
// we need to re-create programs
234
237
parsedConfiguration = undefined ;
235
238
dependencies = undefined ;
239
+ compilerHost = undefined ;
236
240
watchCompilerHost = undefined ;
237
241
watchSolutionBuilderHost = undefined ;
242
+ program = undefined ;
238
243
watchProgram = undefined ;
239
244
solutionBuilder = undefined ;
240
245
@@ -346,7 +351,7 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
346
351
undefined ,
347
352
( builderProgram ) => {
348
353
const projectName = getProjectNameOfBuilderProgram ( builderProgram ) ;
349
- const diagnostics = getDiagnosticsOfBuilderProgram ( builderProgram ) ;
354
+ const diagnostics = getDiagnosticsOfProgram ( builderProgram ) ;
350
355
351
356
// update diagnostics
352
357
diagnosticsPerProject . set ( projectName , diagnostics ) ;
@@ -379,7 +384,7 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
379
384
solutionBuilder . build ( ) ;
380
385
performance . markEnd ( 'Build Solutions' ) ;
381
386
}
382
- } else {
387
+ } else if ( watching ) {
383
388
// watch compiler case
384
389
// ensure watch compiler host exists
385
390
if ( ! watchCompilerHost ) {
@@ -412,7 +417,7 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
412
417
undefined ,
413
418
( builderProgram ) => {
414
419
const projectName = getProjectNameOfBuilderProgram ( builderProgram ) ;
415
- const diagnostics = getDiagnosticsOfBuilderProgram ( builderProgram ) ;
420
+ const diagnostics = getDiagnosticsOfProgram ( builderProgram ) ;
416
421
417
422
// update diagnostics
418
423
diagnosticsPerProject . set ( projectName , diagnostics ) ;
@@ -440,6 +445,28 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
440
445
watchProgram . updateRootFileNames ( dependencies . files ) ;
441
446
shouldUpdateRootFiles = false ;
442
447
}
448
+ } else {
449
+ if ( ! compilerHost ) {
450
+ compilerHost = createControlledCompilerHost (
451
+ typescript ,
452
+ parsedConfiguration ,
453
+ system ,
454
+ extensions
455
+ ) ;
456
+ }
457
+ if ( ! program ) {
458
+ program = ts . createProgram ( {
459
+ rootNames : parsedConfiguration . fileNames ,
460
+ options : parsedConfiguration . options ,
461
+ projectReferences : parsedConfiguration . projectReferences ,
462
+ host : compilerHost ,
463
+ } ) ;
464
+ }
465
+ const diagnostics = getDiagnosticsOfProgram ( program ) ;
466
+ const projectName = getConfigFilePathFromCompilerOptions ( program . getCompilerOptions ( ) ) ;
467
+
468
+ // update diagnostics
469
+ diagnosticsPerProject . set ( projectName , diagnostics ) ;
443
470
}
444
471
445
472
changedFiles . forEach ( ( changedFile ) => {
0 commit comments