Skip to content

Commit cbfcfd0

Browse files
authored
fix: hermes-compiler path for RN 0.82+ in the hermes bytecode plugin (#1332)
1 parent 75f1017 commit cbfcfd0

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

.changeset/cuddly-months-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Fix hermes-compiler path for RN 0.82+ in the hermes bytecode plugin

packages/repack/src/plugins/HermesBytecodePlugin/utils/getHermesCLIPath.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs';
12
import os from 'node:os';
23
import path from 'node:path';
34

@@ -20,7 +21,7 @@ const getHermesOSBin = (): string | null => {
2021
/**
2122
* Determines the path to the Hermes compiler binary.
2223
*
23-
* Defaults to './node_modules/react-native/sdks/hermesc/{os-bin}/hermesc'
24+
* Defaults to './node_modules/hermes-compiler/hermesc/{os-bin}/hermesc'
2425
*/
2526
export const getHermesCLIPath = (reactNativePath: string): string => {
2627
const osBin = getHermesOSBin();
@@ -32,5 +33,19 @@ export const getHermesCLIPath = (reactNativePath: string): string => {
3233
);
3334
}
3435

36+
const hermesCompilerPath = path.join(
37+
reactNativePath,
38+
'..',
39+
'hermes-compiler',
40+
'hermesc',
41+
osBin,
42+
'hermesc'
43+
);
44+
45+
if (fs.existsSync(hermesCompilerPath)) {
46+
return hermesCompilerPath;
47+
}
48+
49+
// Fallback to the previous hermesc path in older react native versions, <0.82.
3550
return path.join(reactNativePath, 'sdks', 'hermesc', osBin, 'hermesc');
3651
};

packages/repack/src/plugins/__tests__/HermesBytecodePlugin.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import fs from 'node:fs';
2+
import os from 'node:os';
23
import { type Compiler, ModuleFilenameHelpers } from '@rspack/core';
34
import execa from 'execa';
45
import { HermesBytecodePlugin } from '../HermesBytecodePlugin/index.js';
6+
import { getHermesCLIPath } from '../HermesBytecodePlugin/utils/getHermesCLIPath.js';
57

68
jest.mock('node:fs', () => ({
79
__esModule: true,
810
default: {
11+
existsSync: jest.fn(),
912
promises: {
1013
access: jest.fn(),
1114
rename: jest.fn(),
@@ -142,4 +145,30 @@ describe('HermesBytecodePlugin', () => {
142145
expect(execaMock.mock.calls[0][0]).toEqual('path/to/hermesc');
143146
});
144147
});
148+
149+
describe('getHermesCLIPath', () => {
150+
const reactNativePath = 'path/to/react-native';
151+
152+
it('returns new hermes-compiler path when it exists', () => {
153+
jest.spyOn(os, 'platform').mockReturnValue('darwin');
154+
jest.mocked(fs.existsSync).mockReturnValue(true);
155+
156+
const hermesPath = getHermesCLIPath(reactNativePath);
157+
158+
expect(hermesPath).toBe(
159+
'path/to/hermes-compiler/hermesc/osx-bin/hermesc'
160+
);
161+
});
162+
163+
it('falls back to legacy hermesc path when hermes-compiler does not exist', () => {
164+
jest.spyOn(os, 'platform').mockReturnValue('darwin');
165+
jest.mocked(fs.existsSync).mockReturnValue(false);
166+
167+
const hermesPath = getHermesCLIPath(reactNativePath);
168+
169+
expect(hermesPath).toBe(
170+
'path/to/react-native/sdks/hermesc/osx-bin/hermesc'
171+
);
172+
});
173+
});
145174
});

0 commit comments

Comments
 (0)