Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame^] | 1 | var resolve = require('resolve') |
| 2 | , path = require('path') |
| 3 | |
| 4 | var log = require('debug')('eslint-plugin-import:resolver:node') |
| 5 | |
| 6 | exports.interfaceVersion = 2 |
| 7 | |
| 8 | exports.resolve = function (source, file, config) { |
| 9 | log('Resolving:', source, 'from:', file) |
| 10 | var resolvedPath |
| 11 | |
| 12 | if (resolve.isCore(source)) { |
| 13 | log('resolved to core') |
| 14 | return { found: true, path: null } |
| 15 | } |
| 16 | |
| 17 | try { |
| 18 | resolvedPath = resolve.sync(source, opts(file, config)) |
| 19 | log('Resolved to:', resolvedPath) |
| 20 | return { found: true, path: resolvedPath } |
| 21 | } catch (err) { |
| 22 | log('resolve threw error:', err) |
| 23 | return { found: false } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | function opts(file, config) { |
| 28 | return Object.assign({ |
| 29 | // more closely matches Node (#333) |
| 30 | // plus 'mjs' for native modules! (#939) |
| 31 | extensions: ['.mjs', '.js', '.json'], |
| 32 | }, |
| 33 | config, |
| 34 | { |
| 35 | // path.resolve will handle paths relative to CWD |
| 36 | basedir: path.dirname(path.resolve(file)), |
| 37 | packageFilter: packageFilter, |
| 38 | |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | function packageFilter(pkg) { |
| 43 | if (pkg['jsnext:main']) { |
| 44 | pkg['main'] = pkg['jsnext:main'] |
| 45 | } |
| 46 | return pkg |
| 47 | } |