* Creates a `require` function that can be used to load modules from the specified path. * @param {string} filename The path to the module * @returns {any}
(filename, fileURL)
| 2117 | * @returns {any} |
| 2118 | */ |
| 2119 | function createRequireFromPath(filename, fileURL) { |
| 2120 | // Allow a directory to be passed as the filename |
| 2121 | const trailingSlash = |
| 2122 | StringPrototypeEndsWith(filename, '/') || |
| 2123 | (isWindows && StringPrototypeEndsWith(filename, '\\')); |
| 2124 | |
| 2125 | const proxyPath = trailingSlash ? |
| 2126 | path.join(filename, 'noop.js') : |
| 2127 | filename; |
| 2128 | |
| 2129 | const m = new Module(proxyPath); |
| 2130 | m.filename = proxyPath; |
| 2131 | if (fileURL !== undefined) { |
| 2132 | // Save the URL if createRequire() was given a URL, to preserve search params, if any. |
| 2133 | m[kURL] = fileURL.href; |
| 2134 | } |
| 2135 | |
| 2136 | m.paths = Module._nodeModulePaths(m.path); |
| 2137 | return makeRequireFunction(m, null); |
| 2138 | } |
| 2139 | |
| 2140 | const createRequireError = 'must be a file URL object, file URL string, or ' + |
| 2141 | 'absolute path string'; |
no test coverage detected
searching dependent graphs…