@@ -4,12 +4,15 @@ const findRoot = require('find-root');
4
4
const path = require ( 'path' ) ;
5
5
const isEqual = require ( 'lodash/isEqual' ) ;
6
6
const interpret = require ( 'interpret' ) ;
7
- const fs = require ( 'fs' ) ;
7
+ const existsSync = require ( 'fs' ) . existsSync ;
8
8
const isCore = require ( 'is-core-module' ) ;
9
9
const resolve = require ( 'resolve/sync' ) ;
10
10
const semver = require ( 'semver' ) ;
11
11
const hasOwn = require ( 'hasown' ) ;
12
12
const isRegex = require ( 'is-regex' ) ;
13
+ const isArray = Array . isArray ;
14
+ const keys = Object . keys ;
15
+ const assign = Object . assign ;
13
16
14
17
const log = require ( 'debug' ) ( 'eslint-plugin-import:resolver:webpack' ) ;
15
18
@@ -19,7 +22,7 @@ function registerCompiler(moduleDescriptor) {
19
22
if ( moduleDescriptor ) {
20
23
if ( typeof moduleDescriptor === 'string' ) {
21
24
require ( moduleDescriptor ) ;
22
- } else if ( ! Array . isArray ( moduleDescriptor ) ) {
25
+ } else if ( ! isArray ( moduleDescriptor ) ) {
23
26
moduleDescriptor . register ( require ( moduleDescriptor . module ) ) ;
24
27
} else {
25
28
for ( let i = 0 ; i < moduleDescriptor . length ; i ++ ) {
@@ -35,42 +38,34 @@ function registerCompiler(moduleDescriptor) {
35
38
}
36
39
37
40
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 ) {
39
42
return a === '.js' ? - 1 : b === '.js' ? 1 : a . length - b . length ;
40
43
} ) ;
41
44
let extension ;
42
45
43
46
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 ) {
52
50
extension = maybeExtension ;
53
51
}
54
- } ) ;
52
+ }
55
53
56
54
// see if we've got an absolute path
57
55
if ( ! path . isAbsolute ( configPath ) ) {
58
56
configPath = path . join ( packageDir , configPath ) ;
59
57
}
60
58
} 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 ] ;
66
61
const maybePath = path . resolve (
67
62
path . join ( packageDir , 'webpack.config' + maybeExtension )
68
63
) ;
69
- if ( fs . existsSync ( maybePath ) ) {
64
+ if ( existsSync ( maybePath ) ) {
70
65
configPath = maybePath ;
71
66
extension = maybeExtension ;
72
67
}
73
- } ) ;
68
+ }
74
69
}
75
70
76
71
registerCompiler ( interpret . extensions [ extension ] ) ;
@@ -84,7 +79,7 @@ function findExternal(source, externals, context, resolveSync) {
84
79
if ( typeof externals === 'string' ) { return source === externals ; }
85
80
86
81
// array: recurse
87
- if ( Array . isArray ( externals ) ) {
82
+ if ( isArray ( externals ) ) {
88
83
return externals . some ( function ( e ) { return findExternal ( source , e , context , resolveSync ) ; } ) ;
89
84
}
90
85
@@ -138,8 +133,9 @@ function findExternal(source, externals, context, resolveSync) {
138
133
139
134
// else, vanilla object
140
135
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
+ }
143
139
}
144
140
return false ;
145
141
}
@@ -160,24 +156,30 @@ const webpack2DefaultResolveConfig = {
160
156
function createWebpack2ResolveSync ( webpackRequire , resolveConfig ) {
161
157
const EnhancedResolve = webpackRequire ( 'enhanced-resolve' ) ;
162
158
163
- return EnhancedResolve . create . sync ( Object . assign ( { } , webpack2DefaultResolveConfig , resolveConfig ) ) ;
159
+ return EnhancedResolve . create . sync ( assign ( { } , webpack2DefaultResolveConfig , resolveConfig ) ) ;
164
160
}
165
161
166
162
/**
167
163
* webpack 1 defaults: https://webpack.github.io/docs/configuration.html#resolve-packagemains
168
- * @type {Array }
164
+ * @type {string[] }
169
165
*/
170
166
const webpack1DefaultMains = [
171
- 'webpack' , 'browser' , 'web' , 'browserify' , [ 'jam' , 'main' ] , 'main' ,
167
+ 'webpack' ,
168
+ 'browser' ,
169
+ 'web' ,
170
+ 'browserify' ,
171
+ [ 'jam' , 'main' ] ,
172
+ 'main' ,
172
173
] ;
173
174
174
175
/* eslint-disable */
175
176
// from https://github.com/webpack/webpack/blob/v1.13.0/lib/WebpackOptionsApply.js#L365
176
177
function makeRootPlugin ( ModulesInRootPlugin , name , root ) {
177
178
if ( typeof root === 'string' ) {
178
179
return new ModulesInRootPlugin ( name , root ) ;
179
- } else if ( Array . isArray ( root ) ) {
180
- return function ( ) {
180
+ }
181
+ if ( isArray ( root ) ) {
182
+ return function ( ) {
181
183
root . forEach ( function ( root ) {
182
184
this . apply ( new ModulesInRootPlugin ( name , root ) ) ;
183
185
} , this ) ;
@@ -236,7 +238,7 @@ function createWebpack1ResolveSync(webpackRequire, resolveConfig, plugins) {
236
238
if (
237
239
plugin . constructor
238
240
&& plugin . constructor . name === 'ResolverPlugin'
239
- && Array . isArray ( plugin . plugins )
241
+ && isArray ( plugin . plugins )
240
242
) {
241
243
resolvePlugins . push . apply ( resolvePlugins , plugin . plugins ) ;
242
244
}
@@ -313,9 +315,9 @@ function getResolveSync(configPath, webpackConfig, cwd) {
313
315
* Find the full path to 'source', given 'file' as a full reference path.
314
316
*
315
317
* 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
319
321
* @example
320
322
* options: {
321
323
* // Path to the webpack config
@@ -399,7 +401,7 @@ exports.resolve = function (source, file, settings) {
399
401
webpackConfig = webpackConfig ( env , argv ) ;
400
402
}
401
403
402
- if ( Array . isArray ( webpackConfig ) ) {
404
+ if ( isArray ( webpackConfig ) ) {
403
405
webpackConfig = webpackConfig . map ( ( cfg ) => {
404
406
if ( typeof cfg === 'function' ) {
405
407
return cfg ( env , argv ) ;
0 commit comments