11import { posix } from "node:path" ;
22import { domainToASCII , fileURLToPath , pathToFileURL } from "node:url" ;
3+ import { TraceMap } from "@jridgewell/trace-mapping" ;
34
45import { z } from "zod" ;
56
@@ -25,22 +26,64 @@ export type JavaScriptSourceMapReference = z.infer<
2526 typeof javascriptSourceMapReferenceSchema
2627> ;
2728
28- /** Interpret a declaration once at the source-map ingestion boundary. */
29- export const resolveJavaScriptSourceMapReference = (
30- sourceName : string ,
29+ /** Interpret declarations together once at their source-map ingestion boundary. */
30+ export const resolveJavaScriptSourceMapReferences = (
31+ sourceNames : readonly ( string | null ) [ ] ,
3132 sourceRoot : string | null ,
3233 mapPath : string ,
33- ) : JavaScriptSourceMapReference => {
34- // TraceMap's producer prefixes sourceRoot before resolving the URL reference.
35- const reference = `${ sourceRoot ? `${ sourceRoot } /` : "" } ${ sourceName } ` ;
36- return {
37- source_name : sourceName ,
38- source_root : sourceRoot ,
39- map_path : mapPath ,
40- resolution : resolveJavaScriptSourceMapPath ( reference , mapPath ) ,
41- } ;
34+ ) : ( JavaScriptSourceMapReference | null ) [ ] => {
35+ if ( sourceNames . length === 0 ) return [ ] ;
36+ const resolvedSources = resolveSourceMapUrls (
37+ sourceNames . map ( ( sourceName ) => sourceName ?? "" ) ,
38+ sourceRoot ,
39+ "" ,
40+ ) ;
41+ return sourceNames . map ( ( sourceName , index ) => {
42+ if ( sourceName === null ) return null ;
43+ const reference = resolvedSources [ index ] ;
44+ if ( reference === undefined )
45+ throw new TypeError (
46+ `TraceMap did not resolve source declaration ${ String ( index ) } ` ,
47+ ) ;
48+ return {
49+ source_name : sourceName ,
50+ source_root : sourceRoot ,
51+ map_path : mapPath ,
52+ resolution : resolveJavaScriptSourceMapPath ( reference , mapPath ) ,
53+ } ;
54+ } ) ;
4255} ;
4356
57+ /** Resolve a source declaration using the same pinned format implementation as map consumers. */
58+ export const resolveJavaScriptSourceMapUrl = (
59+ source : string | null ,
60+ sourceRoot : string | null | undefined ,
61+ mapUrl : string ,
62+ ) : string => {
63+ const resolved = resolveSourceMapUrls ( [ source ?? "" ] , sourceRoot , mapUrl ) [ 0 ] ;
64+ if ( resolved === undefined )
65+ throw new TypeError (
66+ "TraceMap did not resolve its single source declaration" ,
67+ ) ;
68+ return resolved ;
69+ } ;
70+
71+ const resolveSourceMapUrls = (
72+ sources : string [ ] ,
73+ sourceRoot : string | null | undefined ,
74+ mapUrl : string ,
75+ ) : readonly string [ ] =>
76+ new TraceMap (
77+ {
78+ version : 3 ,
79+ names : [ ] ,
80+ sources,
81+ ...( sourceRoot == null ? { } : { sourceRoot } ) ,
82+ mappings : "" ,
83+ } ,
84+ mapUrl ,
85+ ) . resolvedSources ;
86+
4487/** Resolve a source-map URL reference without changing its recorded identity. */
4588const resolveJavaScriptSourceMapPath = (
4689 source : string ,
0 commit comments