Skip to content

Commit 2e766b0

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents db32dcc + 47ba95b commit 2e766b0

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module.exports = {
5454
| **`cacheIdentifier`** | `{String}` | `cache-loader:{version} {process.env.NODE_ENV}` | Provide an invalidation identifier which is used to generate the hashes. You can use it for extra dependencies of loaders (used for default read/write implementation) |
5555
| **`write`** | `{Function(cacheKey, data, callback) -> {void}}` | `undefined` | Allows you to override default write cache data to file (e.g. Redis, memcached) |
5656
| **`read`** | `{Function(cacheKey, callback) -> {void}}` | `undefined` | Allows you to override default read cache data from file |
57+
| **`compare`** | `{Function(stats, dep) -> {Boolean}}` | `undefined` | Allows you to override default comparison function between the cached dependency and the one is being read. Return `true` to use the cached resource. |
5758
| **`readOnly`** | `{Boolean}` | `false` | Allows you to override default value and make the cache read only (useful for some environments where you don't want the cache to be updated, only read from it) |
5859

5960
## Examples

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const defaults = {
2727
read,
2828
readOnly: false,
2929
write,
30+
compare,
3031
};
3132

3233
function pathWithCacheContext(cacheContext, originalPath) {
@@ -138,6 +139,7 @@ function pitch(remainingRequest, prevRequest, dataInput) {
138139
readOnly,
139140
cacheContext,
140141
cacheKey: cacheKeyFn,
142+
compare: compareFn,
141143
} = options;
142144

143145
const callback = this.async();
@@ -173,7 +175,9 @@ function pitch(remainingRequest, prevRequest, dataInput) {
173175
return;
174176
}
175177

176-
if (stats.mtime.getTime() !== dep.mtime) {
178+
// If the compare function returns false
179+
// we not read from cache
180+
if (compareFn(stats, dep) !== true) {
177181
eachCallback(true);
178182
return;
179183
}
@@ -253,5 +257,9 @@ function cacheKey(options, request) {
253257
return path.join(cacheDirectory, `${hash}.json`);
254258
}
255259

260+
function compare(stats, dep) {
261+
return stats.mtime.getTime() === dep.mtime;
262+
}
263+
256264
export const raw = true;
257265
export { loader as default, pitch };

src/options.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
},
2222
"write": {
2323
"instanceof": "Function"
24+
},
25+
"compare": {
26+
"instanceof": "Function"
2427
}
2528
},
2629
"additionalProperties": false

test/compare-option.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { webpack } = require('./helpers');
2+
3+
const mockCacheLoaderCompareFn = jest.fn();
4+
const mockWebpackConfig = {
5+
loader: {
6+
options: {
7+
compare: () => {
8+
mockCacheLoaderCompareFn();
9+
return true;
10+
},
11+
},
12+
},
13+
};
14+
15+
describe('compare option', () => {
16+
it('should call compare function', async () => {
17+
const testId = './basic/index.js';
18+
await webpack(testId, mockWebpackConfig);
19+
await webpack(testId, mockWebpackConfig);
20+
expect(mockCacheLoaderCompareFn).toHaveBeenCalled();
21+
});
22+
});

0 commit comments

Comments
 (0)