diff --git a/packages/metro-resolver/src/resolve.js b/packages/metro-resolver/src/resolve.js index 33faf065..fe3b3beb 100644 --- a/packages/metro-resolver/src/resolve.js +++ b/packages/metro-resolver/src/resolve.js @@ -113,7 +113,8 @@ function resolve( const allDirPaths = dirPaths.concat(extraPaths); for (let i = 0; i < allDirPaths.length; ++i) { - const result = resolveFileOrDir(context, allDirPaths[i], platform); + const realModuleName = context.redirectModulePath(allDirPaths[i]); + const result = resolveFileOrDir(context, realModuleName, platform); if (result.type === 'resolved') { return result.resolution; } @@ -127,11 +128,6 @@ type ModulePathContext = FileOrDirContext & { * resolved. */ +originModulePath: string, - /** - * Lookup the module's closest `package.json` and process the redirects - * metadata. Return an absolute path to the resolved path. - */ - +redirectModulePath: (modulePath: string) => string | false, }; /** @@ -347,6 +343,7 @@ type FileContext = { +doesFileExist: DoesFileExist, +isAssetFile: IsAssetFile, +preferNativePlatform: boolean, + +redirectModulePath: (modulePath: string) => string | false, +resolveAsset: ResolveAsset, +sourceExts: $ReadOnlyArray, }; diff --git a/packages/metro/src/DeltaBundler/__tests__/__snapshots__/traverseDependencies-integration-test.js.snap b/packages/metro/src/DeltaBundler/__tests__/__snapshots__/traverseDependencies-integration-test.js.snap index 565bdea1..29a1f335 100644 --- a/packages/metro/src/DeltaBundler/__tests__/__snapshots__/traverseDependencies-integration-test.js.snap +++ b/packages/metro/src/DeltaBundler/__tests__/__snapshots__/traverseDependencies-integration-test.js.snap @@ -1526,7 +1526,7 @@ Array [ }, Object { "dependencies": Array [], - "path": "/root/node_modules/foo/node_modules/bar/lol.js", + "path": "/root/node_modules/foo/node_modules/bar/wow.js", }, Object { "dependencies": Array [], @@ -1878,7 +1878,7 @@ Array [ }, Object { "dependencies": Array [], - "path": "C:\\\\root\\\\node_modules\\\\foo\\\\node_modules\\\\bar\\\\lol.js", + "path": "C:\\\\root\\\\node_modules\\\\foo\\\\node_modules\\\\bar\\\\wow.js", }, Object { "dependencies": Array [], diff --git a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js index 2f9ef740..8b958df0 100644 --- a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js +++ b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js @@ -75,10 +75,22 @@ class ModuleResolver { } _redirectRequire(fromModule: TModule, modulePath: string): string | false { - const pck = fromModule.getPackage(); - if (pck) { - return pck.redirectRequire(modulePath); + const moduleCache = this._options.moduleCache; + + try { + const pck = + modulePath.startsWith('.') || path.isAbsolute(modulePath) + ? moduleCache.getModule(modulePath).getPackage() + : fromModule.getPackage(); + + if (pck) { + return pck.redirectRequire(modulePath); + } + } catch (err) { + // Do nothing. The standard module cache does not trigger any error, but + // the ModuleGraph one does, if the module does not exist. } + return modulePath; }