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

metro.config.js

74 lines2.6 KB

1const { getDefaultConfig } = require('expo/metro-config')
2const path = require('path')
3​
4const projectRoot = __dirname
5const monorepoRoot = path.resolve(projectRoot, '../../..')
6​
7const config = getDefaultConfig(projectRoot)
8​
9// Watch all files in the monorepo
10config.watchFolders = [monorepoRoot]
11​
12// Ensure symlinks are followed (important for pnpm)
13config.resolver.unstable_enableSymlinks = true
14config.resolver.unstable_enablePackageExports = true
15​
16const localNodeModules = path.resolve(projectRoot, 'node_modules')
17​
18// Singleton packages that must resolve to exactly one copy.
19// In a pnpm monorepo, workspace packages may resolve these to a different
20// version in the .pnpm store. This custom resolveRequest forces every import
21// of these packages (from anywhere) to the app's local node_modules copy.
22const singletonPackages = ['react', 'react-native']
23const singletonPaths = {}
24for (const pkg of singletonPackages) {
25 singletonPaths[pkg] = path.resolve(localNodeModules, pkg)
27​
28const defaultResolveRequest = config.resolver.resolveRequest
29config.resolver.resolveRequest = (context, moduleName, platform) => {
30 // Force singleton packages to resolve from the app's local node_modules,
31 // regardless of where the import originates. This prevents workspace
32 // packages (e.g. react-db) from pulling in their own copy of React.
33 for (const pkg of singletonPackages) {
34 if (moduleName === pkg || moduleName.startsWith(pkg + '/')) {
35 try {
36 const filePath = require.resolve(moduleName, {
37 paths: [projectRoot],
38 })
39 return { type: 'sourceFile', filePath }
40 } catch {}
41 }
42 }
43​
44 if (defaultResolveRequest) {
45 return defaultResolveRequest(context, moduleName, platform)
46 }
47 return context.resolveRequest(
48 { ...context, resolveRequest: undefined },
49 moduleName,
50 platform,
51 )
53​
54// Only singleton packages need explicit remapping. Let Metro resolve all other
55// transitive dependencies from the package that requested them.
56config.resolver.extraNodeModules = singletonPaths
57​
58// Block react-native 0.83 from root node_modules
59const escMonorepoRoot = monorepoRoot.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
60config.resolver.blockList = [
61 new RegExp(`${escMonorepoRoot}/node_modules/\\.pnpm/react-native@0\\.83.*`),
63​
64// Let Metro know where to resolve packages from (local first, then root)
65config.resolver.nodeModulesPaths = [
66 localNodeModules,
67 path.resolve(monorepoRoot, 'node_modules'),
69​
70// Allow dynamic imports with non-literal arguments (used by workspace packages
71// for optional Node.js-only code paths that are never reached on React Native)
72config.transformer.dynamicDepsInPackages = 'throwAtRuntime'
73​
74module.exports = config