@@ -2,7 +2,7 @@ import { existsSync } from "fs";
22import { isAbsolute , join , relative , resolve } from "path" ;
33import ts from "typescript" ;
44import type { JSONOutput , Serializer } from "../../serialization/index" ;
5- import { readFile } from "../../utils/fs" ;
5+ import { getCommonDirectory , readFile } from "../../utils/fs" ;
66import { getQualifiedName } from "../../utils/tsutils" ;
77import { optional , validate } from "../../utils/validation" ;
88import { normalizePath } from "../../utils/paths" ;
@@ -61,27 +61,26 @@ export class ReflectionSymbolId {
6161 }
6262
6363 toObject ( serializer : Serializer ) {
64+ const sourceFileName = isAbsolute ( this . fileName )
65+ ? normalizePath (
66+ relative (
67+ serializer . projectRoot ,
68+ resolveDeclarationMaps ( this . fileName ) ,
69+ ) ,
70+ )
71+ : this . fileName ;
72+
6473 return {
65- sourceFileName : isAbsolute ( this . fileName )
66- ? normalizePath (
67- relative (
68- serializer . projectRoot ,
69- resolveDeclarationMaps ( this . fileName ) ,
70- ) ,
71- )
72- : this . fileName ,
74+ sourceFileName,
7375 qualifiedName : this . qualifiedName ,
7476 } ;
7577 }
7678}
7779
7880const declarationMapCache = new Map < string , string > ( ) ;
7981
80- /**
81- * See also getTsSourceFromJsSource in package-manifest.ts.
82- */
8382function resolveDeclarationMaps ( file : string ) : string {
84- if ( ! file . endsWith ( ".d.ts" ) ) return file ;
83+ if ( ! / \. d \. [ c m ] ? t s $ / . test ( file ) ) return file ;
8584 if ( declarationMapCache . has ( file ) ) return declarationMapCache . get ( file ) ! ;
8685
8786 const mapFile = file + ".map" ;
@@ -123,3 +122,19 @@ function resolveDeclarationMaps(file: string): string {
123122
124123 return file ;
125124}
125+
126+ export function addInferredDeclarationMapPaths (
127+ opts : ts . CompilerOptions ,
128+ files : readonly string [ ] ,
129+ ) {
130+ const rootDir = opts . rootDir || getCommonDirectory ( files ) ;
131+ const declDir = opts . declarationDir || opts . outDir || rootDir ;
132+
133+ for ( const file of files ) {
134+ const mapFile = resolve ( declDir , relative ( rootDir , file ) ) . replace (
135+ / \. ( [ c m ] ? [ t j ] s ) x ? $ / ,
136+ ".d.$1" ,
137+ ) ;
138+ declarationMapCache . set ( mapFile , file ) ;
139+ }
140+ }
0 commit comments