Skip to content

Commit 8106ade

Browse files
author
jeffmo
committed
Always try to use resolve first. If that fails, fallback to FB-specific clownery
1 parent 5f38808 commit 8106ade

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/HasteModuleLoader/HasteModuleLoader.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,25 @@ Loader.prototype._nodeModuleNameToPath = function(currPath, moduleName) {
463463
subModulePath = projectPathParts.join('/');
464464
}
465465

466+
if (NODE_CORE_MODULES[moduleName]) {
467+
return null;
468+
}
469+
470+
var resolveError = null;
471+
try {
472+
return resolve.sync(moduleName, {
473+
basedir: path.dirname(currPath),
474+
extensions: ['.js', '.json']
475+
});
476+
} catch (e) {
477+
// Facebook has clowny package.json resolution rules that don't apply to
478+
// regular Node rules. Until we can make ModuleLoaders more pluggable
479+
// (so that FB can have a custom ModuleLoader and all the normal people can
480+
// have a normal ModuleLoader), we catch node-resolution exceptions and
481+
// fall back to some custom resolution logic before throwing the error.
482+
resolveError = e;
483+
}
484+
466485
// Memoize the project name -> package.json resource lookup map
467486
if (this._nodeModuleProjectConfigNameToResource === null) {
468487
this._nodeModuleProjectConfigNameToResource = {};
@@ -476,18 +495,15 @@ Loader.prototype._nodeModuleNameToPath = function(currPath, moduleName) {
476495
// Get the resource for the package.json file
477496
var resource = this._nodeModuleProjectConfigNameToResource[moduleProjectPart];
478497
if (!resource) {
479-
if (NODE_CORE_MODULES[moduleName]) {
480-
return null;
481-
}
482-
return resolve.sync(moduleName, {basedir: path.dirname(currPath)});
498+
throw resolveError;
483499
}
484500

485501
// Make sure the resource path is above the currPath in the fs path
486502
// tree. If so, just use node's resolve
487503
var resourceDirname = path.dirname(resource.path);
488504
var currFileDirname = path.dirname(currPath);
489505
if (resourceDirname.indexOf(currFileDirname) > 0) {
490-
return resolve.sync(moduleName, {basedir: path.dirname(currPath)});
506+
throw resolveError;
491507
}
492508

493509
if (subModulePath === null) {

0 commit comments

Comments
 (0)