Skip to content

Commit 19dbc33

Browse files
committed
[resolvers/webpack] [refactor] misc cleanup
1 parent db8b95d commit 19dbc33

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

resolvers/webpack/index.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ const findRoot = require('find-root');
44
const path = require('path');
55
const isEqual = require('lodash/isEqual');
66
const interpret = require('interpret');
7-
const fs = require('fs');
7+
const existsSync = require('fs').existsSync;
88
const isCore = require('is-core-module');
99
const resolve = require('resolve/sync');
1010
const semver = require('semver');
1111
const hasOwn = require('hasown');
1212
const isRegex = require('is-regex');
13+
const isArray = Array.isArray;
14+
const keys = Object.keys;
15+
const assign = Object.assign;
1316

1417
const log = require('debug')('eslint-plugin-import:resolver:webpack');
1518

@@ -19,7 +22,7 @@ function registerCompiler(moduleDescriptor) {
1922
if (moduleDescriptor) {
2023
if (typeof moduleDescriptor === 'string') {
2124
require(moduleDescriptor);
22-
} else if (!Array.isArray(moduleDescriptor)) {
25+
} else if (!isArray(moduleDescriptor)) {
2326
moduleDescriptor.register(require(moduleDescriptor.module));
2427
} else {
2528
for (let i = 0; i < moduleDescriptor.length; i++) {
@@ -35,42 +38,34 @@ function registerCompiler(moduleDescriptor) {
3538
}
3639

3740
function findConfigPath(configPath, packageDir) {
38-
const extensions = Object.keys(interpret.extensions).sort(function (a, b) {
41+
const extensions = keys(interpret.extensions).sort(function (a, b) {
3942
return a === '.js' ? -1 : b === '.js' ? 1 : a.length - b.length;
4043
});
4144
let extension;
4245

4346
if (configPath) {
44-
// extensions is not reused below, so safe to mutate it here.
45-
extensions.reverse();
46-
extensions.forEach(function (maybeExtension) {
47-
if (extension) {
48-
return;
49-
}
50-
51-
if (configPath.substr(-maybeExtension.length) === maybeExtension) {
47+
for (let i = extensions.length - 1; i >= 0 && !extension; i--) {
48+
const maybeExtension = extensions[i];
49+
if (configPath.slice(-maybeExtension.length) === maybeExtension) {
5250
extension = maybeExtension;
5351
}
54-
});
52+
}
5553

5654
// see if we've got an absolute path
5755
if (!path.isAbsolute(configPath)) {
5856
configPath = path.join(packageDir, configPath);
5957
}
6058
} else {
61-
extensions.forEach(function (maybeExtension) {
62-
if (extension) {
63-
return;
64-
}
65-
59+
for (let i = 0; i < extensions.length && !extension; i++) {
60+
const maybeExtension = extensions[i];
6661
const maybePath = path.resolve(
6762
path.join(packageDir, 'webpack.config' + maybeExtension)
6863
);
69-
if (fs.existsSync(maybePath)) {
64+
if (existsSync(maybePath)) {
7065
configPath = maybePath;
7166
extension = maybeExtension;
7267
}
73-
});
68+
}
7469
}
7570

7671
registerCompiler(interpret.extensions[extension]);
@@ -84,7 +79,7 @@ function findExternal(source, externals, context, resolveSync) {
8479
if (typeof externals === 'string') { return source === externals; }
8580

8681
// array: recurse
87-
if (Array.isArray(externals)) {
82+
if (isArray(externals)) {
8883
return externals.some(function (e) { return findExternal(source, e, context, resolveSync); });
8984
}
9085

@@ -138,8 +133,9 @@ function findExternal(source, externals, context, resolveSync) {
138133

139134
// else, vanilla object
140135
for (const key in externals) {
141-
if (!hasOwn(externals, key)) { continue; }
142-
if (source === key) { return true; }
136+
if (hasOwn(externals, key) && source === key) {
137+
return true;
138+
}
143139
}
144140
return false;
145141
}
@@ -160,24 +156,30 @@ const webpack2DefaultResolveConfig = {
160156
function createWebpack2ResolveSync(webpackRequire, resolveConfig) {
161157
const EnhancedResolve = webpackRequire('enhanced-resolve');
162158

163-
return EnhancedResolve.create.sync(Object.assign({}, webpack2DefaultResolveConfig, resolveConfig));
159+
return EnhancedResolve.create.sync(assign({}, webpack2DefaultResolveConfig, resolveConfig));
164160
}
165161

166162
/**
167163
* webpack 1 defaults: https://webpack.github.io/docs/configuration.html#resolve-packagemains
168-
* @type {Array}
164+
* @type {string[]}
169165
*/
170166
const webpack1DefaultMains = [
171-
'webpack', 'browser', 'web', 'browserify', ['jam', 'main'], 'main',
167+
'webpack',
168+
'browser',
169+
'web',
170+
'browserify',
171+
['jam', 'main'],
172+
'main',
172173
];
173174

174175
/* eslint-disable */
175176
// from https://github.com/webpack/webpack/blob/v1.13.0/lib/WebpackOptionsApply.js#L365
176177
function makeRootPlugin(ModulesInRootPlugin, name, root) {
177178
if (typeof root === 'string') {
178179
return new ModulesInRootPlugin(name, root);
179-
} else if (Array.isArray(root)) {
180-
return function() {
180+
}
181+
if (isArray(root)) {
182+
return function () {
181183
root.forEach(function (root) {
182184
this.apply(new ModulesInRootPlugin(name, root));
183185
}, this);
@@ -236,7 +238,7 @@ function createWebpack1ResolveSync(webpackRequire, resolveConfig, plugins) {
236238
if (
237239
plugin.constructor
238240
&& plugin.constructor.name === 'ResolverPlugin'
239-
&& Array.isArray(plugin.plugins)
241+
&& isArray(plugin.plugins)
240242
) {
241243
resolvePlugins.push.apply(resolvePlugins, plugin.plugins);
242244
}
@@ -313,9 +315,9 @@ function getResolveSync(configPath, webpackConfig, cwd) {
313315
* Find the full path to 'source', given 'file' as a full reference path.
314316
*
315317
* resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js'
316-
* @param {string} source - the module to resolve; i.e './some-module'
317-
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
318-
* @param {object} settings - the webpack config file name, as well as cwd
318+
* @param {string} source - the module to resolve; i.e './some-module'
319+
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
320+
* @param {object} settings - the webpack config file name, as well as cwd
319321
* @example
320322
* options: {
321323
* // Path to the webpack config
@@ -399,7 +401,7 @@ exports.resolve = function (source, file, settings) {
399401
webpackConfig = webpackConfig(env, argv);
400402
}
401403

402-
if (Array.isArray(webpackConfig)) {
404+
if (isArray(webpackConfig)) {
403405
webpackConfig = webpackConfig.map((cfg) => {
404406
if (typeof cfg === 'function') {
405407
return cfg(env, argv);

0 commit comments

Comments
 (0)