Skip to content

Commit f33a474

Browse files
committed
add webpack resolver option
1 parent 74b34bf commit f33a474

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

lib/options-manager.js

Lines changed: 27 additions & 4 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');
@@ -271,14 +272,13 @@ const buildConfig = options => {
271272
Object.assign(config.rules, options.rules);
272273
}
273274

274-
if (options.settings) {
275-
config.baseConfig.settings = options.settings;
276-
}
277-
278275
if (options.parser) {
279276
config.baseConfig.parser = options.parser;
280277
}
281278

279+
config.baseConfig.settings = options.settings || {};
280+
config.baseConfig.settings['import/resolver'] = gatherImportResolvers(config, options);
281+
282282
if (options.extends && options.extends.length > 0) {
283283
// TODO: This logic needs to be improved, preferably use the same code as ESLint
284284
// user's configs must be resolved to their absolute paths
@@ -355,6 +355,29 @@ const findApplicableOverrides = (path, overrides) => {
355355
};
356356
};
357357

358+
const gatherImportResolvers = (config, options) => {
359+
let importResolvers = config.baseConfig.settings['import/resolver'] || {};
360+
361+
// Convert import/resolver: 'x' => import/resolver: {x: {}}
362+
if (typeof importResolvers === 'string') {
363+
const resolverName = importResolvers;
364+
importResolvers = {};
365+
importResolvers[resolverName] = {};
366+
}
367+
368+
if (options.webpack) {
369+
importResolvers.webpack = options.webpack === true ? {} : options.webpack;
370+
} else if (options.webpack !== false && !importResolvers.webpack) {
371+
// If a webpack config file exists, add the import resolver automatically
372+
const webpackConfigPath = findUp.sync('webpack.config.js', {cwd: options.cwd});
373+
if (webpackConfigPath) {
374+
importResolvers.webpack = {config: webpackConfigPath};
375+
}
376+
}
377+
378+
return importResolvers;
379+
};
380+
358381
const mergeApplicableOverrides = (baseOptions, applicableOverrides) => {
359382
applicableOverrides = applicableOverrides.map(override => normalizeOptions(override));
360383
const overrides = [emptyOptions(), baseOptions].concat(applicableOverrides, mergeFn);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"eslint-plugin-unicorn": "^16.1.1",
6666
"find-cache-dir": "^3.0.0",
6767
"get-stdin": "^7.0.0",
68+
"find-up": "^4.1.0",
6869
"globby": "^9.0.0",
6970
"has-flag": "^4.0.0",
7071
"lodash": "^4.17.15",

readme.md

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

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

253+
### webpack
254+
255+
Type: `boolean | object`
256+
257+
Use [eslint-import-resolver-webpack](https://github.com/benmosher/eslint-plugin-import) to resolve import search paths. Value can be an object with plugin configuration.
258+
253259
## TypeScript and Flow
254260

255261
### TypeScript

test/fixtures/webpack-config/webpack.config.js

Whitespace-only changes.

test/options-manager.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,34 @@ test('buildConfig: parser', t => {
353353
});
354354

355355
test('buildConfig: settings', t => {
356-
const settings = {'import/resolver': 'webpack'};
356+
const settings = {'import/resolver': {webpack: {}}};
357357
const config = manager.buildConfig({settings});
358358
t.deepEqual(config.baseConfig.settings, settings);
359359
});
360360

361+
test('buildConfig: finds webpack config file', t => {
362+
const cwd = path.resolve('fixtures', 'webpack-config');
363+
const config = manager.buildConfig({cwd});
364+
const expected = {webpack: {config: path.resolve(cwd, 'webpack.config.js')}};
365+
t.deepEqual(config.baseConfig.settings['import/resolver'], expected);
366+
});
367+
368+
test('buildConfig: webpack option sets resolver', t => {
369+
const config = manager.buildConfig({webpack: true, settings: {'import/resolver': 'node'}});
370+
t.deepEqual(config.baseConfig.settings['import/resolver'], {webpack: {}, node: {}});
371+
});
372+
373+
test('buildConfig: webpack option handles object values', t => {
374+
const config = manager.buildConfig({webpack: {foo: 1}, settings: {'import/resolver': 'node'}});
375+
t.deepEqual(config.baseConfig.settings['import/resolver'], {webpack: {foo: 1}, node: {}});
376+
});
377+
378+
test('buildConfig: webpack resolver is not added automatically if webpack option is set to false', t => {
379+
const cwd = path.resolve('fixtures', 'webpack-config');
380+
const config = manager.buildConfig({cwd, webpack: false, settings: {}});
381+
t.deepEqual(config.baseConfig.settings['import/resolver'], {});
382+
});
383+
361384
test('buildConfig: extends', t => {
362385
const config = manager.buildConfig({extends: [
363386
'plugin:foo/bar',

0 commit comments

Comments
 (0)