|
1 | 1 | import { createRequire } from 'node:module'; |
| 2 | +import type { PluginItem, types } from '@babel/core'; |
2 | 3 |
|
3 | | -export function resolveReactNativeParser(projectRoot: string) { |
| 4 | +function resolvePreset(projectRoot: string) { |
| 5 | + const presetPath = require.resolve( |
| 6 | + '@react-native/babel-preset/package.json', |
| 7 | + { |
| 8 | + paths: [projectRoot], |
| 9 | + } |
| 10 | + ); |
| 11 | + const presetRequire = createRequire(presetPath); |
| 12 | + const { version, dependencies } = presetRequire('./package.json'); |
| 13 | + |
| 14 | + return { |
| 15 | + presetRequire, |
| 16 | + dependencies: dependencies as Record<string, string> | undefined, |
| 17 | + location: `@react-native/babel-preset@${version} (${presetPath})`, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Caches a lookup per project root: failed resolutions aren't cached by Node, |
| 23 | + * and these run for every transformed file. |
| 24 | + */ |
| 25 | +function cacheByProjectRoot<T>(resolve: (projectRoot: string) => T) { |
| 26 | + const cache = new Map<string, T>(); |
| 27 | + return (projectRoot: string): T => { |
| 28 | + if (!cache.has(projectRoot)) { |
| 29 | + cache.set(projectRoot, resolve(projectRoot)); |
| 30 | + } |
| 31 | + return cache.get(projectRoot)!; |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Resolves a dependency of the project's `@react-native/babel-preset`, |
| 37 | + * e.g. a Babel plugin the preset ships with. |
| 38 | + */ |
| 39 | +export function resolveFromReactNativePreset( |
| 40 | + projectRoot: string, |
| 41 | + request: string |
| 42 | +): string { |
4 | 43 | let presetLocation = projectRoot; |
5 | 44 |
|
6 | 45 | try { |
7 | | - const presetPath = require.resolve( |
8 | | - '@react-native/babel-preset/package.json', |
9 | | - { |
10 | | - paths: [projectRoot], |
11 | | - } |
| 46 | + const { presetRequire, location } = resolvePreset(projectRoot); |
| 47 | + presetLocation = location; |
| 48 | + return presetRequire.resolve(request); |
| 49 | + } catch (cause) { |
| 50 | + throw Object.assign( |
| 51 | + new Error( |
| 52 | + `Failed to resolve '${request}' from ${presetLocation}. ` + |
| 53 | + `Make sure '@react-native/babel-preset' is installed.` |
| 54 | + ), |
| 55 | + { cause } |
12 | 56 | ); |
| 57 | + } |
| 58 | +} |
13 | 59 |
|
14 | | - const presetRequire = createRequire(presetPath); |
15 | | - const { version, dependencies } = presetRequire('./package.json'); |
| 60 | +/** |
| 61 | + * Resolves `flow-enums-runtime`, which lowered Flow enums require. |
| 62 | + * |
| 63 | + * Only React Native depends on it, so with isolated installs (pnpm) it often |
| 64 | + * can't be resolved from the project root or from the file using an enum. |
| 65 | + */ |
| 66 | +const resolveFlowEnumsRuntime = cacheByProjectRoot( |
| 67 | + (projectRoot): string | null => { |
| 68 | + try { |
| 69 | + return require.resolve('flow-enums-runtime', { paths: [projectRoot] }); |
| 70 | + } catch {} |
16 | 71 |
|
17 | | - presetLocation = `@react-native/babel-preset@${version} (${presetPath})`; |
| 72 | + try { |
| 73 | + const reactNativePath = require.resolve('react-native/package.json', { |
| 74 | + paths: [projectRoot], |
| 75 | + }); |
| 76 | + return createRequire(reactNativePath).resolve('flow-enums-runtime'); |
| 77 | + } catch { |
| 78 | + return null; |
| 79 | + } |
| 80 | + } |
| 81 | +); |
| 82 | + |
| 83 | +// lets the plugin run alongside a copy the project adds itself, which Babel |
| 84 | +// rejects as a duplicate when neither copy is named |
| 85 | +const FLOW_ENUMS_PLUGIN_NAME = '@callstack/repack/flow-enums'; |
| 86 | + |
| 87 | +/** |
| 88 | + * `babel-plugin-transform-flow-enums` from the project's React Native preset, |
| 89 | + * or nothing when the preset isn't installed. |
| 90 | + * |
| 91 | + * Has to run ahead of the preset: the preset's `flow-strip-types` removes enum |
| 92 | + * declarations before the preset's own enum plugin gets to them. |
| 93 | + */ |
| 94 | +export const getFlowEnumsPlugins = cacheByProjectRoot( |
| 95 | + (projectRoot): PluginItem[] => { |
| 96 | + let pluginPath: string; |
| 97 | + try { |
| 98 | + pluginPath = resolveFromReactNativePreset( |
| 99 | + projectRoot, |
| 100 | + 'babel-plugin-transform-flow-enums' |
| 101 | + ); |
| 102 | + } catch { |
| 103 | + return []; |
| 104 | + } |
| 105 | + |
| 106 | + const runtimePath = resolveFlowEnumsRuntime(projectRoot); |
| 107 | + // the plugin defaults to `require('flow-enums-runtime')` |
| 108 | + if (!runtimePath) return [[pluginPath, {}, FLOW_ENUMS_PLUGIN_NAME]]; |
| 109 | + |
| 110 | + return [ |
| 111 | + [ |
| 112 | + pluginPath, |
| 113 | + { |
| 114 | + getRuntime: (t: typeof types) => |
| 115 | + t.callExpression(t.identifier('require'), [ |
| 116 | + t.stringLiteral(runtimePath), |
| 117 | + ]), |
| 118 | + }, |
| 119 | + FLOW_ENUMS_PLUGIN_NAME, |
| 120 | + ], |
| 121 | + ]; |
| 122 | + } |
| 123 | +); |
| 124 | + |
| 125 | +/** |
| 126 | + * Picks the React runtime that Flow `component` declarations are lowered for, |
| 127 | + * based on the project's `react` version: React 19 passes `ref` as a prop, |
| 128 | + * React 18 needs `forwardRef`. Defaults to React 19. |
| 129 | + */ |
| 130 | +export const resolveReactRuntimeTarget = cacheByProjectRoot( |
| 131 | + (projectRoot): '18' | '19' => { |
| 132 | + try { |
| 133 | + const { version } = require( |
| 134 | + require.resolve('react/package.json', { paths: [projectRoot] }) |
| 135 | + ); |
| 136 | + return Number.parseInt(version, 10) < 19 ? '18' : '19'; |
| 137 | + } catch { |
| 138 | + return '19'; |
| 139 | + } |
| 140 | + } |
| 141 | +); |
| 142 | + |
| 143 | +export function resolveReactNativeParser(projectRoot: string) { |
| 144 | + let presetLocation = projectRoot; |
| 145 | + |
| 146 | + try { |
| 147 | + const { presetRequire, dependencies, location } = |
| 148 | + resolvePreset(projectRoot); |
| 149 | + presetLocation = location; |
18 | 150 |
|
19 | 151 | // A hoisted parser must not override the one declared by the preset. |
20 | 152 | if (dependencies?.['flow-parser']) { |
|
0 commit comments