diff --git a/README.md b/README.md index 0843278..6c84aec 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.js b/src/index.js index 69b41a1..54f4957 100644 --- a/src/index.js +++ b/src/index.js @@ -27,6 +27,7 @@ const defaults = { read, readOnly: false, write, + compare, }; function pathWithCacheContext(cacheContext, originalPath) { @@ -138,6 +139,7 @@ function pitch(remainingRequest, prevRequest, dataInput) { readOnly, cacheContext, cacheKey: cacheKeyFn, + compare: compareFn, } = options; const callback = this.async(); @@ -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; } @@ -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 }; diff --git a/src/options.json b/src/options.json index c9b56fc..be497ae 100644 --- a/src/options.json +++ b/src/options.json @@ -21,6 +21,9 @@ }, "write": { "instanceof": "Function" + }, + "compare": { + "instanceof": "Function" } }, "additionalProperties": false diff --git a/test/compare-option.test.js b/test/compare-option.test.js new file mode 100644 index 0000000..1481324 --- /dev/null +++ b/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(); + }); +});