Skip to content

Commit ac7d396

Browse files
seiyabljharb
authored andcommitted
[resolvers/webpack] [new] add cache option
1 parent 67cc798 commit ac7d396

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

resolvers/webpack/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
44
This change log adheres to standards from [Keep a CHANGELOG](https://keepachangelog.com).
55

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

89
## 0.13.9 - 2024-09-02
910
- [refactor] simplify loop ([#3029], thanks [@fregante])
@@ -182,6 +183,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
182183
### Added
183184
- `interpret` configs (such as `.babel.js`). Thanks to [@gausie] for the initial PR ([#164], ages ago! 😅) and [@jquense] for tests ([#278]).
184185

186+
[#3100]: https://github.com/import-js/eslint-plugin-import/pull/3100
185187
[#3029]: https://github.com/import-js/eslint-plugin-import/pull/3029
186188
[#2287]: https://github.com/import-js/eslint-plugin-import/pull/2287
187189
[#2023]: https://github.com/import-js/eslint-plugin-import/pull/2023
@@ -247,6 +249,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
247249
[@Rogeres]: https://github.com/Rogeres
248250
[@Satyam]: https://github.com/Satyam
249251
[@Schweinepriester]: https://github.com/Schweinepriester
252+
[@seiyab]: https://github.com/seiyab
250253
[@SkeLLLa]: https://github.com/SkeLLLa
251254
[@taion]: https://github.com/taion
252255
[@toshafed]: https://github.com/toshafed

resolvers/webpack/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ settings:
9494
production: true
9595
```
9696

97+
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:
98+
99+
```yaml
100+
---
101+
settings:
102+
import/resolver:
103+
webpack:
104+
config: 'webpack.config.js'
105+
cache: true
106+
```
107+
97108
## Support
98109

99110
[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)

resolvers/webpack/index.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,22 @@ function getResolveSync(configPath, webpackConfig, cwd) {
311311
return cached.value;
312312
}
313313

314+
const _evalCache = new Map();
315+
function evaluateFunctionConfigCached(configPath, webpackConfig, env, argv) {
316+
const cacheKey = JSON.stringify({ configPath, args: [env, argv] });
317+
if (_evalCache.has(cacheKey)) {
318+
return _evalCache.get(cacheKey);
319+
}
320+
const cached = webpackConfig(env, argv);
321+
_evalCache.set(cacheKey, cached);
322+
323+
while (_evalCache.size > MAX_CACHE) {
324+
// remove oldest item
325+
_evalCache.delete(_evalCache.keys().next().value);
326+
}
327+
return cached;
328+
}
329+
314330
/**
315331
* Find the full path to 'source', given 'file' as a full reference path.
316332
*
@@ -354,6 +370,7 @@ exports.resolve = function (source, file, settings) {
354370
const configIndex = settings && settings['config-index'];
355371
const env = settings && settings.env;
356372
const argv = settings && typeof settings.argv !== 'undefined' ? settings.argv : {};
373+
const shouldCacheFunctionConfig = settings && settings.cache;
357374
let packageDir;
358375

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

400417
if (typeof webpackConfig === 'function') {
401-
webpackConfig = webpackConfig(env, argv);
418+
webpackConfig = shouldCacheFunctionConfig
419+
? evaluateFunctionConfigCached(configPath, webpackConfig, env, argv)
420+
: webpackConfig(env, argv);
402421
}
403422

404423
if (isArray(webpackConfig)) {

resolvers/webpack/test/cache.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
const chai = require('chai');
4+
const expect = chai.expect;
5+
const path = require('path');
6+
7+
const resolve = require('../index').resolve;
8+
9+
const file = path.join(__dirname, 'files', 'src', 'jsx', 'dummy.js');
10+
11+
describe('cache', function () {
12+
it('can distinguish different config files', function () {
13+
const setting1 = {
14+
config: require(path.join(__dirname, './files/webpack.function.config.js')),
15+
argv: {
16+
mode: 'test',
17+
},
18+
cache: true,
19+
};
20+
expect(resolve('baz', file, setting1)).to.have.property('path')
21+
.and.equal(path.join(__dirname, 'files', 'some', 'bar', 'bar.js'));
22+
const setting2 = {
23+
config: require(path.join(__dirname, './files/webpack.function.config.multiple.js')),
24+
cache: true,
25+
};
26+
expect(resolve('baz', file, setting2)).to.have.property('path')
27+
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'foo.js'));
28+
});
29+
30+
it('can distinguish different config', function () {
31+
const setting1 = {
32+
config: require(path.join(__dirname, './files/webpack.function.config.js')),
33+
env: {
34+
dummy: true,
35+
},
36+
cache: true,
37+
};
38+
expect(resolve('bar', file, setting1)).to.have.property('path')
39+
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'bar.js'));
40+
const setting2 = {
41+
config: require(path.join(__dirname, './files/webpack.function.config.multiple.js')),
42+
cache: true,
43+
};
44+
const result = resolve('bar', file, setting2);
45+
expect(result).not.to.have.property('path');
46+
expect(result).to.have.property('found').to.be.false;
47+
});
48+
});

resolvers/webpack/test/files/webpack.function.config.multiple.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = [function(env) {
77
alias: {
88
'foo': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'),
99
'bar': env ? path.join(__dirname, 'some', 'goofy', 'path', 'bar.js') : undefined,
10+
'baz': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'),
1011
'some-alias': path.join(__dirname, 'some'),
1112
},
1213
modules: [

0 commit comments

Comments
 (0)