Skip to content

Commit

Permalink
ESLint: Use custom module resolver for ESLint
Browse files Browse the repository at this point in the history
This custom resolver inspects the file's path to decide which off-the-shelf resolver would be appropriate. For files from the client folder, this resolver uses Webpack's resolver. For other files, this resolver uses Node's resolver.
  • Loading branch information
jsnmoon committed Mar 27, 2017
1 parent 04394ac commit cb47d9f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
55 changes: 55 additions & 0 deletions .eslint-resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* External dependencies
*/
const assign = require ( 'lodash/assign' );
const debugFactory = require( 'debug' );
const nodeResolver = require( 'eslint-import-resolver-node' );
const path = require( 'path' );
const webpackResolver = require( 'eslint-import-resolver-webpack' );

/**
* Internal dependencies
*/
const webpackResolveConfig = require( './webpack.config.resolve' );

/**
* Module variables
*/
const log = debugFactory('eslint-plugin-import:resolver:wp-calypso-resolver');

const nodeResolverConfig = {
extensions: [
'.js',
'.jsx',
],
moduleDirectory: [
'node_modules',
'server'
],
paths: [
path.join( __dirname, 'test' ),
path.join( __dirname, 'server' ),
path.join( __dirname, 'client' ),
]
};

const webpackResolverConfig = {
config: {
resolve: webpackResolveConfig,
},
};

exports.interfaceVersion = 2

exports.resolve = function (source, file, config) {
log('Resolving', source, 'from:', file);
// If the file is from the client folder, use Webpack for resolution.
// Otherwise, use Node's resolver.
if (file.indexOf('/client/') > 0) {
log('Client file detected, resolving with Webpack');
return webpackResolver.resolve( source, file, webpackResolverConfig );
} else {
log('Resolving with Node resolver');
return nodeResolver.resolve( source, file, nodeResolverConfig );
}
}
17 changes: 6 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable import/no-commonjs */
const resolveConfig = require( './webpack.config.resolve.js' );

const path = require( 'path' );
const customResolverPath = path.join( __dirname, './.eslint-resolver.js' );

module.exports = {
root: true,
Expand All @@ -17,21 +19,14 @@ module.exports = {
'import'
],
settings: {
'import/ignore': [
'server\/.*$',
],
'import/resolver': {
webpack: {
config: {
resolve: resolveConfig,
}
},
[ customResolverPath ]: {}
}
},
rules: {
camelcase: 0, // REST API objects include underscores
// NOTE: Some import rules are errors in client and warnings in server
// due to how CommonJS and ES module notations inter-op.
// NOTE: Some import rules are errors in client (Webpack's resolution) and
// warnings in server (Node's module resolution).
'import/default': 2,
'import/export': 2,
'import/named': 2,
Expand Down

0 comments on commit cb47d9f

Please sign in to comment.