forked from selfxyz/self
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetro.config.cjs
More file actions
362 lines (328 loc) · 12.6 KB
/
metro.config.cjs
File metadata and controls
362 lines (328 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// SPDX-FileCopyrightText: 2025 Social Connect Labs, Inc.
// SPDX-License-Identifier: BUSL-1.1
// NOTE: Converts to Apache-2.0 on 2029-06-11 per LICENSE.
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const path = require('node:path');
const fs = require('node:fs');
const findYarnWorkspaceRoot = require('find-yarn-workspace-root');
const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;
const projectRoot = __dirname;
const workspaceRoot =
findYarnWorkspaceRoot(__dirname) || path.resolve(__dirname, '..');
/**
* Modern Metro configuration using native workspace capabilities
* Eliminates need for manual symlink management through:
* - enableGlobalPackages: Automatic workspace package discovery
* - unstable_enablePackageExports: Native subpath import support
* - unstable_enableSymlinks: Optional symlink resolution
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
projectRoot,
watchFolders: [
workspaceRoot, // Watch entire workspace root for changes
path.resolve(workspaceRoot, 'common'),
path.resolve(workspaceRoot, 'packages/mobile-sdk-alpha'),
path.resolve(projectRoot, 'node_modules'), // Watch app's node_modules for custom resolved modules
],
transformer: {
babelTransformerPath: require.resolve(
'react-native-svg-transformer/react-native',
),
disableImportExportTransform: true,
inlineRequires: true,
},
resolver: {
// Prevent Haste module naming collisions from duplicate package.json files
blockList: [
// Ignore built package.json files to prevent Haste collisions
/.*\/dist\/package\.json$/,
/.*\/dist\/esm\/package\.json$/,
/.*\/dist\/cjs\/package\.json$/,
/.*\/build\/package\.json$/,
// Prevent duplicate React/React Native - block workspace root versions and use app's versions
// Use precise regex patterns to avoid blocking packages like react-native-get-random-values
new RegExp(
`^${workspaceRoot.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/node_modules/react(/|$)`,
),
new RegExp(
`^${workspaceRoot.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/node_modules/react-dom(/|$)`,
),
new RegExp(
`^${workspaceRoot.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/node_modules/react-native(/|$)`,
),
new RegExp(
`^${workspaceRoot.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/node_modules/scheduler(/|$)`,
),
new RegExp('packages/mobile-sdk-alpha/node_modules/react(/|$)'),
new RegExp('packages/mobile-sdk-alpha/node_modules/react-dom(/|$)'),
new RegExp('packages/mobile-sdk-alpha/node_modules/react-native(/|$)'),
new RegExp('packages/mobile-sdk-alpha/node_modules/scheduler(/|$)'),
new RegExp('packages/mobile-sdk-demo/node_modules/react(/|$)'),
new RegExp('packages/mobile-sdk-demo/node_modules/react-dom(/|$)'),
new RegExp('packages/mobile-sdk-demo/node_modules/react-native(/|$)'),
new RegExp('packages/mobile-sdk-demo/node_modules/scheduler(/|$)'),
],
// Enable automatic workspace package resolution
enableGlobalPackages: true,
// Handle subpath exports (@selfxyz/common/constants)
unstable_enablePackageExports: true,
// Enable native symlink support (optional, for compatibility)
unstable_enableSymlinks: true,
// Define search order for node modules - prioritize app's modules for React consistency
nodeModulesPaths: [
path.resolve(projectRoot, 'node_modules'), // App's own node_modules FIRST
path.resolve(workspaceRoot, 'node_modules'), // Workspace root node_modules SECOND
],
// Essential polyfills for React Native
extraNodeModules: {
stream: require.resolve('stream-browserify'),
buffer: require.resolve('buffer'),
util: require.resolve('util'),
assert: require.resolve('assert'),
events: require.resolve('events'),
process: require.resolve('process'),
// App-specific alias
'@': path.join(__dirname, 'src'),
},
// Support package exports with conditions
unstable_conditionNames: ['react-native', 'import', 'require'],
// SVG support
assetExts: assetExts.filter(ext => ext !== 'svg'),
sourceExts: [...sourceExts, 'svg'],
// Custom resolver to handle both .js imports in TypeScript and Node.js modules
resolveRequest: (context, moduleName, platform) => {
// Handle React Native gesture handler that needs app-level resolution
const appLevelModules = {
'react-native-gesture-handler':
'react-native-gesture-handler/lib/commonjs/index.js',
};
const sdkAlphaPath = path.resolve(
workspaceRoot,
'packages/mobile-sdk-alpha',
);
// Custom resolver to handle Node.js modules and dynamic flow imports
if (moduleName.startsWith('@selfxyz/mobile-sdk-alpha/')) {
const subPath = moduleName.replace('@selfxyz/mobile-sdk-alpha/', '');
// Check if it's a flow import (onboarding/* or disclosing/*)
if (
subPath.startsWith('onboarding/') ||
subPath.startsWith('disclosing/')
) {
const flowPath = path.resolve(
sdkAlphaPath,
'dist/esm/flows',
`${subPath}.js`,
);
// Check if the file exists
if (fs.existsSync(flowPath)) {
return {
type: 'sourceFile',
filePath: flowPath,
};
}
}
}
if (appLevelModules[moduleName]) {
try {
return {
type: 'sourceFile',
filePath: require.resolve(appLevelModules[moduleName], {
paths: [projectRoot],
}),
};
} catch (error) {
console.warn(`Failed to resolve ${moduleName}:`, error);
// Fall back to default resolution
return context.resolveRequest(context, moduleName, platform);
}
}
// React modules now resolve naturally through nodeModulesPaths (app's node_modules first)
// Force SDK to use built ESM to avoid duplicate React and source transpilation issues
if (moduleName === '@selfxyz/mobile-sdk-alpha') {
return {
type: 'sourceFile',
filePath: path.resolve(
workspaceRoot,
'packages/mobile-sdk-alpha/dist/esm/index.js',
),
};
}
// For relative imports in common source files that end with .js
if (
context.originModulePath?.includes('/common/src/') &&
moduleName.endsWith('.js')
) {
const tsModuleName = moduleName.replace(/\.js$/, '.ts');
return context.resolveRequest(context, tsModuleName, platform);
}
// Handle problematic package exports and Node.js modules
// Fix @tamagui/config v2-native export resolution
if (moduleName === '@tamagui/config/v2-native') {
try {
return {
type: 'sourceFile',
filePath: require.resolve('@tamagui/config/dist/esm/v2-native.js'),
};
} catch {
// Fallback to main export if specific file doesn't exist
return {
type: 'sourceFile',
filePath: require.resolve('@tamagui/config'),
};
}
}
// Fix @noble/hashes subpath export resolution
if (moduleName.startsWith('@noble/hashes/')) {
try {
// Extract the subpath (e.g., 'crypto.js', 'sha256', 'hmac')
const subpath = moduleName.replace('@noble/hashes/', '');
const basePath = require.resolve('@noble/hashes');
// For .js files, look in the package directory
if (subpath.endsWith('.js')) {
const subpathFile = path.join(path.dirname(basePath), subpath);
return {
type: 'sourceFile',
filePath: subpathFile,
};
} else {
// For other imports like 'sha256', 'hmac', etc., try the main directory
const subpathFile = path.join(
path.dirname(basePath),
`${subpath}.js`,
);
return {
type: 'sourceFile',
filePath: subpathFile,
};
}
} catch {
// Fallback to main package if subpath doesn't exist
return {
type: 'sourceFile',
filePath: require.resolve('@noble/hashes'),
};
}
}
// Fix snarkjs and ffjavascript platform exports for Android
if (platform === 'android') {
// Handle snarkjs and its nested dependencies that have platform export issues
if (
moduleName.includes('/snarkjs') &&
(moduleName.endsWith('/snarkjs') ||
moduleName.includes('/snarkjs/node_modules'))
) {
try {
// Try to resolve the main package file
const packagePath = moduleName.split('/node_modules/').pop();
const resolved = require.resolve(packagePath || 'snarkjs');
return {
type: 'sourceFile',
filePath: resolved,
};
} catch {
// Fallback to basic snarkjs resolution
try {
return {
type: 'sourceFile',
filePath: require.resolve('snarkjs'),
};
} catch {
// Continue to next check
}
}
}
// Handle ffjavascript from any nested location
if (
moduleName.includes('/ffjavascript') &&
moduleName.endsWith('/ffjavascript')
) {
try {
// Try to resolve ffjavascript from the specific nested location first
const resolved = require.resolve(moduleName);
return {
type: 'sourceFile',
filePath: resolved,
};
} catch {
// Fallback to resolving ffjavascript from the closest available location
try {
const resolved = require.resolve('ffjavascript');
return {
type: 'sourceFile',
filePath: resolved,
};
} catch {
// Continue to next check
}
}
}
// Handle direct package imports for known problematic packages
const platformProblematicPackages = ['snarkjs', 'ffjavascript'];
for (const pkg of platformProblematicPackages) {
if (moduleName === pkg || moduleName.startsWith(`${pkg}/`)) {
try {
return {
type: 'sourceFile',
filePath: require.resolve(pkg),
};
} catch {
// Continue to next check
continue;
}
}
}
}
const nodeModuleRedirects = {
crypto: path.resolve(__dirname, '../common/src/polyfills/crypto.ts'),
fs: false, // Disable filesystem access
os: false, // Disable OS-specific modules
readline: false, // Disable readline module
constants: require.resolve('constants-browserify'),
path: require.resolve('path-browserify'),
'web-worker': false, // Disable web workers (not available in React Native)
};
if (
Object.prototype.hasOwnProperty.call(nodeModuleRedirects, moduleName)
) {
if (nodeModuleRedirects[moduleName] === false) {
// Return empty module for disabled modules
return { type: 'empty' };
}
// Redirect to polyfill
return {
type: 'sourceFile',
filePath: nodeModuleRedirects[moduleName],
};
}
// Handle optional peer dependencies by returning empty modules
const optionalPeerDependencies = [
'react-native-reanimated',
'@react-native-masked-view/masked-view',
'@react-native-firebase/analytics',
];
if (optionalPeerDependencies.includes(moduleName)) {
// Return empty module for optional peer dependencies
return { type: 'empty' };
}
// Fall back to default Metro resolver for all other modules
try {
return context.resolveRequest(context, moduleName, platform);
} catch (error) {
// Check if this is one of our expected optional dependencies
if (optionalPeerDependencies.some(dep => moduleName.includes(dep))) {
return { type: 'empty' };
}
// If default resolution fails, log and re-throw
console.warn(
`Metro resolver failed for module "${moduleName}":`,
error.message,
);
throw error;
}
},
},
};
module.exports = mergeConfig(defaultConfig, config);