| 1 |
// Generates module path resolver for Node.js |
| 2 |
// Used to generate both sync and async version |
| 3 |
|
| 4 |
"use strict"; |
| 5 |
|
| 6 |
const isObject = require("type/object/is") |
| 7 |
, ensureString = require("type/string/ensure") |
| 8 |
, getResolver = require("../get-resolver") |
| 9 |
, { resolve } = require("path"); |
| 10 |
|
| 11 |
module.exports = function (confirmFile, resolvePackageMain) { |
| 12 |
const resolveModule = getResolver([".js", ".json", ".node"], confirmFile, resolvePackageMain); |
| 13 |
return function (dir, path, options = {}) { |
| 14 |
if (!isObject(options)) options = {}; |
| 15 |
dir = resolve(ensureString(dir)); |
| 16 |
path = ensureString(path); |
| 17 |
if (!path) throw new TypeError("Empty string is not a valid require path"); |
| 18 |
return resolveModule(dir, path).then(result => { |
| 19 |
if (result) return result; |
| 20 |
if (options.silent) return null; |
| 21 |
const error = new Error(`Cannot find module '${ path }'`); |
| 22 |
error.code = "MODULE_NOT_FOUND"; |
| 23 |
throw error; |
| 24 |
}); |
| 25 |
}; |
| 26 |
}; |