@@ -32,6 +32,42 @@ function getComposeSourceMapsPath(): string {
3232 return composeSourceMapsPath ;
3333}
3434
35+ /**
36+ * Extracts debugId from sourcemap file.
37+ * @see https://github.com/tc39/ecma426/blob/main/proposals/debug-id.md
38+ * @param sourceMapPath - Sourcemap file path
39+ * @returns debugId value. Returns null if extraction fails
40+ */
41+ function extractDebugId ( sourceMapPath : string ) : string | null {
42+ try {
43+ const sourceMapContent = fs . readFileSync ( sourceMapPath , 'utf-8' ) ;
44+ const sourceMap = JSON . parse ( sourceMapContent ) ;
45+ return sourceMap . debugId ;
46+ } catch {
47+ return null ;
48+ }
49+ }
50+
51+ /**
52+ * Inject debugId into sourcemap file at the top level.
53+ * @see https://github.com/tc39/ecma426/blob/main/proposals/debug-id.md
54+ * @param sourceMapPath - Sourcemap file path
55+ * @param debugId - debugId value to inject
56+ * @throws {RnefError } Throws an error if injection fails
57+ */
58+ function injectDebugId ( sourceMapPath : string , debugId : string ) {
59+ try {
60+ const sourceMapContent = fs . readFileSync ( sourceMapPath , 'utf-8' ) ;
61+ const sourceMap = JSON . parse ( sourceMapContent ) ;
62+ sourceMap . debugId = debugId ;
63+ fs . writeFileSync ( sourceMapPath , JSON . stringify ( sourceMap ) ) ;
64+ } catch {
65+ throw new RnefError (
66+ `Failed to inject debugId into sourcemap: ${ sourceMapPath } `
67+ ) ;
68+ }
69+ }
70+
3571export async function runHermes ( {
3672 bundleOutputPath,
3773 sourcemapOutputPath,
@@ -79,13 +115,22 @@ export async function runHermes({
79115 const composeSourceMapsPath = getComposeSourceMapsPath ( ) ;
80116
81117 try {
118+ // Extract debugId from original sourcemap
119+ const debugId = extractDebugId ( sourcemapOutputPath ) ;
120+
82121 await spawn ( 'node' , [
83122 composeSourceMapsPath ,
84123 sourcemapOutputPath ,
85124 hermesSourceMapFile ,
86125 '-o' ,
87126 sourcemapOutputPath ,
88127 ] ) ;
128+
129+ // Inject debugId back into the composed sourcemap
130+ if ( debugId ) {
131+ injectDebugId ( sourcemapOutputPath , debugId ) ;
132+ }
133+
89134 } catch ( error ) {
90135 throw new RnefError (
91136 'Failed to run compose-source-maps script' ,
0 commit comments