@@ -2,6 +2,7 @@ import { readFile } from 'fs/promises';
22import { createHash } from 'node:crypto' ;
33import { access , writeFile } from 'node:fs/promises' ;
44import remapping , { SourceMapInput } from '@ampproject/remapping' ;
5+ import path from 'path' ;
56
67type FileName = string ;
78type SourceMapLookup = Record < FileName , SourceMapInput > ;
@@ -73,3 +74,35 @@ export async function saveBuildData(
7374 throw error ;
7475 }
7576}
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