diff --git a/packages/mpack/src/metro/enhancedResolver.ts b/packages/mpack/src/metro/enhancedResolver.ts index 4a8cff03c..13c0b08b4 100644 --- a/packages/mpack/src/metro/enhancedResolver.ts +++ b/packages/mpack/src/metro/enhancedResolver.ts @@ -45,6 +45,10 @@ export function createResolver(rootPath: string, options?: CreateResolverOptions conditionNames: options?.conditionNames ?? [...RESOLVER_EXPORTS_MAP_CONDITIONS, 'require', 'node', 'default'], mainFiles: ['index'], modules: ['node_modules', path.join(rootPath, 'src')], + alias: { + 'react-native': path.join(rootPath, 'node_modules', 'react-native'), + react: path.join(rootPath, 'node_modules', 'react'), + }, }); function resolve(context: MetroResolutionContext, request: string) { diff --git a/packages/mpack/src/metro/getMetroConfig.ts b/packages/mpack/src/metro/getMetroConfig.ts index 38a544f36..ae4bd3bb1 100644 --- a/packages/mpack/src/metro/getMetroConfig.ts +++ b/packages/mpack/src/metro/getMetroConfig.ts @@ -1,12 +1,12 @@ -import path from 'path'; import type { AdditionalMetroConfig } from '@granite-js/plugin-core'; import { getPackageRoot } from '@granite-js/utils'; -import { createResolver } from './enhancedResolver'; -import { getMonorepoRoot } from './getMonorepoRoot'; +import path from 'path'; import { DEV_SERVER_DEFAULT_PORT, RESOLVER_MAIN_FIELDS, SOURCE_EXTENSIONS } from '../constants'; import { getDefaultValues } from '../vendors/metro-config/src/defaults'; import exclusionList from '../vendors/metro-config/src/defaults/exclusionList'; import { mergeConfig } from '../vendors/metro-config/src/loadConfig'; +import { createResolver } from './enhancedResolver'; +import { getMonorepoRoot } from './getMonorepoRoot'; export interface GetMetroConfig { rootPath: string; @@ -78,12 +78,12 @@ export async function getMetroConfig({ rootPath }: GetMetroConfig, additionalCon resolveRequest, // metro-file-map sourceExts: [...SOURCE_EXTENSIONS.map((extension) => extension.replace(/^\.?/, '')), 'cjs', 'mjs'], - blockList: exclusionList( - additionalConfig?.resolver?.blockList ? asArray(additionalConfig.resolver.blockList) : [] - ), - nodeModulesPaths: additionalConfig?.resolver?.nodeModulesPaths || [], + blockList: exclusionList([ + ...(additionalConfig?.resolver?.blockList ? asArray(additionalConfig.resolver.blockList) : []), + ]), + nodeModulesPaths: [path.join(rootPath, 'node_modules'), ...(additionalConfig?.resolver?.nodeModulesPaths || [])], extraNodeModules: additionalConfig?.resolver?.extraNodeModules || {}, - disableHierarchicalLookup: additionalConfig?.resolver?.disableHierarchicalLookup, + disableHierarchicalLookup: additionalConfig?.resolver?.disableHierarchicalLookup ?? true, resolverMainFields: additionalConfig?.resolver?.resolverMainFields ?? RESOLVER_MAIN_FIELDS, }, serializer: { diff --git a/packages/mpack/src/vendors/metro/src/node-haste/DependencyGraph.js b/packages/mpack/src/vendors/metro/src/node-haste/DependencyGraph.js index 16b0e4ca1..8e133b1db 100644 --- a/packages/mpack/src/vendors/metro/src/node-haste/DependencyGraph.js +++ b/packages/mpack/src/vendors/metro/src/node-haste/DependencyGraph.js @@ -191,15 +191,28 @@ class DependencyGraph extends EventEmitter { // MARK: - GRANITE const realpath = fs.realpathSync(containerName); const resolvedPath = (pnpapi ? pnpapi.resolveVirtual(realpath) : realpath) ?? realpath; - const sha1 = this._hasteFS.getSha1(resolvedPath); + let sha1 = this._hasteFS.getSha1(resolvedPath); + // Fallback for pnpm symlinks: try original filename + if (!sha1 && resolvedPath !== filename) { + sha1 = this._hasteFS.getSha1(filename); + } + + // Fallback: compute SHA-1 directly from file content if (!sha1) { - throw new ReferenceError( - `SHA-1 for file ${filename} (${resolvedPath}) is not computed. - Potential causes: - 1) You have symlinks in your project - watchman does not follow symlinks. - 2) Check \`blockList\` in your metro.config.js and make sure it isn't excluding the file path.` - ); + try { + const crypto = require('crypto'); + const content = fs.readFileSync(resolvedPath); + sha1 = crypto.createHash('sha1').update(content).digest('hex'); + } catch (error) { + throw new ReferenceError( + `SHA-1 for file ${filename} (${resolvedPath}) is not computed. + Potential causes: + 1) You have symlinks in your project - watchman does not follow symlinks. + 2) Check \`blockList\` in your metro.config.js and make sure it isn't excluding the file path. + Error: ${error.message}` + ); + } } return sha1;