Skip to content

Commit

Permalink
refactor(resolve): extract resolution function that does not throw if…
Browse files Browse the repository at this point in the history
… a module is missing
  • Loading branch information
jeysal committed Jun 15, 2018
1 parent 794e86a commit 67fd822
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ class Resolver {
return null;
}

resolveModule(
from: Path,
_resolveModuleFromDirIfExists(
dirname: Path,
moduleName: string,
options?: ResolveModuleConfig,
): Path {
const dirname = path.dirname(from);
): ?Path {
const paths = this._options.modulePaths;
const moduleDirectory = this._options.moduleDirectories;
const key = dirname + path.delimiter + moduleName;
Expand Down Expand Up @@ -188,9 +187,25 @@ class Resolver {
} catch (ignoredError) {}
}

// 4. Throw an error if the module could not be found. `resolve.sync`
// only produces an error based on the dirname but we have the actual
// current module name available.
return null;
}

resolveModule(
from: Path,
moduleName: string,
options?: ResolveModuleConfig,
): Path {
const dirname = path.dirname(from);
const module = this._resolveModuleFromDirIfExists(
dirname,
moduleName,
options,
);
if (module) return module;

// Throw an error if the module could not be found. `resolve.sync`
// only produces an error based on the dirname but we have the actual
// current module name available.
const relativePath = path.relative(dirname, from);
const err = new Error(
`Cannot find module '${moduleName}' from '${relativePath || '.'}'`,
Expand Down

0 comments on commit 67fd822

Please sign in to comment.