@@ -24,6 +24,7 @@ export interface Options {
2424 project ?: string ;
2525 display ?: Display ;
2626 emit ?: boolean ;
27+ generateTrace ?: string ;
2728}
2829
2930interface Report {
@@ -45,8 +46,8 @@ const isSourceMapExtensionReg = /\.map$/;
4546const skipRemapExtensionsReg =
4647 / \. (?: [ c m ] ? j s x ? | j s o n | m a r k o | c s s | l e s s | s a s s | s c s s | s t y l | s t y l u s | p c s s | p o s t c s s | s s s | a ? p n g | j p e ? g | j f i f | p i p e g | p j p | g i f | s v g | i c o | w e b [ p m ] | a v i f | m p 4 | o g g | m p 3 | w a v | f l a c | a a c | o p u s | w o f f 2 ? | e o t | [ o t ] t f | w e b m a n i f e s t | p d f | t x t ) $ / ;
4748
48- const extractCache = new WeakMap <
49- ts . SourceFile ,
49+ const extractCache = new Map <
50+ string ,
5051 ReturnType < Processors . Processor [ "extract" ] >
5152> ( ) ;
5253const requiredTSCompilerOptions : ts . CompilerOptions = {
@@ -66,8 +67,55 @@ export default function run(opts: Options) {
6667 findRootConfigFile ( "jsconfig.json" ) ,
6768 } = opts ;
6869
69- if ( ! configFile )
70+ if ( ! configFile ) {
7071 throw new Error ( "Could not find tsconfig.json or jsconfig.json" ) ;
72+ }
73+
74+ if ( opts . generateTrace ) {
75+ ( ts as any ) . startTracing ?.(
76+ "build" ,
77+ path . resolve ( opts . generateTrace ) ,
78+ configFile ,
79+ ) ;
80+ const tracing = ( ts as any ) . tracing ;
81+ if ( ! tracing ) {
82+ throw new Error ( "generateTrace not available in TypeScript compiler." ) ;
83+ }
84+
85+ const { push } = tracing ;
86+ tracing . push = (
87+ phase : unknown ,
88+ name : unknown ,
89+ args ?: unknown ,
90+ separateBeginAndEnd ?: unknown ,
91+ ) => {
92+ if (
93+ args &&
94+ typeof args === "object" &&
95+ "pos" in args &&
96+ typeof args . pos === "number" &&
97+ "end" in args &&
98+ typeof args . end === "number"
99+ ) {
100+ const fileName =
101+ "path" in args
102+ ? args . path
103+ : "fileName" in args
104+ ? args . fileName
105+ : undefined ;
106+ if ( typeof fileName === "string" ) {
107+ const extracted = extractCache . get ( getCanonicalFileName ( fileName ) ) ;
108+ if ( extracted ) {
109+ ( args as any ) . generatedPos = args . pos ;
110+ ( args as any ) . generatedEnd = args . end ;
111+ args . pos = extracted . sourceOffsetAt ( args . pos ) ;
112+ args . end = extracted . sourceOffsetAt ( args . end ) ;
113+ }
114+ }
115+ }
116+ return push . call ( tracing , phase , name , args , separateBeginAndEnd ) ;
117+ } ;
118+ }
71119
72120 const formatSettings = ts . getDefaultFormatCodeSettings (
73121 ts . sys . newLine ,
@@ -332,7 +380,7 @@ export default function run(opts: Options) {
332380 . createHash ( "md5" )
333381 . update ( extractedCode )
334382 . digest ( "hex" ) ;
335- extractCache . set ( sourceFile , extracted ) ;
383+ extractCache . set ( getCanonicalFileName ( fileName ) , extracted ) ;
336384 return sourceFile ;
337385 }
338386
@@ -433,7 +481,7 @@ export default function run(opts: Options) {
433481 }
434482
435483 const extracted = extractCache . get (
436- program . getSourceFile ( sourceFile . fileName ) ! ,
484+ getCanonicalFileName ( sourceFile . fileName ) ,
437485 ) ! ;
438486 const printContext : Processors . PrintContext = {
439487 extracted,
@@ -542,7 +590,9 @@ function reportDiagnostic(report: Report, diag: ts.Diagnostic) {
542590 let loc : Location | void = undefined ;
543591
544592 if ( diag . start !== undefined ) {
545- const extracted = extractCache . get ( diag . file ) ;
593+ const extracted = extractCache . get (
594+ getCanonicalFileName ( diag . file . fileName ) ,
595+ ) ;
546596
547597 if ( extracted ) {
548598 loc = extracted . sourceLocationAt (
0 commit comments