Skip to content

Commit 7db4e41

Browse files
tringenbachjbroma
andauthored
fix: add react-native to byDependency (#1185)
Co-authored-by: Jakub Romańczyk <lorczyslav@gmail.com> Co-authored-by: Jakub Romanczyk <jakub.romanczyk@callstack.com>
1 parent 023a4a9 commit 7db4e41

File tree

4 files changed

+39
-47
lines changed

4 files changed

+39
-47
lines changed

.changeset/better-views-stay.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+
Resolve `react-native` package exports condition before `import` or `require`

packages/repack/src/utils/getResolveOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ export function getResolveOptions(
115115
* and they need condition names to be defined.
116116
*/
117117
const byDependency = {
118-
esm: { conditionNames: ['import'] },
119-
commonjs: { conditionNames: ['require'] },
118+
esm: { conditionNames: ['react-native', 'import'] },
119+
commonjs: { conditionNames: ['react-native', 'require'] },
120120
};
121121

122122
/**

tests/metro-compat/resolver/resolve.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,9 @@ export function resolve(
149149
// this is equivalent to "byDependency" configuration in rspack/webpack
150150
// enhanced-resolve does not use "byDependency" configuration
151151
if (metroContext.isESMImport) {
152-
resolveOptions.conditionNames?.push(
153-
...resolutionPreset.byDependency.esm.conditionNames
154-
);
152+
resolveOptions.conditionNames?.push('import');
155153
} else {
156-
resolveOptions.conditionNames?.push(
157-
...resolutionPreset.byDependency.commonjs.conditionNames
158-
);
154+
resolveOptions.conditionNames?.push('require');
159155
}
160156

161157
const resolve = enhancedResolve.create.sync(resolveOptions);

tests/resolver-cases/src/__tests__/exports-resolution.test.ts

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,25 @@ import { describe, expect, test } from 'vitest';
22
import { setupTestEnvironment } from '../test-helpers.js';
33

44
describe('Package Exports Resolution', () => {
5-
test.fails(
6-
'should resolve ESM imports with react-native condition',
7-
async () => {
8-
const { resolve } = await setupTestEnvironment(['exports'], {
9-
platform: 'ios',
10-
enablePackageExports: true,
11-
});
5+
test('should resolve ESM imports with react-native condition', async () => {
6+
const { resolve } = await setupTestEnvironment(['exports'], {
7+
platform: 'ios',
8+
enablePackageExports: true,
9+
});
1210

13-
const result = await resolve('complex-lib', 'esm');
14-
expect(result).toBe('/node_modules/complex-lib/esm/index.native.js');
15-
}
16-
);
11+
const result = await resolve('complex-lib', 'esm');
12+
expect(result).toBe('/node_modules/complex-lib/esm/index.native.js');
13+
});
1714

18-
test.fails(
19-
'should resolve CommonJS requires with react-native condition',
20-
async () => {
21-
const { resolve } = await setupTestEnvironment(['exports'], {
22-
platform: 'ios',
23-
enablePackageExports: true,
24-
});
15+
test('should resolve CommonJS requires with react-native condition', async () => {
16+
const { resolve } = await setupTestEnvironment(['exports'], {
17+
platform: 'ios',
18+
enablePackageExports: true,
19+
});
2520

26-
const result = await resolve('complex-lib', 'cjs');
27-
expect(result).toBe('/node_modules/complex-lib/cjs/index.native.js');
28-
}
29-
);
21+
const result = await resolve('complex-lib', 'cjs');
22+
expect(result).toBe('/node_modules/complex-lib/cjs/index.native.js');
23+
});
3024

3125
test('should resolve utils subpath', async () => {
3226
const { resolve } = await setupTestEnvironment(['exports'], {
@@ -51,25 +45,22 @@ describe('Package Exports Resolution', () => {
5145
expect(result).toBe('/node_modules/complex-lib/native-specific.js');
5246
});
5347

54-
test.fails(
55-
'should resolve to native version for react-native condition',
56-
async () => {
57-
const { resolve } = await setupTestEnvironment(['react-strict-dom'], {
58-
platform: 'ios',
59-
enablePackageExports: true,
60-
});
48+
test('should resolve to native version for react-native condition', async () => {
49+
const { resolve } = await setupTestEnvironment(['react-strict-dom'], {
50+
platform: 'ios',
51+
enablePackageExports: true,
52+
});
6153

62-
const esmResult = await resolve('react-strict-dom', 'esm');
63-
expect(esmResult).toBe(
64-
'/node_modules/react-strict-dom/dist/native/index.js'
65-
);
54+
const esmResult = await resolve('react-strict-dom', 'esm');
55+
expect(esmResult).toBe(
56+
'/node_modules/react-strict-dom/dist/native/index.js'
57+
);
6658

67-
const cjsResult = await resolve('react-strict-dom', 'cjs');
68-
expect(cjsResult).toBe(
69-
'/node_modules/react-strict-dom/dist/native/index.js'
70-
);
71-
}
72-
);
59+
const cjsResult = await resolve('react-strict-dom', 'cjs');
60+
expect(cjsResult).toBe(
61+
'/node_modules/react-strict-dom/dist/native/index.js'
62+
);
63+
});
7364

7465
test('should fail to resolve with package exports disabled', async () => {
7566
const { resolve } = await setupTestEnvironment(['react-strict-dom'], {

0 commit comments

Comments
 (0)