diff --git a/README.md b/README.md index 18aeab6..8e67e1c 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ module.exports = { | **`cacheKey`** | `{Function(options, request) -> {String}}` | `undefined` | Allows you to override default cache key generator | | **`cacheDirectory`** | `{String}` | `findCacheDir({ name: 'cache-loader' }) or os.tmpdir()` | Provide a cache directory where cache items should be stored (used for default read/write implementation) | | **`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) | -| **`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 | +| **`compare`** | `{Function(stats, dep, cacheData) -> {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 | | **`precision`** | `{Number}` | `0` | Round `mtime` by this number of milliseconds both for `stats` and `dep` before passing those params to the comparing function | | **`read`** | `{Function(cacheKey, callback) -> {void}}` | `undefined` | Allows you to override default read cache data from file | | **`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) | diff --git a/src/index.js b/src/index.js index 0b41a97..fb9ef59 100644 --- a/src/index.js +++ b/src/index.js @@ -57,7 +57,7 @@ function roundMs(mtime, precision) { // right before writing. Every other internal steps with the paths // should be accomplish over absolute paths. Otherwise we have the risk // to break watchpack -> chokidar watch logic over webpack@4 --watch -function loader(...args) { +function loader(source, ...args) { const options = Object.assign({}, defaults, getOptions(this)); validateOptions(schema, options, { @@ -70,7 +70,7 @@ function loader(...args) { // In case we are under a readOnly mode on cache-loader // we don't want to write or update any cache file if (readOnly) { - this.callback(null, ...args); + this.callback(null, source, ...args); return; } @@ -117,11 +117,11 @@ function loader(...args) { ], (err, taskResults) => { if (err) { - callback(null, ...args); + callback(null, source, ...args); return; } if (!cache) { - callback(null, ...args); + callback(null, source, ...args); return; } const [deps, contextDeps] = taskResults; @@ -132,13 +132,14 @@ function loader(...args) { options.cacheContext, data.remainingRequest ), + source, dependencies: deps, contextDependencies: contextDeps, - result: args, + result: [source, ...args], }, () => { // ignore errors here - callback(null, ...args); + callback(null, source, ...args); } ); } @@ -228,7 +229,7 @@ function pitch(remainingRequest, prevRequest, dataInput) { // If the compare function returns false // we not read from cache - if (compareFn(compStats, compDep) !== true) { + if (compareFn(compStats, compDep, cacheData) !== true) { eachCallback(true); return; } diff --git a/test/__snapshots__/cacheContext-option.test.js.snap b/test/__snapshots__/cacheContext-option.test.js.snap index dec9806..2ad080d 100644 --- a/test/__snapshots__/cacheContext-option.test.js.snap +++ b/test/__snapshots__/cacheContext-option.test.js.snap @@ -10,6 +10,10 @@ exports[`cacheContext option should generate relative paths to the project root: "[ { \\"remainingRequest\\": \\"test/fixtures/basic/file_1.js\\", + \\"source\\": { + \\"type\\": \\"Buffer\\", + \\"data\\": \\"base64:LyogZXNsaW50LWRpc2FibGUgKi8KY29uc29sZS5sb2coJ2ZpbGVfMScpOwo=\\" + }, \\"dependencies\\": [ { \\"path\\": \\"test/fixtures/basic/file_1.js\\", @@ -30,6 +34,10 @@ exports[`cacheContext option should generate relative paths to the project root: }, { \\"remainingRequest\\": \\"test/fixtures/basic/file_2.js\\", + \\"source\\": { + \\"type\\": \\"Buffer\\", + \\"data\\": \\"base64:LyogZXNsaW50LWRpc2FibGUgKi8KY29uc29sZS5sb2coJ2ZpbGVfMicpOwo=\\" + }, \\"dependencies\\": [ { \\"path\\": \\"test/fixtures/basic/file_2.js\\", @@ -50,6 +58,10 @@ exports[`cacheContext option should generate relative paths to the project root: }, { \\"remainingRequest\\": \\"test/fixtures/basic/index.js\\", + \\"source\\": { + \\"type\\": \\"Buffer\\", + \\"data\\": \\"base64:LyogZXNsaW50LWRpc2FibGUgKi8KcmVxdWlyZSgnLi9maWxlXzEuanMnKTsKcmVxdWlyZSgnLi9maWxlXzIuanMnKTsKCmNvbnNvbGUubG9nKCdiYXNpY19lbnRyeScpOwo=\\" + }, \\"dependencies\\": [ { \\"path\\": \\"test/fixtures/basic/index.js\\", diff --git a/test/compare-option.test.js b/test/compare-option.test.js index ac9fd71..582aeac 100644 --- a/test/compare-option.test.js +++ b/test/compare-option.test.js @@ -12,8 +12,8 @@ const mockWebpackConfig = { loader: { options: { cacheDirectory: mockRandomTmpDir, - compare: (stats, dep) => { - mockCacheLoaderCompareFn(stats, dep); + compare: (stats, dep, cacheData) => { + mockCacheLoaderCompareFn(stats, dep, cacheData); return true; }, }, @@ -26,8 +26,8 @@ const mockRelativeWebpackConfig = { options: { cacheContext: path.resolve('./'), cacheDirectory: mockRandomTmpDir, - compare: (stats, dep) => { - mockCacheLoaderCompareOnRelativeFn(stats, dep); + compare: (stats, dep, cacheData) => { + mockCacheLoaderCompareOnRelativeFn(stats, dep, cacheData); return true; }, }, @@ -50,12 +50,12 @@ describe('compare option', () => { expect(mockCacheLoaderCompareFn).toHaveBeenCalled(); }); - it('should call compare function with 2 args', async () => { + it('should call compare function with 3 args', async () => { const testId = './basic/index.js'; await webpack(testId, mockWebpackConfig); await webpack(testId, mockWebpackConfig); expect(mockCacheLoaderCompareFn).toHaveBeenCalled(); - expect(mockCacheLoaderCompareFn.mock.calls[0].length).toBe(2); + expect(mockCacheLoaderCompareFn.mock.calls[0].length).toBe(3); }); it('should call compare function with correct args', async () => { @@ -68,11 +68,19 @@ describe('compare option', () => { const stats = mockCacheLoaderCompareFn.mock.calls[0][0]; // eslint-disable-next-line const dep = mockCacheLoaderCompareFn.mock.calls[0][1]; + // eslint-disable-next-line + const cacheData = mockCacheLoaderCompareFn.mock.calls[0][2]; expect(stats).toBeDefined(); expect(stats instanceof fs.Stats); expect(dep).toBeDefined(); expect(dep.mtime).toBeDefined(); expect(dep.path).toBeDefined(); + expect(cacheData).toBeDefined(); + expect(cacheData.remainingRequest).toBeDefined(); + expect(cacheData.source).toBeDefined(); + expect(cacheData.dependencies).toBeDefined(); + expect(cacheData.contextDependencies).toBeDefined(); + expect(cacheData.result).toBeDefined(); }); it('should call compare with contextualized dep', async () => { diff --git a/test/helpers.js b/test/helpers.js index df8932e..74a52ae 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -58,7 +58,7 @@ function compile(fixture, config = {}, options = {}) { // eslint-disable-next-line no-param-reassign config = { mode: 'development', - devtool: config.devtool || 'sourcemap', + devtool: config.devtool || 'source-map', context: path.resolve(__dirname, 'fixtures'), entry: path.resolve(__dirname, 'fixtures', fixture), output: outputConfig(config),