diff --git a/metro.config.js b/metro.config.js index c3cd6e9649..170c84ce1a 100644 --- a/metro.config.js +++ b/metro.config.js @@ -1,22 +1,64 @@ // Learn more https://docs.expo.io/guides/customizing-metro +const fs = require('fs') +const path = require('path') const {getDefaultConfig} = require('expo/metro-config') const cfg = getDefaultConfig(__dirname) +if (process.env.ATPROTO_ROOT) { + const atprotoPackages = path.resolve(process.env.ATPROTO_ROOT, 'packages') + + cfg.watchFolders ||= [] + cfg.watchFolders.push( + ...fs + .readdirSync(atprotoPackages) + .map(dir => path.join(atprotoPackages, dir)) + .filter(dir => fs.statSync(dir).isDirectory()), + ) + + const resolveRequest = cfg.resolver.resolveRequest + cfg.resolver.resolveRequest = (context, moduleName, platform) => { + // When resolving a module from the atproto packages, try finding it there + // first. If it's not found, try resolving it from the project root (here). + if (context.originModulePath.startsWith(atprotoPackages)) { + try { + return context.resolveRequest(context, moduleName, platform) + } catch (err) { + // If a module is not found in the atproto packages, try and resolve it + // from here (e.g. @babel polyfills) + return { + type: 'sourceFile', + filePath: require.resolve(moduleName), + } + } + } + + // When resolving an @atproto/* module, replace the path prefix with + // . + if (moduleName.startsWith('@atproto/')) { + const [prefix, suffix] = moduleName.split('/', 2) + const resolution = context.resolveRequest(context, moduleName, platform) + const relativePathIdx = resolution.filePath.lastIndexOf(moduleName) + const relativePath = resolution.filePath.slice( + relativePathIdx + moduleName.length + 1, + ) + return { + type: 'sourceFile', + filePath: path.join(atprotoPackages, suffix, relativePath), + } + } + + return (resolveRequest || context.resolveRequest)( + context, + moduleName, + platform, + ) + } +} + cfg.resolver.sourceExts = process.env.RN_SRC_EXT ? process.env.RN_SRC_EXT.split(',').concat(cfg.resolver.sourceExts) : cfg.resolver.sourceExts -cfg.resolver.resolveRequest = (context, moduleName, platform) => { - if (moduleName === 'crypto' && platform !== 'web') { - return context.resolveRequest( - context, - 'react-native-quick-crypto', - platform, - ) - } - return context.resolveRequest(context, moduleName, platform) -} - cfg.transformer.getTransformOptions = async () => ({ transform: { experimentalImportSupport: true,