UNPKG

1.04 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5const Module = require('module');
6const path = require('path');
7
8// borrowed from babel-eslint
9/** @type {(filename: string) => Module} */
10function createModule(filename) {
11 const mod = new Module(filename);
12 mod.filename = filename;
13 // @ts-expect-error _nodeModulesPaths are undocumented
14 mod.paths = Module._nodeModulePaths(path.dirname(filename));
15 return mod;
16}
17
18/** @type {import('./module-require').default} */
19exports.default = function moduleRequire(p) {
20 try {
21 // attempt to get espree relative to eslint
22 const eslintPath = require.resolve('eslint');
23 const eslintModule = createModule(eslintPath);
24 // @ts-expect-error _resolveFilename is undocumented
25 return require(Module._resolveFilename(p, eslintModule));
26 } catch (err) { /* ignore */ }
27
28 try {
29 // try relative to entry point
30 // @ts-expect-error TODO: figure out what this is
31 return require.main.require(p);
32 } catch (err) { /* ignore */ }
33
34 // finally, try from here
35 return require(p);
36};