@@ -18,6 +18,13 @@ import {
18
18
import { createPerformance } from '../../profile/Performance' ;
19
19
import { connectTypeScriptPerformance } from '../profile/TypeScriptPerformance' ;
20
20
21
+ // write this type as it's available only in the newest TypeScript versions (^4.1.0)
22
+ interface Tracing {
23
+ startTracing ( configFilePath : string , traceDirPath : string , isBuildMode : boolean ) : void ;
24
+ stopTracing ( typeCatalog : unknown ) : void ;
25
+ dumpLegend ( ) : void ;
26
+ }
27
+
21
28
function createTypeScriptReporter ( configuration : TypeScriptReporterConfiguration ) : Reporter {
22
29
let parsedConfiguration : ts . ParsedCommandLine | undefined ;
23
30
let parseConfigurationDiagnostics : ts . Diagnostic [ ] = [ ] ;
@@ -49,8 +56,17 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
49
56
extensions . push ( createTypeScriptVueExtension ( configuration . extensions . vue ) ) ;
50
57
}
51
58
59
+ function getConfigFilePathFromCompilerOptions ( compilerOptions : ts . CompilerOptions ) : string {
60
+ return ( compilerOptions . configFilePath as unknown ) as string ;
61
+ }
62
+
52
63
function getProjectNameOfBuilderProgram ( builderProgram : ts . BuilderProgram ) : string {
53
- return ( builderProgram . getProgram ( ) . getCompilerOptions ( ) . configFilePath as unknown ) as string ;
64
+ return getConfigFilePathFromCompilerOptions ( builderProgram . getProgram ( ) . getCompilerOptions ( ) ) ;
65
+ }
66
+
67
+ function getTracing ( ) : Tracing | undefined {
68
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
+ return ( typescript as any ) . tracing ;
54
70
}
55
71
56
72
function getDiagnosticsOfBuilderProgram ( builderProgram : ts . BuilderProgram ) {
@@ -152,6 +168,49 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
152
168
) ;
153
169
}
154
170
171
+ function startProfilingIfNeeded ( ) {
172
+ if ( configuration . profile ) {
173
+ performance . enable ( ) ;
174
+ }
175
+ }
176
+
177
+ function stopProfilingIfNeeded ( ) {
178
+ if ( configuration . profile ) {
179
+ performance . print ( ) ;
180
+ performance . disable ( ) ;
181
+ }
182
+ }
183
+
184
+ function startTracingIfNeeded ( compilerOptions : ts . CompilerOptions ) {
185
+ const tracing = getTracing ( ) ;
186
+
187
+ if ( compilerOptions . generateTrace && tracing ) {
188
+ tracing . startTracing (
189
+ getConfigFilePathFromCompilerOptions ( compilerOptions ) ,
190
+ compilerOptions . generateTrace as string ,
191
+ configuration . build
192
+ ) ;
193
+ }
194
+ }
195
+
196
+ function stopTracingIfNeeded ( program : ts . BuilderProgram ) {
197
+ const tracing = getTracing ( ) ;
198
+ const compilerOptions = program . getCompilerOptions ( ) ;
199
+
200
+ if ( compilerOptions . generateTrace && tracing ) {
201
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
202
+ tracing . stopTracing ( ( program . getProgram ( ) as any ) . getTypeCatalog ( ) ) ;
203
+ }
204
+ }
205
+
206
+ function dumpTracingLegendIfNeeded ( ) {
207
+ const tracing = getTracing ( ) ;
208
+
209
+ if ( tracing ) {
210
+ tracing . dumpLegend ( ) ;
211
+ }
212
+ }
213
+
155
214
return {
156
215
getReport : async ( { changedFiles = [ ] , deletedFiles = [ ] } ) => {
157
216
// clear cache to be ready for next iteration and to free memory
@@ -227,9 +286,7 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
227
286
return dependencies ;
228
287
} ,
229
288
async getIssues ( ) {
230
- if ( configuration . profile ) {
231
- performance . enable ( ) ;
232
- }
289
+ startProfilingIfNeeded ( ) ;
233
290
234
291
parsedConfiguration = parseConfigurationIfNeeded ( ) ;
235
292
@@ -261,7 +318,26 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
261
318
typescript ,
262
319
parsedConfiguration ,
263
320
system ,
264
- typescript . createSemanticDiagnosticsBuilderProgram ,
321
+ (
322
+ rootNames ,
323
+ compilerOptions ,
324
+ host ,
325
+ oldProgram ,
326
+ configFileParsingDiagnostics ,
327
+ projectReferences
328
+ ) => {
329
+ if ( compilerOptions ) {
330
+ startTracingIfNeeded ( compilerOptions ) ;
331
+ }
332
+ return typescript . createSemanticDiagnosticsBuilderProgram (
333
+ rootNames ,
334
+ compilerOptions ,
335
+ host ,
336
+ oldProgram ,
337
+ configFileParsingDiagnostics ,
338
+ projectReferences
339
+ ) ;
340
+ } ,
265
341
undefined ,
266
342
undefined ,
267
343
undefined ,
@@ -275,6 +351,8 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
275
351
276
352
// emit .tsbuildinfo file if needed
277
353
emitTsBuildInfoFileForBuilderProgram ( builderProgram ) ;
354
+
355
+ stopTracingIfNeeded ( builderProgram ) ;
278
356
} ,
279
357
extensions
280
358
) ;
@@ -308,7 +386,26 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
308
386
typescript ,
309
387
parsedConfiguration ,
310
388
system ,
311
- typescript . createSemanticDiagnosticsBuilderProgram ,
389
+ (
390
+ rootNames ,
391
+ compilerOptions ,
392
+ host ,
393
+ oldProgram ,
394
+ configFileParsingDiagnostics ,
395
+ projectReferences
396
+ ) => {
397
+ if ( compilerOptions ) {
398
+ startTracingIfNeeded ( compilerOptions ) ;
399
+ }
400
+ return typescript . createSemanticDiagnosticsBuilderProgram (
401
+ rootNames ,
402
+ compilerOptions ,
403
+ host ,
404
+ oldProgram ,
405
+ configFileParsingDiagnostics ,
406
+ projectReferences
407
+ ) ;
408
+ } ,
312
409
undefined ,
313
410
undefined ,
314
411
( builderProgram ) => {
@@ -320,6 +417,8 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
320
417
321
418
// emit .tsbuildinfo file if needed
322
419
emitTsBuildInfoFileForBuilderProgram ( builderProgram ) ;
420
+
421
+ stopTracingIfNeeded ( builderProgram ) ;
323
422
} ,
324
423
extensions
325
424
) ;
@@ -370,10 +469,8 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
370
469
}
371
470
} ) ;
372
471
373
- if ( configuration . profile ) {
374
- performance . print ( ) ;
375
- performance . disable ( ) ;
376
- }
472
+ dumpTracingLegendIfNeeded ( ) ;
473
+ stopProfilingIfNeeded ( ) ;
377
474
378
475
return issues ;
379
476
} ,
0 commit comments