Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

metro.config.js

96 lines2.6 KB

1const path = require('path')
2const { getDefaultConfig } = require('expo/metro-config')
3const {
4 wrapWithReanimatedMetroConfig,
5} = require('react-native-reanimated/metro-config')
6​
7const projectRoot = __dirname
8const monorepoRoot = path.resolve(projectRoot, '../../..')
9​
10// Set EXPO_ROUTER_APP_ROOT to absolute path BEFORE config is created
11// This ensures require.context resolves correctly with pnpm symlinks
12const appRoot = path.resolve(projectRoot, 'app')
13process.env.EXPO_ROUTER_APP_ROOT = appRoot
14process.env.EXPO_ROUTER_IMPORT_MODE = 'sync'
15​
16const config = getDefaultConfig(projectRoot)
17​
18config.projectRoot = projectRoot
19​
20config.watchFolders = [
21 path.resolve(monorepoRoot, 'packages'),
22 path.resolve(monorepoRoot, 'node_modules/.pnpm'),
24​
25config.resolver.unstable_enablePackageExports = true
26​
27// Resolve from app's node_modules first, then root .pnpm for transitive deps
28config.resolver.nodeModulesPaths = [
29 path.resolve(projectRoot, 'node_modules'),
30 path.resolve(monorepoRoot, 'node_modules'),
31 path.resolve(monorepoRoot, 'node_modules/.pnpm/node_modules'),
33// Pin singletons and expo packages to prevent duplicate instances and ensure resolution
34const singletons = [
35 'react',
36 'react-dom',
37 'react-native',
38 '@tanstack/react-store',
39 'use-sync-external-store',
40 'expo',
41 'expo-router',
42 'expo-modules-core',
43 'expo-constants',
44 '@expo/metro-runtime',
46const resolvePackageRoot = (name) => {
47 try {
48 return path.dirname(
49 require.resolve(`${name}/package.json`, { paths: [projectRoot] }),
50 )
51 } catch {
52 return null
53 }
55const singletonPaths = singletons.reduce((acc, name) => {
56 const packageRoot = resolvePackageRoot(name)
57​
58 if (packageRoot) {
59 acc[name] = packageRoot
60 }
61​
62 return acc
63}, {})
64​
65config.resolver.extraNodeModules = singletons.reduce((acc, name) => {
66 if (singletonPaths[name]) {
67 acc[name] = singletonPaths[name]
68 }
69​
70 return acc
71}, {})
72​
73function getPackageName(moduleName) {
74 if (moduleName.startsWith('@')) {
75 return moduleName.split('/').slice(0, 2).join('/')
76 }
77​
78 return moduleName.split('/')[0]
80​
81config.resolver.resolveRequest = (context, moduleName, platform) => {
82 const packageName = getPackageName(moduleName)
83 const singletonPath = singletonPaths[packageName]
84​
85 if (singletonPath) {
86 const subpath = moduleName.slice(packageName.length + 1)
87 const target = subpath ? path.join(singletonPath, subpath) : singletonPath
88​
89 return context.resolveRequest(context, target, platform)
90 }
91​
92 return context.resolveRequest(context, moduleName, platform)
94​
95// Wrap with NativeWind, then Reanimated
96module.exports = wrapWithReanimatedMetroConfig(config)