Skip to content

Commit e45fc32

Browse files
gronxbthymikee
andauthored
feat: support hermes sourcemap compose (#404)
* feat: hermes compose sourcemap * chore: changeset * fix: filename * feat: ensure compose source map path * feat: cleanup * fix * feat: throw in getComposeSourceMapsPath * fix: unused * fix: unused code * chore: changeset * Update .changeset/violet-readers-yell.md --------- Co-authored-by: Michał Pierzchała <[email protected]>
1 parent d509636 commit e45fc32

File tree

6 files changed

+90
-5
lines changed

6 files changed

+90
-5
lines changed

.changeset/violet-readers-yell.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@rnef/platform-apple-helpers': patch
3+
'@rnef/platform-android': patch
4+
'@rnef/plugin-repack': patch
5+
'@rnef/plugin-metro': patch
6+
'@rnef/tools': patch
7+
---
8+
9+
feat: support hermes sourcemap compose

packages/platform-android/src/lib/commands/signAndroid/bundle.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,8 @@ export async function buildJsBundle(options: BuildJsBundleOptions) {
6464
return;
6565
}
6666

67-
await runHermes({ bundleOutputPath: options.bundleOutputPath });
67+
await runHermes({
68+
bundleOutputPath: options.bundleOutputPath,
69+
sourcemapOutputPath: options.sourcemapOutputPath,
70+
});
6871
}

packages/platform-apple-helpers/src/lib/commands/sign/bundle.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type BuildJsBundleOptions = {
1212
bundleOutputPath: string;
1313
assetsDestPath: string;
1414
useHermes?: boolean;
15+
sourcemapOutputPath?: string;
1516
};
1617

1718
/**
@@ -41,6 +42,7 @@ export async function buildJsBundle(options: BuildJsBundleOptions) {
4142
options.bundleOutputPath,
4243
'--assets-dest',
4344
options.assetsDestPath,
45+
...(options.sourcemapOutputPath ? ['--sourcemap-output', options.sourcemapOutputPath] : []),
4446
];
4547
try {
4648
await spawn('rnef', rnefBundleArgs, { preferLocal: true });
@@ -54,5 +56,8 @@ export async function buildJsBundle(options: BuildJsBundleOptions) {
5456
return;
5557
}
5658

57-
await runHermes({ bundleOutputPath: options.bundleOutputPath });
59+
await runHermes({
60+
bundleOutputPath: options.bundleOutputPath,
61+
sourcemapOutputPath: options.sourcemapOutputPath,
62+
});
5863
}

packages/plugin-metro/src/lib/bundle/command.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ export function registerBundleCommand(api: PluginApi) {
6262
if (args.hermes) {
6363
const loader = spinner();
6464
loader.start('Running Hermes compiler...');
65-
await runHermes({ bundleOutputPath: args.bundleOutput });
65+
await runHermes({
66+
bundleOutputPath: args.bundleOutput,
67+
sourcemapOutputPath: args.sourcemapOutput,
68+
});
6669
loader.stop(
6770
`Hermes bytecode bundle created at: ${color.cyan(args.bundleOutput)}`
6871
);

packages/plugin-repack/src/lib/pluginRepack.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ export const pluginRepack =
9090

9191
const loader = spinner();
9292
loader.start('Running Hermes compiler...');
93-
await runHermes({ bundleOutputPath: args.bundleOutput });
93+
await runHermes({
94+
bundleOutputPath: args.bundleOutput,
95+
sourcemapOutputPath: args.sourcemapOutput,
96+
});
9497
loader.stop(
9598
`Hermes bytecode bundle created at: ${color.cyan(
9699
args.bundleOutput

packages/tools/src/lib/hermes.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,30 @@ function getReactNativePackagePath() {
1414
return path.dirname(input);
1515
}
1616

17+
/**
18+
* Returns the path to the react-native compose-source-maps.js script.
19+
*/
20+
function getComposeSourceMapsPath(): string {
21+
const rnPackagePath = getReactNativePackagePath();
22+
const composeSourceMapsPath = path.join(
23+
rnPackagePath,
24+
'scripts',
25+
'compose-source-maps.js',
26+
);
27+
if (!fs.existsSync(composeSourceMapsPath)) {
28+
throw new RnefError(
29+
"Could not find react-native's compose-source-maps.js script."
30+
);
31+
}
32+
return composeSourceMapsPath;
33+
}
34+
1735
export async function runHermes({
1836
bundleOutputPath,
37+
sourcemapOutputPath,
1938
}: {
2039
bundleOutputPath: string;
40+
sourcemapOutputPath?: string;
2141
}) {
2242
const hermescPath = getHermescPath();
2343
if (!hermescPath) {
@@ -26,15 +46,24 @@ export async function runHermes({
2646
);
2747
}
2848

49+
// Output will be .hbc file
50+
const hbcOutputPath = `${bundleOutputPath}.hbc`;
51+
2952
const hermescArgs = [
3053
'-emit-binary',
3154
'-max-diagnostic-width=80',
3255
'-O',
3356
'-w',
3457
'-out',
35-
bundleOutputPath, // Needs `-out` path, or otherwise outputs to stdout
58+
hbcOutputPath,
3659
bundleOutputPath,
3760
];
61+
62+
// Add sourcemap flag if enabled
63+
if (sourcemapOutputPath) {
64+
hermescArgs.push('-output-source-map');
65+
}
66+
3867
try {
3968
await spawn(hermescPath, hermescArgs);
4069
} catch (error) {
@@ -43,6 +72,39 @@ export async function runHermes({
4372
{ cause: (error as SubprocessError).stderr }
4473
);
4574
}
75+
76+
// Handle sourcemap composition if enabled
77+
if (sourcemapOutputPath) {
78+
const hermesSourceMapFile = `${hbcOutputPath}.map`;
79+
const composeSourceMapsPath = getComposeSourceMapsPath();
80+
81+
try {
82+
await spawn('node', [
83+
composeSourceMapsPath,
84+
sourcemapOutputPath,
85+
hermesSourceMapFile,
86+
'-o',
87+
sourcemapOutputPath,
88+
]);
89+
} catch (error) {
90+
throw new RnefError(
91+
'Failed to run compose-source-maps script',
92+
{ cause: (error as SubprocessError).stderr }
93+
);
94+
}
95+
}
96+
97+
// Move .hbc file to overwrite the original bundle file
98+
try {
99+
if (fs.existsSync(bundleOutputPath)) {
100+
fs.unlinkSync(bundleOutputPath);
101+
}
102+
fs.renameSync(hbcOutputPath, bundleOutputPath);
103+
} catch (error) {
104+
throw new RnefError(
105+
`Failed to move compiled Hermes bytecode to bundle output path: ${error}`
106+
);
107+
}
46108
}
47109

48110
/**

0 commit comments

Comments
 (0)