Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(resolve): extract resolution function that does not throw if…
… a module is missing
  • Loading branch information
jeysal committed Jul 10, 2018
commit e3a7d7988cd50ac17700643c54ab4f252c0ba0bf
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 @@ -187,9 +186,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;

// (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.
const relativePath = path.relative(dirname, from);
const err = new Error(
`Cannot find module '${moduleName}' from '${relativePath || '.'}'`,
Expand Down