Skip to content

Commit

Permalink
utils: Uses createRequireFromPath to resolve loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis authored and ljharb committed Jan 2, 2020
1 parent 16ae652 commit fb0cbeb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/src/core/resolve.js
Expand Up @@ -94,6 +94,14 @@ describe('resolve', function () {
)).to.equal(utils.testFilePath('./bar.jsx'))
})

it('finds resolvers from the source files rather than eslint-module-utils', function () {
const testContext = utils.testContext({ 'import/resolver': { 'foo': {} } })

expect(resolve( '../files/foo'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('foo.js') } }),
)).to.equal(utils.testFilePath('./bar.jsx'))
})

it('reports invalid import/resolver config', function () {
const testContext = utils.testContext({ 'import/resolver': 123.456 })
const testContextReports = []
Expand Down
8 changes: 4 additions & 4 deletions tests/src/rules/no-unresolved.js
Expand Up @@ -343,19 +343,19 @@ ruleTester.run('no-unresolved unknown resolver', rule, {
// logs resolver load error
test({
code: 'import "./malformed.js"',
settings: { 'import/resolver': 'foo' },
settings: { 'import/resolver': 'doesnt-exist' },
errors: [
`Resolve error: unable to load resolver "foo".`,
`Resolve error: unable to load resolver "doesnt-exist".`,
`Unable to resolve path to module './malformed.js'.`,
],
}),

// only logs resolver message once
test({
code: 'import "./malformed.js"; import "./fake.js"',
settings: { 'import/resolver': 'foo' },
settings: { 'import/resolver': 'doesnt-exist' },
errors: [
`Resolve error: unable to load resolver "foo".`,
`Resolve error: unable to load resolver "doesnt-exist".`,
`Unable to resolve path to module './malformed.js'.`,
`Unable to resolve path to module './fake.js'.`,
],
Expand Down
6 changes: 5 additions & 1 deletion utils/CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

### Fixed
- Uses createRequireFromPath to resolve loaders ([#1591], thanks [@arcanis])

## v2.5.0 - 2019-12-07

### Added
Expand Down Expand Up @@ -59,7 +62,7 @@ Yanked due to critical issue with cache key resulting from #839.
- `unambiguous.test()` regex is now properly in multiline mode



[#1591]: https://github.com/benmosher/eslint-plugin-import/pull/1591
[#1551]: https://github.com/benmosher/eslint-plugin-import/pull/1551
[#1435]: https://github.com/benmosher/eslint-plugin-import/pull/1435
[#1409]: https://github.com/benmosher/eslint-plugin-import/pull/1409
Expand All @@ -77,3 +80,4 @@ Yanked due to critical issue with cache key resulting from #839.
[@christophercurrie]: https://github.com/christophercurrie
[@brettz9]: https://github.com/brettz9
[@JounQin]: https://github.com/JounQin
[@arcanis]: https://github.com/arcanis
24 changes: 20 additions & 4 deletions utils/resolve.js
Expand Up @@ -4,6 +4,7 @@ exports.__esModule = true
const pkgDir = require('pkg-dir')

const fs = require('fs')
const Module = require('module')
const path = require('path')

const hashObject = require('./hash').hashObject
Expand All @@ -14,11 +15,26 @@ exports.CASE_SENSITIVE_FS = CASE_SENSITIVE_FS

const fileExistsCache = new ModuleCache()

function tryRequire(target) {
// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
const createRequireFromPath = Module.createRequireFromPath || function (filename) {
const mod = new Module(filename, null)
mod.filename = filename
mod.paths = Module._nodeModulePaths(path.dirname(filename))

mod._compile(`module.exports = require;`, filename)

return mod.exports
}

function tryRequire(target, sourceFile) {
let resolved
try {
// Check if the target exists
resolved = require.resolve(target)
if (sourceFile != null) {
resolved = createRequireFromPath(sourceFile).resolve(target)
} else {
resolved = require.resolve(target)
}
} catch(e) {
// If the target does not exist then just return undefined
return undefined
Expand Down Expand Up @@ -154,8 +170,8 @@ function getBaseDir(sourceFile) {
}
function requireResolver(name, sourceFile) {
// Try to resolve package with conventional name
let resolver = tryRequire(`eslint-import-resolver-${name}`) ||
tryRequire(name) ||
let resolver = tryRequire(`eslint-import-resolver-${name}`, sourceFile) ||
tryRequire(name, sourceFile) ||
tryRequire(path.resolve(getBaseDir(sourceFile), name))

if (!resolver) {
Expand Down

0 comments on commit fb0cbeb

Please sign in to comment.