Skip to content

Commit

Permalink
[New] enable passing cwd as an option to `eslint-import-resolver-webp…
Browse files Browse the repository at this point in the history
…ack`

This enables users to change the lookup of the webpack module
for the resolve functionality should it not be in the user's
local node_modules. This pertains to the case of a CLI where the
CLI may be in charge of webpack, and the user's repo doesn't have it.

Co-Authored-By: Jordan Harband <ljharb@gmail.com>
  • Loading branch information
Aghassi and ljharb committed Oct 12, 2019
1 parent 8224e51 commit 414c923
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
5 changes: 5 additions & 0 deletions resolvers/webpack/CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

### Added
- [New] enable passing cwd as an option to `eslint-import-resolver-webpack` ([#1503], thanks [@Aghassi])

## 0.11.1 - 2019-04-13

### Fixed
Expand Down Expand Up @@ -117,6 +120,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- `interpret` configs (such as `.babel.js`).
Thanks to [@gausie] for the initial PR ([#164], ages ago! 😅) and [@jquense] for tests ([#278]).

[#1503]: https://github.com/benmosher/eslint-plugin-import/pull/1503
[#1297]: https://github.com/benmosher/eslint-plugin-import/pull/1297
[#1261]: https://github.com/benmosher/eslint-plugin-import/pull/1261
[#1220]: https://github.com/benmosher/eslint-plugin-import/pull/1220
Expand Down Expand Up @@ -166,3 +170,4 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
[@mattkrick]: https://github.com/mattkrick
[@idudinov]: https://github.com/idudinov
[@keann]: https://github.com/keann
[@Aghassi]: https://github.com/Aghassi
28 changes: 22 additions & 6 deletions resolvers/webpack/index.js
Expand Up @@ -20,7 +20,15 @@ exports.interfaceVersion = 2
* resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js'
* @param {string} source - the module to resolve; i.e './some-module'
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
* TODO: take options as a third param, with webpack config file name
* @param {object} settings - the webpack config file name, as well as cwd
* @example
* options: {
* // Path to the webpack config
* config: 'webpack.config.js',
* // Path to be used to determine where to resolve webpack from
* // (may differ from the cwd in some cases)
* cwd: process.cwd()
* }
* @return {string?} the resolved path to source, undefined if not resolved, or null
* if resolved to a non-FS resource (i.e. script tag at page load)
*/
Expand All @@ -41,6 +49,11 @@ exports.resolve = function (source, file, settings) {
var webpackConfig

var configPath = get(settings, 'config')
/**
* Attempt to set the current working directory.
* If none is passed, default to the `cwd` where the config is located.
*/
, cwd = get(settings, 'cwd')
, configIndex = get(settings, 'config-index')
, env = get(settings, 'env')
, argv = get(settings, 'argv', {})
Expand Down Expand Up @@ -114,7 +127,7 @@ exports.resolve = function (source, file, settings) {
}

// otherwise, resolve "normally"
var resolveSync = getResolveSync(configPath, webpackConfig)
var resolveSync = getResolveSync(configPath, webpackConfig, cwd)

try {
return { found: true, path: resolveSync(path.dirname(file), source) }
Expand All @@ -130,13 +143,13 @@ exports.resolve = function (source, file, settings) {

var MAX_CACHE = 10
var _cache = []
function getResolveSync(configPath, webpackConfig) {
function getResolveSync(configPath, webpackConfig, cwd) {
var cacheKey = { configPath: configPath, webpackConfig: webpackConfig }
var cached = find(_cache, function (entry) { return isEqual(entry.key, cacheKey) })
if (!cached) {
cached = {
key: cacheKey,
value: createResolveSync(configPath, webpackConfig),
value: createResolveSync(configPath, webpackConfig, cwd),
}
// put in front and pop last item
if (_cache.unshift(cached) > MAX_CACHE) {
Expand All @@ -146,15 +159,18 @@ function getResolveSync(configPath, webpackConfig) {
return cached.value
}

function createResolveSync(configPath, webpackConfig) {
function createResolveSync(configPath, webpackConfig, cwd) {
var webpackRequire
, basedir = null

if (typeof configPath === 'string') {
basedir = path.dirname(configPath)
// This can be changed via the settings passed in when defining the resolver
basedir = cwd || configPath
log(`Attempting to load webpack path from ${basedir}`)
}

try {
// Attempt to resolve webpack from the given `basedir`
var webpackFilename = resolve.sync('webpack', { basedir, preserveSymlinks: false })
var webpackResolveOpts = { basedir: path.dirname(webpackFilename), preserveSymlinks: false }

Expand Down
9 changes: 9 additions & 0 deletions resolvers/webpack/test/example.js
@@ -0,0 +1,9 @@
var path = require('path')

var resolve = require('../index').resolve

var file = path.join(__dirname, 'files', 'src', 'dummy.js')

var webpackDir = path.join(__dirname, "different-package-location")

console.log(resolve('main-module', file, { config: "webpack.config.js", cwd: webpackDir}))
11 changes: 10 additions & 1 deletion resolvers/webpack/test/root.js
Expand Up @@ -6,6 +6,7 @@ var resolve = require('../index').resolve


var file = path.join(__dirname, 'files', 'src', 'dummy.js')
var webpackDir = path.join(__dirname, "different-package-location")

describe("root", function () {
it("works", function () {
Expand All @@ -32,5 +33,13 @@ describe("root", function () {
.property('path')
.to.equal(path.join(__dirname, 'files', 'bower_components', 'typeahead.js'))
})

it("supports passing a different directory to load webpack from", function () {
// Webpack should still be able to resolve the config here
expect(resolve('main-module', file, { config: "webpack.config.js", cwd: webpackDir}))
.property('path')
.to.equal(path.join(__dirname, 'files', 'src', 'main-module.js'))
expect(resolve('typeahead', file, { config: "webpack.config.js", cwd: webpackDir}))
.property('path')
.to.equal(path.join(__dirname, 'files', 'bower_components', 'typeahead.js'))
})
})

0 comments on commit 414c923

Please sign in to comment.