Make "browser" field work with the destination too

Reviewed By: rafeca

Differential Revision: D7974989

fbshipit-source-id: f1a14dbd44a82c1929525416b56f455a6a685bcd
This commit is contained in:
Miguel Jimenez Esun 2018-05-14 08:38:37 -07:00 committed by Facebook Github Bot
parent 2b9a8fdc30
commit 4a1e4f7646
3 changed files with 20 additions and 11 deletions

View File

@ -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<string>,
};

View File

@ -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 [],

View File

@ -75,10 +75,22 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
}
_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;
}