From db8b95d7a5768c0ccd372b6b5228287a182c7679 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sun, 14 Jul 2024 13:45:26 +0000 Subject: [PATCH] [resolvers/webpack] [refactor] simplify loop --- resolvers/webpack/CHANGELOG.md | 5 ++++- resolvers/webpack/index.js | 31 ++++++++++++++++++------------- resolvers/webpack/package.json | 1 - 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/resolvers/webpack/CHANGELOG.md b/resolvers/webpack/CHANGELOG.md index 4fed046b4..cd49cc3f4 100644 --- a/resolvers/webpack/CHANGELOG.md +++ b/resolvers/webpack/CHANGELOG.md @@ -5,11 +5,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ## Unreleased +- [refactor] simplify loop ([#3029], thanks [@fregante]) + ## 0.13.8 - 2023-10-22 - [refactor] use `hasown` instead of `has` - [deps] update `array.prototype.find`, `is-core-module`, `resolve` - ## 0.13.7 - 2023-08-19 - [fix] use the `dirname` of the `configPath` as `basedir` ([#2859]) @@ -178,6 +179,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ### Added - `interpret` configs (such as `.babel.js`). Thanks to [@gausie] for the initial PR ([#164], ages ago! 😅) and [@jquense] for tests ([#278]). +[#3029]: https://github.com/import-js/eslint-plugin-import/pull/3029 [#2287]: https://github.com/import-js/eslint-plugin-import/pull/2287 [#2023]: https://github.com/import-js/eslint-plugin-import/pull/2023 [#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 [@benmvp]: https://github.com/benmvp [@daltones]: https://github.com/daltones [@echenley]: https://github.com/echenley +[@fregante]: https://github.com/fregante [@gausie]: https://github.com/gausie [@grahamb]: https://github.com/grahamb [@graingert]: https://github.com/graingert diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index da16eda59..eabe3cf33 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -3,7 +3,6 @@ const findRoot = require('find-root'); const path = require('path'); const isEqual = require('lodash/isEqual'); -const find = require('array.prototype.find'); const interpret = require('interpret'); const fs = require('fs'); const isCore = require('is-core-module'); @@ -293,17 +292,20 @@ const MAX_CACHE = 10; const _cache = []; function getResolveSync(configPath, webpackConfig, cwd) { const cacheKey = { configPath, webpackConfig }; - let cached = find(_cache, function (entry) { return isEqual(entry.key, cacheKey); }); - if (!cached) { - cached = { - key: cacheKey, - value: createResolveSync(configPath, webpackConfig, cwd), - }; - // put in front and pop last item - if (_cache.unshift(cached) > MAX_CACHE) { - _cache.pop(); + for (let i = 0; i < _cache.length; i++) { + if (isEqual(_cache[i].key, cacheKey)) { + return _cache[i].value; } } + + const cached = { + key: cacheKey, + value: createResolveSync(configPath, webpackConfig, cwd), + }; + // put in front and pop last item + if (_cache.unshift(cached) > MAX_CACHE) { + _cache.pop(); + } return cached.value; } @@ -409,9 +411,12 @@ exports.resolve = function (source, file, settings) { if (typeof configIndex !== 'undefined' && webpackConfig.length > configIndex) { webpackConfig = webpackConfig[configIndex]; } else { - webpackConfig = find(webpackConfig, function findFirstWithResolve(config) { - return !!config.resolve; - }); + for (let i = 0; i < webpackConfig.length; i++) { + if (webpackConfig[i].resolve) { + webpackConfig = webpackConfig[i]; + break; + } + } } } diff --git a/resolvers/webpack/package.json b/resolvers/webpack/package.json index 7f8cb718f..38465bcde 100644 --- a/resolvers/webpack/package.json +++ b/resolvers/webpack/package.json @@ -31,7 +31,6 @@ }, "homepage": "https://github.com/import-js/eslint-plugin-import/tree/HEAD/resolvers/webpack", "dependencies": { - "array.prototype.find": "^2.2.2", "debug": "^3.2.7", "enhanced-resolve": "^0.9.1", "find-root": "^1.1.0",