From cb47d9fd796c2e59ad68b3207d1079fcd3adcb37 Mon Sep 17 00:00:00 2001 From: Jason Moon Date: Sat, 25 Mar 2017 00:41:26 -0700 Subject: [PATCH] ESLint: Use custom module resolver for ESLint 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. --- .eslint-resolver.js | 55 +++++++++++++++++++++++++++++++++++++++++++++ .eslintrc.js | 17 +++++--------- 2 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 .eslint-resolver.js diff --git a/.eslint-resolver.js b/.eslint-resolver.js new file mode 100644 index 0000000000000..41f6ea22ef000 --- /dev/null +++ b/.eslint-resolver.js @@ -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 ); + } +} diff --git a/.eslintrc.js b/.eslintrc.js index a177f11510278..aedb4f50f1111 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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, @@ -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,