Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Commit

Permalink
Add option for custom compare function (#79)
Browse files Browse the repository at this point in the history
* chore(na): change default cache directory

* chore(na): remove error changes

* feat: add option for custom compare function

It's not always reliable to use `mtime`, so allowing for a `compare` function to be passed makes it possible for custom logic to be used. (ie. hashing)

* test(na): add a simple test for the compare function

* chore(na): updated info on read me for compare fn

* test(na): changed return value on compare test function

* test(na): second compiler call to create cache  in the first place
  • Loading branch information
mistic committed May 30, 2019
1 parent 687a6a4 commit 47ba95b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -54,6 +54,7 @@ module.exports = {
| **`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) |
| **`write`** | `{Function(cacheKey, data, callback) -> {void}}` | `undefined` | Allows you to override default write cache data to file (e.g. Redis, memcached) |
| **`read`** | `{Function(cacheKey, callback) -> {void}}` | `undefined` | Allows you to override default read cache data from file |
| **`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. |
| **`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) |

## Examples
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Expand Up @@ -27,6 +27,7 @@ const defaults = {
read,
readOnly: false,
write,
compare,
};

function pathWithCacheContext(cacheContext, originalPath) {
Expand Down Expand Up @@ -138,6 +139,7 @@ function pitch(remainingRequest, prevRequest, dataInput) {
readOnly,
cacheContext,
cacheKey: cacheKeyFn,
compare: compareFn,
} = options;

const callback = this.async();
Expand Down Expand Up @@ -173,7 +175,9 @@ function pitch(remainingRequest, prevRequest, dataInput) {
return;
}

if (stats.mtime.getTime() !== dep.mtime) {
// If the compare function returns false
// we not read from cache
if (compareFn(stats, dep) !== true) {
eachCallback(true);
return;
}
Expand Down Expand Up @@ -253,5 +257,9 @@ function cacheKey(options, request) {
return path.join(cacheDirectory, `${hash}.json`);
}

function compare(stats, dep) {
return stats.mtime.getTime() === dep.mtime;
}

export const raw = true;
export { loader as default, pitch };
3 changes: 3 additions & 0 deletions src/options.json
Expand Up @@ -21,6 +21,9 @@
},
"write": {
"instanceof": "Function"
},
"compare": {
"instanceof": "Function"
}
},
"additionalProperties": false
Expand Down
22 changes: 22 additions & 0 deletions test/compare-option.test.js
@@ -0,0 +1,22 @@
const { webpack } = require('./helpers');

const mockCacheLoaderCompareFn = jest.fn();
const mockWebpackConfig = {
loader: {
options: {
compare: () => {
mockCacheLoaderCompareFn();
return true;
},
},
},
};

describe('compare option', () => {
it('should call compare function', async () => {
const testId = './basic/index.js';
await webpack(testId, mockWebpackConfig);
await webpack(testId, mockWebpackConfig);
expect(mockCacheLoaderCompareFn).toHaveBeenCalled();
});
});

0 comments on commit 47ba95b

Please sign in to comment.