@@ -2,6 +2,7 @@ import { readFile } from 'fs/promises';
2
2
import { createHash } from 'node:crypto' ;
3
3
import { access , writeFile } from 'node:fs/promises' ;
4
4
import remapping , { SourceMapInput } from '@ampproject/remapping' ;
5
+ import path from 'path' ;
5
6
6
7
type FileName = string ;
7
8
type SourceMapLookup = Record < FileName , SourceMapInput > ;
@@ -73,3 +74,35 @@ export async function saveBuildData(
73
74
throw error ;
74
75
}
75
76
}
77
+
78
+ export async function getSourceMapFromFile ( filePath : string ) : Promise < SourceMapInput | null > {
79
+ try {
80
+ const content = await readFile ( filePath , 'utf8' ) ;
81
+
82
+ // Look for the sourceMappingURL comment
83
+ const sourceMapRegex = / \/ \/ [ # @ ] \s * s o u r c e M a p p i n g U R L = ( .+ ) $ / m;
84
+ const match = content . match ( sourceMapRegex ) ;
85
+
86
+ if ( ! match ) return null ;
87
+
88
+ const sourceMapUrl = match [ 1 ] . trim ( ) ;
89
+
90
+ if ( sourceMapUrl . startsWith ( 'data:application/json;base64,' ) ) {
91
+ // Inline base64-encoded source map
92
+ const base64 = sourceMapUrl . slice ( 'data:application/json;base64,' . length ) ;
93
+ const rawMap = Buffer . from ( base64 , 'base64' ) . toString ( 'utf8' ) ;
94
+ return JSON . parse ( rawMap ) ;
95
+ } else {
96
+ // External .map file
97
+ const mapPath = path . resolve ( path . dirname ( filePath ) , sourceMapUrl ) ;
98
+ try {
99
+ const rawMap = await readFile ( mapPath , 'utf8' ) ;
100
+ return JSON . parse ( rawMap ) ;
101
+ } catch ( e ) {
102
+ return null ; // map file not found or invalid
103
+ }
104
+ }
105
+ } catch ( err ) {
106
+ return null ; // file doesn't exist or can't be read
107
+ }
108
+ }
0 commit comments