Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/resolvers/node/index.js
829 views
1
'use strict';
2
3
const resolve = require('resolve');
4
const path = require('path');
5
6
const log = require('debug')('eslint-plugin-import:resolver:node');
7
8
exports.interfaceVersion = 2;
9
10
exports.resolve = function (source, file, config) {
11
log('Resolving:', source, 'from:', file);
12
let resolvedPath;
13
14
if (resolve.isCore(source)) {
15
log('resolved to core');
16
return { found: true, path: null };
17
}
18
19
try {
20
const cachedFilter = function (pkg, dir) { return packageFilter(pkg, dir, config); };
21
resolvedPath = resolve.sync(source, opts(file, config, cachedFilter));
22
log('Resolved to:', resolvedPath);
23
return { found: true, path: resolvedPath };
24
} catch (err) {
25
log('resolve threw error:', err);
26
return { found: false };
27
}
28
};
29
30
function opts(file, config, packageFilter) {
31
return Object.assign({
32
// more closely matches Node (#333)
33
// plus 'mjs' for native modules! (#939)
34
extensions: ['.mjs', '.js', '.json', '.node'],
35
},
36
config,
37
{
38
// path.resolve will handle paths relative to CWD
39
basedir: path.dirname(path.resolve(file)),
40
packageFilter,
41
});
42
}
43
44
function identity(x) { return x; }
45
46
function packageFilter(pkg, dir, config) {
47
let found = false;
48
const file = path.join(dir, 'dummy.js');
49
if (pkg.module) {
50
try {
51
resolve.sync(String(pkg.module).replace(/^(?:\.\/)?/, './'), opts(file, config, identity));
52
pkg.main = pkg.module;
53
found = true;
54
} catch (err) {
55
log('resolve threw error trying to find pkg.module:', err);
56
}
57
}
58
if (!found && pkg['jsnext:main']) {
59
try {
60
resolve.sync(String(pkg['jsnext:main']).replace(/^(?:\.\/)?/, './'), opts(file, config, identity));
61
pkg.main = pkg['jsnext:main'];
62
found = true;
63
} catch (err) {
64
log('resolve threw error trying to find pkg[\'jsnext:main\']:', err);
65
}
66
}
67
return pkg;
68
}
69
70