|
| 1 | +import path from 'path'; |
| 2 | +import fs from 'fs'; |
| 3 | +import type { Resolution, CustomResolutionContext } from 'metro-resolver'; |
| 4 | +import type { HarnessResolver } from './types'; |
| 5 | + |
| 6 | +// This resolver is based on the Expo's implementation. |
| 7 | +// The reason to have it in Harness is that Expo doesn't set the resolveRequest function in the context. |
| 8 | +// In order for tsconfig's paths to work, we need to recreate this logic ourselves. |
| 9 | + |
| 10 | +export type TsConfigPaths = { |
| 11 | + paths: Record<string, string[]>; |
| 12 | + baseUrl: string; |
| 13 | + hasBaseUrl: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Load tsconfig.json or jsconfig.json and extract path mappings |
| 18 | + */ |
| 19 | +export const loadTsConfigPaths = ( |
| 20 | + projectRoot: string |
| 21 | +): TsConfigPaths | null => { |
| 22 | + const configFiles = ['tsconfig.json', 'jsconfig.json']; |
| 23 | + |
| 24 | + for (const configFile of configFiles) { |
| 25 | + const configPath = path.join(projectRoot, configFile); |
| 26 | + |
| 27 | + if (!fs.existsSync(configPath)) continue; |
| 28 | + |
| 29 | + try { |
| 30 | + const content = fs.readFileSync(configPath, 'utf8'); |
| 31 | + // Strip comments without touching string literals |
| 32 | + const jsonContent = stripJsonComments(content); |
| 33 | + const config = JSON.parse(jsonContent); |
| 34 | + |
| 35 | + const compilerOptions = config.compilerOptions || {}; |
| 36 | + const paths = compilerOptions.paths || {}; |
| 37 | + const baseUrl = compilerOptions.baseUrl; |
| 38 | + |
| 39 | + if (Object.keys(paths).length > 0 || baseUrl) { |
| 40 | + return { |
| 41 | + paths, |
| 42 | + baseUrl: baseUrl ? path.resolve(projectRoot, baseUrl) : projectRoot, |
| 43 | + hasBaseUrl: !!baseUrl, |
| 44 | + }; |
| 45 | + } |
| 46 | + } catch (error) { |
| 47 | + console.warn(`Failed to parse ${configFile}:`, error); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return null; |
| 52 | +}; |
| 53 | + |
| 54 | +const stripJsonComments = (input: string): string => { |
| 55 | + let result = ''; |
| 56 | + let inString = false; |
| 57 | + let stringChar = ''; |
| 58 | + let isEscaped = false; |
| 59 | + let inLineComment = false; |
| 60 | + let inBlockComment = false; |
| 61 | + |
| 62 | + for (let i = 0; i < input.length; i += 1) { |
| 63 | + const char = input[i]; |
| 64 | + const nextChar = input[i + 1]; |
| 65 | + |
| 66 | + if (inLineComment) { |
| 67 | + if (char === '\n') { |
| 68 | + inLineComment = false; |
| 69 | + result += char; |
| 70 | + } |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if (inBlockComment) { |
| 75 | + if (char === '*' && nextChar === '/') { |
| 76 | + inBlockComment = false; |
| 77 | + i += 1; |
| 78 | + } |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + if (inString) { |
| 83 | + result += char; |
| 84 | + if (!isEscaped && char === stringChar) { |
| 85 | + inString = false; |
| 86 | + stringChar = ''; |
| 87 | + } |
| 88 | + isEscaped = !isEscaped && char === '\\'; |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + if (char === '"' || char === "'") { |
| 93 | + inString = true; |
| 94 | + stringChar = char; |
| 95 | + result += char; |
| 96 | + isEscaped = false; |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + if (char === '/' && nextChar === '/') { |
| 101 | + inLineComment = true; |
| 102 | + i += 1; |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if (char === '/' && nextChar === '*') { |
| 107 | + inBlockComment = true; |
| 108 | + i += 1; |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + result += char; |
| 113 | + } |
| 114 | + |
| 115 | + return result; |
| 116 | +}; |
| 117 | + |
| 118 | +/** |
| 119 | + * Match module name against tsconfig path pattern (supports wildcards) |
| 120 | + */ |
| 121 | +const matchPattern = ( |
| 122 | + pattern: string, |
| 123 | + moduleName: string |
| 124 | +): { matched: boolean; captured: string } => { |
| 125 | + const escapedPattern = pattern |
| 126 | + .replace(/[.+?^${}()|[\]\\]/g, '\\$&') |
| 127 | + .replace(/\*/g, '(.*)'); |
| 128 | + |
| 129 | + const regex = new RegExp(`^${escapedPattern}$`); |
| 130 | + const match = moduleName.match(regex); |
| 131 | + |
| 132 | + return { |
| 133 | + matched: !!match, |
| 134 | + captured: match?.[1] || '', |
| 135 | + }; |
| 136 | +}; |
| 137 | + |
| 138 | +/** |
| 139 | + * Resolve module using tsconfig path mappings |
| 140 | + * Use this directly in your custom resolver |
| 141 | + */ |
| 142 | +export const resolveWithTsConfigPaths = ( |
| 143 | + tsConfig: TsConfigPaths, |
| 144 | + context: CustomResolutionContext, |
| 145 | + moduleName: string, |
| 146 | + platform: string | null |
| 147 | +): Resolution | null => { |
| 148 | + const { paths, baseUrl, hasBaseUrl } = tsConfig; |
| 149 | + const resolveRequest = context.resolveRequest; |
| 150 | + |
| 151 | + if (!resolveRequest) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + // Try path mappings first |
| 156 | + for (const [pattern, targets] of Object.entries(paths)) { |
| 157 | + const { matched, captured } = matchPattern(pattern, moduleName); |
| 158 | + if (!matched) continue; |
| 159 | + |
| 160 | + // Try each target |
| 161 | + for (const target of targets) { |
| 162 | + const resolvedTarget = target.replace('*', captured); |
| 163 | + const absolutePath = path.resolve(baseUrl, resolvedTarget); |
| 164 | + |
| 165 | + try { |
| 166 | + return resolveRequest(context, absolutePath, platform); |
| 167 | + } catch { |
| 168 | + continue; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Try baseUrl for non-relative imports |
| 174 | + if (hasBaseUrl && !moduleName.startsWith('.') && !moduleName.startsWith('/')) { |
| 175 | + const absolutePath = path.resolve(baseUrl, moduleName); |
| 176 | + try { |
| 177 | + return resolveRequest(context, absolutePath, platform); |
| 178 | + } catch { |
| 179 | + // Fall through |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return null; |
| 184 | +}; |
| 185 | + |
| 186 | +export const createTsConfigResolver = ( |
| 187 | + projectRoot: string |
| 188 | +): HarnessResolver => { |
| 189 | + const tsConfig = loadTsConfigPaths(projectRoot); |
| 190 | + |
| 191 | + return (context, moduleName, platform) => { |
| 192 | + if (!tsConfig) { |
| 193 | + return null; |
| 194 | + } |
| 195 | + |
| 196 | + if (!context.resolveRequest) { |
| 197 | + return null; |
| 198 | + } |
| 199 | + |
| 200 | + const resolved = resolveWithTsConfigPaths( |
| 201 | + tsConfig, |
| 202 | + context, |
| 203 | + moduleName, |
| 204 | + platform |
| 205 | + ); |
| 206 | + |
| 207 | + return resolved ?? null; |
| 208 | + }; |
| 209 | +}; |
0 commit comments