Skip to content

Commit aa000ab

Browse files
committed
add webpack resolver option
1 parent 7e56730 commit aa000ab

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

lib/options-manager.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const arrify = require('arrify');
55
const mergeWith = require('lodash.mergewith');
66
const multimatch = require('multimatch');
77
const pathExists = require('path-exists');
8+
const findUp = require('find-up');
89
const pkgConf = require('pkg-conf');
910
const findCacheDir = require('find-cache-dir');
1011
const resolveFrom = require('resolve-from');
@@ -243,8 +244,30 @@ const buildConfig = options => {
243244
Object.assign(config.rules, options.rules);
244245
}
245246

246-
if (options.settings) {
247-
config.baseConfig.settings = options.settings;
247+
config.baseConfig.settings = Object.assign({}, options.settings);
248+
let importResolver = config.baseConfig.settings['import/resolver'] || {};
249+
250+
// Convert import/resolver: 'x' => import/resolver: {x: {}}
251+
if (typeof importResolver === 'string') {
252+
const resolverName = importResolver;
253+
importResolver = {};
254+
importResolver[resolverName] = {};
255+
}
256+
257+
if (options.webpack) {
258+
importResolver.webpack = options.webpack === true ? {} : options.webpack;
259+
} else if (!importResolver.webpack) {
260+
// If a webpack config file exists, add the import resolver automatically
261+
const webpackConfigPath = findUp.sync('webpack.config.js', {cwd: options.cwd});
262+
if (webpackConfigPath) {
263+
importResolver.webpack = {config: webpackConfigPath};
264+
}
265+
}
266+
267+
if (Object.keys(importResolver).length > 0) {
268+
config.baseConfig.settings['import/resolver'] = importResolver;
269+
} else {
270+
delete config.baseConfig.settings['import/resolver'];
248271
}
249272

250273
if (options.parser) {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"eslint-config-prettier": "^3.3.0",
5656
"eslint-config-xo": "^0.26.0",
5757
"eslint-formatter-pretty": "^2.0.0",
58+
"eslint-import-resolver-webpack": "^0.11.0",
5859
"eslint-plugin-ava": "^5.1.0",
5960
"eslint-plugin-eslint-comments": "^3.0.1",
6061
"eslint-plugin-import": "^2.14.0",
@@ -64,6 +65,7 @@
6465
"eslint-plugin-promise": "^4.0.0",
6566
"eslint-plugin-unicorn": "^7.0.0",
6667
"find-cache-dir": "^2.0.0",
68+
"find-up": "^3.0.0",
6769
"get-stdin": "^6.0.0",
6870
"globby": "^9.0.0",
6971
"has-flag": "^3.0.0",

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ Enforce ES2015+ rules. Disabling this will make it not *enforce* ES2015+ syntax
266266

267267
*ES2015+ is parsed even without this option. You can already use ES2017 features like [`async`/`await`](https://github.com/lukehoban/ecmascript-asyncawait).
268268

269+
### webpack
270+
271+
Type: `boolean`, `Object`
272+
273+
Enable the ESLint webpack import resolver plugin. Value can be an `Object` with plugin configuration.
274+
275+
See [eslint-import-resolver-webpack](https://www.npmjs.com/package/eslint-import-resolver-webpack)
269276

270277
## TypeScript and Flow
271278

test/options-manager.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,23 @@ test('buildConfig: parser', t => {
342342
});
343343

344344
test('buildConfig: settings', t => {
345-
const settings = {'import/resolver': 'webpack'};
345+
const settings = {'import/resolver': {webpack: {}}};
346346
const config = manager.buildConfig({settings});
347347
t.deepEqual(config.baseConfig.settings, settings);
348348
});
349349

350+
test('buildConfig: finds webpack config file', t => {
351+
const cwd = path.resolve('fixtures', 'webpack-config');
352+
const config = manager.buildConfig({cwd});
353+
const expected = {webpack: {config: path.resolve(cwd, 'webpack.config.js')}};
354+
t.deepEqual(config.baseConfig.settings['import/resolver'], expected);
355+
});
356+
357+
test('buildConfig: webpack option sets resolver', t => {
358+
const config = manager.buildConfig({webpack: true, settings: {'import/resolver': 'node'}});
359+
t.deepEqual(config.baseConfig.settings['import/resolver'], {webpack: {}, node: {}});
360+
});
361+
350362
test('buildConfig: extends', t => {
351363
const config = manager.buildConfig({extends: [
352364
'plugin:foo/bar',

0 commit comments

Comments
 (0)