@@ -18,6 +18,13 @@ import {
1818import { createPerformance } from '../../profile/Performance' ;
1919import { connectTypeScriptPerformance } from '../profile/TypeScriptPerformance' ;
2020
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+
2128function createTypeScriptReporter ( configuration : TypeScriptReporterConfiguration ) : Reporter {
2229 let parsedConfiguration : ts . ParsedCommandLine | undefined ;
2330 let parseConfigurationDiagnostics : ts . Diagnostic [ ] = [ ] ;
@@ -49,8 +56,17 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
4956 extensions . push ( createTypeScriptVueExtension ( configuration . extensions . vue ) ) ;
5057 }
5158
59+ function getConfigFilePathFromCompilerOptions ( compilerOptions : ts . CompilerOptions ) : string {
60+ return ( compilerOptions . configFilePath as unknown ) as string ;
61+ }
62+
5263 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 ;
5470 }
5571
5672 function getDiagnosticsOfBuilderProgram ( builderProgram : ts . BuilderProgram ) {
@@ -152,6 +168,49 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
152168 ) ;
153169 }
154170
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+
155214 return {
156215 getReport : async ( { changedFiles = [ ] , deletedFiles = [ ] } ) => {
157216 // clear cache to be ready for next iteration and to free memory
@@ -227,9 +286,7 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
227286 return dependencies ;
228287 } ,
229288 async getIssues ( ) {
230- if ( configuration . profile ) {
231- performance . enable ( ) ;
232- }
289+ startProfilingIfNeeded ( ) ;
233290
234291 parsedConfiguration = parseConfigurationIfNeeded ( ) ;
235292
@@ -261,7 +318,26 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
261318 typescript ,
262319 parsedConfiguration ,
263320 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+ } ,
265341 undefined ,
266342 undefined ,
267343 undefined ,
@@ -275,6 +351,8 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
275351
276352 // emit .tsbuildinfo file if needed
277353 emitTsBuildInfoFileForBuilderProgram ( builderProgram ) ;
354+
355+ stopTracingIfNeeded ( builderProgram ) ;
278356 } ,
279357 extensions
280358 ) ;
@@ -308,7 +386,26 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
308386 typescript ,
309387 parsedConfiguration ,
310388 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+ } ,
312409 undefined ,
313410 undefined ,
314411 ( builderProgram ) => {
@@ -320,6 +417,8 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
320417
321418 // emit .tsbuildinfo file if needed
322419 emitTsBuildInfoFileForBuilderProgram ( builderProgram ) ;
420+
421+ stopTracingIfNeeded ( builderProgram ) ;
323422 } ,
324423 extensions
325424 ) ;
@@ -370,10 +469,8 @@ function createTypeScriptReporter(configuration: TypeScriptReporterConfiguration
370469 }
371470 } ) ;
372471
373- if ( configuration . profile ) {
374- performance . print ( ) ;
375- performance . disable ( ) ;
376- }
472+ dumpTracingLegendIfNeeded ( ) ;
473+ stopProfilingIfNeeded ( ) ;
377474
378475 return issues ;
379476 } ,
0 commit comments