Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Uses createRequireFromPath to resolve loaders #1591

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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