index.js 1.1 KB

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