Skip to content

Commit e5e6a31

Browse files
authored
feat: extract and inject debugId in hermes sourcemaps (#410)
* feat: extract and inject debug_id in hermes sourcemaps * fix * chore: changeset * fix
1 parent 02d0a0b commit e5e6a31

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

.changeset/shiny-apes-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rnef/tools': patch
3+
---
4+
5+
feat: extract and inject debugId in hermes sourcemaps

packages/tools/src/lib/hermes.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3571
export 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

Comments
 (0)