|
1 | 1 | import type { MetroConfig } from '@react-native/metro-config'; |
2 | 2 | import type { Config as HarnessConfig } from '@react-native-harness/config'; |
| 3 | +import path from 'node:path'; |
3 | 4 | import { createHarnessResolver } from './composite-resolver'; |
4 | 5 | import { createTsConfigResolver } from './tsconfig-resolver'; |
5 | 6 | import type { HarnessResolver, MetroResolver } from './types'; |
6 | 7 |
|
7 | | -export const createHarnessEntryPointResolver = ( |
8 | | - harnessConfig: HarnessConfig |
9 | | -): HarnessResolver => { |
10 | | - // Can be relative to the project root or absolute, need to normalize it |
11 | | - const resolvedEntryPointPath = require.resolve(harnessConfig.entryPoint, { |
12 | | - paths: [process.cwd()], |
13 | | - }); |
| 8 | +// Safely resolves a path and strips its extension |
| 9 | +const getExtensionlessAbsolutePath = (basePath: string, relativePath = ''): string => { |
| 10 | + const fullPath = path.resolve(basePath, relativePath); |
| 11 | + const parsed = path.parse(fullPath); |
| 12 | + return path.join(parsed.dir, parsed.name); |
| 13 | +} |
14 | 14 |
|
15 | | - return (_context, moduleName, _platform) => { |
16 | | - if (moduleName === resolvedEntryPointPath) { |
17 | | - return { |
18 | | - type: 'sourceFile', |
19 | | - filePath: require.resolve('@react-native-harness/runtime/entry-point'), |
20 | | - }; |
| 15 | +export const createHarnessEntryPointResolver = (harnessConfig: HarnessConfig): HarnessResolver => { |
| 16 | + const rootPath = path.resolve(process.cwd()); |
| 17 | + const expectedEntryPoint = getExtensionlessAbsolutePath(rootPath, harnessConfig.entryPoint); |
| 18 | + const resolvedHarnessPath = require.resolve('@react-native-harness/runtime/entry-point'); |
| 19 | + |
| 20 | + return (context, moduleName, _platform) => { |
| 21 | + // 1. Resolve the origin path of the file making the import |
| 22 | + const currentOrigin = path.resolve(context.originModulePath); |
| 23 | + |
| 24 | + // Fast Fail: If the import isn't happening from the root directory, skip it immediately |
| 25 | + if (currentOrigin !== rootPath) { |
| 26 | + return null; |
21 | 27 | } |
22 | 28 |
|
23 | | - if (moduleName === harnessConfig.entryPoint) { |
| 29 | + // 2. Resolve the module being imported and strip its extension |
| 30 | + // This safely normalizes './index', './index.js', 'index.js', etc. |
| 31 | + const requestedModule = getExtensionlessAbsolutePath(currentOrigin, moduleName); |
| 32 | + |
| 33 | + // 3. String comparison |
| 34 | + if (requestedModule === expectedEntryPoint) { |
24 | 35 | return { |
25 | 36 | type: 'sourceFile', |
26 | | - filePath: require.resolve('@react-native-harness/runtime/entry-point'), |
| 37 | + filePath: resolvedHarnessPath, |
27 | 38 | }; |
28 | 39 | } |
29 | 40 |
|
30 | | - if (typeof moduleName === 'string') { |
31 | | - try { |
32 | | - const resolvedModuleName = require.resolve(moduleName, { |
33 | | - paths: [process.cwd()], |
34 | | - }); |
35 | | - if (resolvedModuleName === resolvedEntryPointPath) { |
36 | | - return { |
37 | | - type: 'sourceFile', |
38 | | - filePath: require.resolve( |
39 | | - '@react-native-harness/runtime/entry-point' |
40 | | - ), |
41 | | - }; |
42 | | - } |
43 | | - } catch { |
44 | | - // Ignore and fall through |
45 | | - } |
46 | | - } |
47 | | - |
48 | 41 | return null; |
49 | 42 | }; |
50 | 43 | }; |
|
0 commit comments