Skip to content

Commit 414c923

Browse files
Aghassiljharb
andcommitted
[New] enable passing cwd as an option to eslint-import-resolver-webpack
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 <[email protected]>
1 parent 8224e51 commit 414c923

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

resolvers/webpack/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## Unreleased
77

8+
### Added
9+
- [New] enable passing cwd as an option to `eslint-import-resolver-webpack` ([#1503], thanks [@Aghassi])
10+
811
## 0.11.1 - 2019-04-13
912

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

123+
[#1503]: https://github.com/benmosher/eslint-plugin-import/pull/1503
120124
[#1297]: https://github.com/benmosher/eslint-plugin-import/pull/1297
121125
[#1261]: https://github.com/benmosher/eslint-plugin-import/pull/1261
122126
[#1220]: https://github.com/benmosher/eslint-plugin-import/pull/1220
@@ -166,3 +170,4 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
166170
[@mattkrick]: https://github.com/mattkrick
167171
[@idudinov]: https://github.com/idudinov
168172
[@keann]: https://github.com/keann
173+
[@Aghassi]: https://github.com/Aghassi

resolvers/webpack/index.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ exports.interfaceVersion = 2
2020
* resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js'
2121
* @param {string} source - the module to resolve; i.e './some-module'
2222
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
23-
* TODO: take options as a third param, with webpack config file name
23+
* @param {object} settings - the webpack config file name, as well as cwd
24+
* @example
25+
* options: {
26+
* // Path to the webpack config
27+
* config: 'webpack.config.js',
28+
* // Path to be used to determine where to resolve webpack from
29+
* // (may differ from the cwd in some cases)
30+
* cwd: process.cwd()
31+
* }
2432
* @return {string?} the resolved path to source, undefined if not resolved, or null
2533
* if resolved to a non-FS resource (i.e. script tag at page load)
2634
*/
@@ -41,6 +49,11 @@ exports.resolve = function (source, file, settings) {
4149
var webpackConfig
4250

4351
var configPath = get(settings, 'config')
52+
/**
53+
* Attempt to set the current working directory.
54+
* If none is passed, default to the `cwd` where the config is located.
55+
*/
56+
, cwd = get(settings, 'cwd')
4457
, configIndex = get(settings, 'config-index')
4558
, env = get(settings, 'env')
4659
, argv = get(settings, 'argv', {})
@@ -114,7 +127,7 @@ exports.resolve = function (source, file, settings) {
114127
}
115128

116129
// otherwise, resolve "normally"
117-
var resolveSync = getResolveSync(configPath, webpackConfig)
130+
var resolveSync = getResolveSync(configPath, webpackConfig, cwd)
118131

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

131144
var MAX_CACHE = 10
132145
var _cache = []
133-
function getResolveSync(configPath, webpackConfig) {
146+
function getResolveSync(configPath, webpackConfig, cwd) {
134147
var cacheKey = { configPath: configPath, webpackConfig: webpackConfig }
135148
var cached = find(_cache, function (entry) { return isEqual(entry.key, cacheKey) })
136149
if (!cached) {
137150
cached = {
138151
key: cacheKey,
139-
value: createResolveSync(configPath, webpackConfig),
152+
value: createResolveSync(configPath, webpackConfig, cwd),
140153
}
141154
// put in front and pop last item
142155
if (_cache.unshift(cached) > MAX_CACHE) {
@@ -146,15 +159,18 @@ function getResolveSync(configPath, webpackConfig) {
146159
return cached.value
147160
}
148161

149-
function createResolveSync(configPath, webpackConfig) {
162+
function createResolveSync(configPath, webpackConfig, cwd) {
150163
var webpackRequire
151164
, basedir = null
152165

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

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

resolvers/webpack/test/example.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var path = require('path')
2+
3+
var resolve = require('../index').resolve
4+
5+
var file = path.join(__dirname, 'files', 'src', 'dummy.js')
6+
7+
var webpackDir = path.join(__dirname, "different-package-location")
8+
9+
console.log(resolve('main-module', file, { config: "webpack.config.js", cwd: webpackDir}))

resolvers/webpack/test/root.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var resolve = require('../index').resolve
66

77

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

1011
describe("root", function () {
1112
it("works", function () {
@@ -32,5 +33,13 @@ describe("root", function () {
3233
.property('path')
3334
.to.equal(path.join(__dirname, 'files', 'bower_components', 'typeahead.js'))
3435
})
35-
36+
it("supports passing a different directory to load webpack from", function () {
37+
// Webpack should still be able to resolve the config here
38+
expect(resolve('main-module', file, { config: "webpack.config.js", cwd: webpackDir}))
39+
.property('path')
40+
.to.equal(path.join(__dirname, 'files', 'src', 'main-module.js'))
41+
expect(resolve('typeahead', file, { config: "webpack.config.js", cwd: webpackDir}))
42+
.property('path')
43+
.to.equal(path.join(__dirname, 'files', 'bower_components', 'typeahead.js'))
44+
})
3645
})

0 commit comments

Comments
 (0)