Skip to content

Commit

Permalink
[resolvers/webpack] [new] add cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyab authored and ljharb committed Nov 12, 2024
1 parent 67cc798 commit ac7d396
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions resolvers/webpack/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](https://keepachangelog.com).

## Unreleased
- [new] add cache option ([#3100], thanks [@seiyab])

## 0.13.9 - 2024-09-02
- [refactor] simplify loop ([#3029], thanks [@fregante])
Expand Down Expand Up @@ -182,6 +183,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]).

[#3100]: https://github.com/import-js/eslint-plugin-import/pull/3100
[#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
Expand Down Expand Up @@ -247,6 +249,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
[@Rogeres]: https://github.com/Rogeres
[@Satyam]: https://github.com/Satyam
[@Schweinepriester]: https://github.com/Schweinepriester
[@seiyab]: https://github.com/seiyab
[@SkeLLLa]: https://github.com/SkeLLLa
[@taion]: https://github.com/taion
[@toshafed]: https://github.com/toshafed
11 changes: 11 additions & 0 deletions resolvers/webpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ settings:
production: true
```

If your config is set as a function, it will be evaluated at every resolution. You have an option to prevent this by caching it using the `cache` parameter:

```yaml
---
settings:
import/resolver:
webpack:
config: 'webpack.config.js'
cache: true
```

## Support

[Get supported eslint-import-resolver-webpack with the Tidelift Subscription](https://tidelift.com/subscription/pkg/npm-eslint-import-resolver-webpack?utm_source=npm-eslint-import-resolver-webpack&utm_medium=referral&utm_campaign=readme)
21 changes: 20 additions & 1 deletion resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,22 @@ function getResolveSync(configPath, webpackConfig, cwd) {
return cached.value;
}

const _evalCache = new Map();
function evaluateFunctionConfigCached(configPath, webpackConfig, env, argv) {
const cacheKey = JSON.stringify({ configPath, args: [env, argv] });
if (_evalCache.has(cacheKey)) {
return _evalCache.get(cacheKey);

Check warning on line 318 in resolvers/webpack/index.js

View check run for this annotation

Codecov / codecov/patch

resolvers/webpack/index.js#L315-L318

Added lines #L315 - L318 were not covered by tests
}
const cached = webpackConfig(env, argv);
_evalCache.set(cacheKey, cached);

Check warning on line 321 in resolvers/webpack/index.js

View check run for this annotation

Codecov / codecov/patch

resolvers/webpack/index.js#L320-L321

Added lines #L320 - L321 were not covered by tests

while (_evalCache.size > MAX_CACHE) {

Check warning on line 323 in resolvers/webpack/index.js

View check run for this annotation

Codecov / codecov/patch

resolvers/webpack/index.js#L323

Added line #L323 was not covered by tests
// remove oldest item
_evalCache.delete(_evalCache.keys().next().value);

Check warning on line 325 in resolvers/webpack/index.js

View check run for this annotation

Codecov / codecov/patch

resolvers/webpack/index.js#L325

Added line #L325 was not covered by tests
}
return cached;

Check warning on line 327 in resolvers/webpack/index.js

View check run for this annotation

Codecov / codecov/patch

resolvers/webpack/index.js#L327

Added line #L327 was not covered by tests
}

/**
* Find the full path to 'source', given 'file' as a full reference path.
*
Expand Down Expand Up @@ -354,6 +370,7 @@ exports.resolve = function (source, file, settings) {
const configIndex = settings && settings['config-index'];
const env = settings && settings.env;
const argv = settings && typeof settings.argv !== 'undefined' ? settings.argv : {};
const shouldCacheFunctionConfig = settings && settings.cache;
let packageDir;

let configPath = typeof _configPath === 'string' && _configPath.startsWith('.')
Expand Down Expand Up @@ -398,7 +415,9 @@ exports.resolve = function (source, file, settings) {
}

if (typeof webpackConfig === 'function') {
webpackConfig = webpackConfig(env, argv);
webpackConfig = shouldCacheFunctionConfig
? evaluateFunctionConfigCached(configPath, webpackConfig, env, argv)
: webpackConfig(env, argv);

Check warning on line 420 in resolvers/webpack/index.js

View check run for this annotation

Codecov / codecov/patch

resolvers/webpack/index.js#L418-L420

Added lines #L418 - L420 were not covered by tests
}

if (isArray(webpackConfig)) {
Expand Down
48 changes: 48 additions & 0 deletions resolvers/webpack/test/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const chai = require('chai');
const expect = chai.expect;
const path = require('path');

const resolve = require('../index').resolve;

const file = path.join(__dirname, 'files', 'src', 'jsx', 'dummy.js');

describe('cache', function () {
it('can distinguish different config files', function () {
const setting1 = {
config: require(path.join(__dirname, './files/webpack.function.config.js')),
argv: {
mode: 'test',
},
cache: true,
};
expect(resolve('baz', file, setting1)).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'some', 'bar', 'bar.js'));
const setting2 = {
config: require(path.join(__dirname, './files/webpack.function.config.multiple.js')),
cache: true,
};
expect(resolve('baz', file, setting2)).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'foo.js'));
});

it('can distinguish different config', function () {
const setting1 = {
config: require(path.join(__dirname, './files/webpack.function.config.js')),
env: {
dummy: true,
},
cache: true,
};
expect(resolve('bar', file, setting1)).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'bar.js'));
const setting2 = {
config: require(path.join(__dirname, './files/webpack.function.config.multiple.js')),
cache: true,
};
const result = resolve('bar', file, setting2);
expect(result).not.to.have.property('path');
expect(result).to.have.property('found').to.be.false;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = [function(env) {
alias: {
'foo': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'),
'bar': env ? path.join(__dirname, 'some', 'goofy', 'path', 'bar.js') : undefined,
'baz': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'),
'some-alias': path.join(__dirname, 'some'),
},
modules: [
Expand Down

0 comments on commit ac7d396

Please sign in to comment.