Skip to content

Commit db8b95d

Browse files
freganteljharb
authored andcommitted
[resolvers/webpack] [refactor] simplify loop
1 parent bdff75d commit db8b95d

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

resolvers/webpack/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
55

66
## Unreleased
77

8+
- [refactor] simplify loop ([#3029], thanks [@fregante])
9+
810
## 0.13.8 - 2023-10-22
911
- [refactor] use `hasown` instead of `has`
1012
- [deps] update `array.prototype.find`, `is-core-module`, `resolve`
1113

12-
1314
## 0.13.7 - 2023-08-19
1415
- [fix] use the `dirname` of the `configPath` as `basedir` ([#2859])
1516

@@ -178,6 +179,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
178179
### Added
179180
- `interpret` configs (such as `.babel.js`). Thanks to [@gausie] for the initial PR ([#164], ages ago! 😅) and [@jquense] for tests ([#278]).
180181

182+
[#3029]: https://github.com/import-js/eslint-plugin-import/pull/3029
181183
[#2287]: https://github.com/import-js/eslint-plugin-import/pull/2287
182184
[#2023]: https://github.com/import-js/eslint-plugin-import/pull/2023
183185
[#1967]: https://github.com/import-js/eslint-plugin-import/pull/1967
@@ -222,6 +224,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
222224
[@benmvp]: https://github.com/benmvp
223225
[@daltones]: https://github.com/daltones
224226
[@echenley]: https://github.com/echenley
227+
[@fregante]: https://github.com/fregante
225228
[@gausie]: https://github.com/gausie
226229
[@grahamb]: https://github.com/grahamb
227230
[@graingert]: https://github.com/graingert

resolvers/webpack/index.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const findRoot = require('find-root');
44
const path = require('path');
55
const isEqual = require('lodash/isEqual');
6-
const find = require('array.prototype.find');
76
const interpret = require('interpret');
87
const fs = require('fs');
98
const isCore = require('is-core-module');
@@ -293,17 +292,20 @@ const MAX_CACHE = 10;
293292
const _cache = [];
294293
function getResolveSync(configPath, webpackConfig, cwd) {
295294
const cacheKey = { configPath, webpackConfig };
296-
let cached = find(_cache, function (entry) { return isEqual(entry.key, cacheKey); });
297-
if (!cached) {
298-
cached = {
299-
key: cacheKey,
300-
value: createResolveSync(configPath, webpackConfig, cwd),
301-
};
302-
// put in front and pop last item
303-
if (_cache.unshift(cached) > MAX_CACHE) {
304-
_cache.pop();
295+
for (let i = 0; i < _cache.length; i++) {
296+
if (isEqual(_cache[i].key, cacheKey)) {
297+
return _cache[i].value;
305298
}
306299
}
300+
301+
const cached = {
302+
key: cacheKey,
303+
value: createResolveSync(configPath, webpackConfig, cwd),
304+
};
305+
// put in front and pop last item
306+
if (_cache.unshift(cached) > MAX_CACHE) {
307+
_cache.pop();
308+
}
307309
return cached.value;
308310
}
309311

@@ -409,9 +411,12 @@ exports.resolve = function (source, file, settings) {
409411
if (typeof configIndex !== 'undefined' && webpackConfig.length > configIndex) {
410412
webpackConfig = webpackConfig[configIndex];
411413
} else {
412-
webpackConfig = find(webpackConfig, function findFirstWithResolve(config) {
413-
return !!config.resolve;
414-
});
414+
for (let i = 0; i < webpackConfig.length; i++) {
415+
if (webpackConfig[i].resolve) {
416+
webpackConfig = webpackConfig[i];
417+
break;
418+
}
419+
}
415420
}
416421
}
417422

resolvers/webpack/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
},
3232
"homepage": "https://github.com/import-js/eslint-plugin-import/tree/HEAD/resolvers/webpack",
3333
"dependencies": {
34-
"array.prototype.find": "^2.2.2",
3534
"debug": "^3.2.7",
3635
"enhanced-resolve": "^0.9.1",
3736
"find-root": "^1.1.0",

0 commit comments

Comments
 (0)