Skip to content

Commit

Permalink
add webpack resolver option
Browse files Browse the repository at this point in the history
  • Loading branch information
dflupu committed Feb 13, 2020
1 parent 74b34bf commit f33a474
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
31 changes: 27 additions & 4 deletions lib/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const arrify = require('arrify');
const mergeWith = require('lodash/mergeWith');
const multimatch = require('multimatch');
const pathExists = require('path-exists');
const findUp = require('find-up');
const pkgConf = require('pkg-conf');
const findCacheDir = require('find-cache-dir');
const resolveFrom = require('resolve-from');
Expand Down Expand Up @@ -271,14 +272,13 @@ const buildConfig = options => {
Object.assign(config.rules, options.rules);
}

if (options.settings) {
config.baseConfig.settings = options.settings;
}

if (options.parser) {
config.baseConfig.parser = options.parser;
}

config.baseConfig.settings = options.settings || {};
config.baseConfig.settings['import/resolver'] = gatherImportResolvers(config, options);

if (options.extends && options.extends.length > 0) {
// TODO: This logic needs to be improved, preferably use the same code as ESLint
// user's configs must be resolved to their absolute paths
Expand Down Expand Up @@ -355,6 +355,29 @@ const findApplicableOverrides = (path, overrides) => {
};
};

const gatherImportResolvers = (config, options) => {
let importResolvers = config.baseConfig.settings['import/resolver'] || {};

// Convert import/resolver: 'x' => import/resolver: {x: {}}
if (typeof importResolvers === 'string') {
const resolverName = importResolvers;
importResolvers = {};
importResolvers[resolverName] = {};
}

if (options.webpack) {
importResolvers.webpack = options.webpack === true ? {} : options.webpack;
} else if (options.webpack !== false && !importResolvers.webpack) {
// If a webpack config file exists, add the import resolver automatically
const webpackConfigPath = findUp.sync('webpack.config.js', {cwd: options.cwd});
if (webpackConfigPath) {
importResolvers.webpack = {config: webpackConfigPath};
}
}

return importResolvers;
};

const mergeApplicableOverrides = (baseOptions, applicableOverrides) => {
applicableOverrides = applicableOverrides.map(override => normalizeOptions(override));
const overrides = [emptyOptions(), baseOptions].concat(applicableOverrides, mergeFn);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"eslint-plugin-unicorn": "^16.1.1",
"find-cache-dir": "^3.0.0",
"get-stdin": "^7.0.0",
"find-up": "^4.1.0",
"globby": "^9.0.0",
"has-flag": "^4.0.0",
"lodash": "^4.17.15",
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ Enforce ES2015+ rules. Disabling this will make it not *enforce* ES2015+ syntax

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

### webpack

Type: `boolean | object`

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.

## TypeScript and Flow

### TypeScript
Expand Down
Empty file.
25 changes: 24 additions & 1 deletion test/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,34 @@ test('buildConfig: parser', t => {
});

test('buildConfig: settings', t => {
const settings = {'import/resolver': 'webpack'};
const settings = {'import/resolver': {webpack: {}}};
const config = manager.buildConfig({settings});
t.deepEqual(config.baseConfig.settings, settings);
});

test('buildConfig: finds webpack config file', t => {
const cwd = path.resolve('fixtures', 'webpack-config');
const config = manager.buildConfig({cwd});
const expected = {webpack: {config: path.resolve(cwd, 'webpack.config.js')}};
t.deepEqual(config.baseConfig.settings['import/resolver'], expected);
});

test('buildConfig: webpack option sets resolver', t => {
const config = manager.buildConfig({webpack: true, settings: {'import/resolver': 'node'}});
t.deepEqual(config.baseConfig.settings['import/resolver'], {webpack: {}, node: {}});
});

test('buildConfig: webpack option handles object values', t => {
const config = manager.buildConfig({webpack: {foo: 1}, settings: {'import/resolver': 'node'}});
t.deepEqual(config.baseConfig.settings['import/resolver'], {webpack: {foo: 1}, node: {}});
});

test('buildConfig: webpack resolver is not added automatically if webpack option is set to false', t => {
const cwd = path.resolve('fixtures', 'webpack-config');
const config = manager.buildConfig({cwd, webpack: false, settings: {}});
t.deepEqual(config.baseConfig.settings['import/resolver'], {});
});

test('buildConfig: extends', t => {
const config = manager.buildConfig({extends: [
'plugin:foo/bar',
Expand Down

0 comments on commit f33a474

Please sign in to comment.