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

Commit d454d28

Browse files
committed
feat(compare-func): pass cacheData to the compare() function
Also added caching the source file.
1 parent 4e0e3ae commit d454d28

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = {
5252
| **`cacheKey`** | `{Function(options, request) -> {String}}` | `undefined` | Allows you to override default cache key generator |
5353
| **`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) |
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) |
55-
| **`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 |
55+
| **`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 |
5656
| **`precision`** | `{Number}` | `0` | Round `mtime` by this number of milliseconds both for `stats` and `dep` before passing those params to the comparing function |
5757
| **`read`** | `{Function(cacheKey, callback) -> {void}}` | `undefined` | Allows you to override default read cache data from file |
5858
| **`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) |

src/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function roundMs(mtime, precision) {
5757
// right before writing. Every other internal steps with the paths
5858
// should be accomplish over absolute paths. Otherwise we have the risk
5959
// to break watchpack -> chokidar watch logic over webpack@4 --watch
60-
function loader(...args) {
60+
function loader(source, ...args) {
6161
const options = Object.assign({}, defaults, getOptions(this));
6262

6363
validateOptions(schema, options, {
@@ -70,7 +70,7 @@ function loader(...args) {
7070
// In case we are under a readOnly mode on cache-loader
7171
// we don't want to write or update any cache file
7272
if (readOnly) {
73-
this.callback(null, ...args);
73+
this.callback(null, source, ...args);
7474
return;
7575
}
7676

@@ -117,11 +117,11 @@ function loader(...args) {
117117
],
118118
(err, taskResults) => {
119119
if (err) {
120-
callback(null, ...args);
120+
callback(null, source, ...args);
121121
return;
122122
}
123123
if (!cache) {
124-
callback(null, ...args);
124+
callback(null, source, ...args);
125125
return;
126126
}
127127
const [deps, contextDeps] = taskResults;
@@ -132,13 +132,14 @@ function loader(...args) {
132132
options.cacheContext,
133133
data.remainingRequest
134134
),
135+
source,
135136
dependencies: deps,
136137
contextDependencies: contextDeps,
137-
result: args,
138+
result: [source, ...args],
138139
},
139140
() => {
140141
// ignore errors here
141-
callback(null, ...args);
142+
callback(null, source, ...args);
142143
}
143144
);
144145
}
@@ -228,7 +229,7 @@ function pitch(remainingRequest, prevRequest, dataInput) {
228229

229230
// If the compare function returns false
230231
// we not read from cache
231-
if (compareFn(compStats, compDep) !== true) {
232+
if (compareFn(compStats, compDep, cacheData) !== true) {
232233
eachCallback(true);
233234
return;
234235
}

test/__snapshots__/cacheContext-option.test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ exports[`cacheContext option should generate relative paths to the project root:
1010
"[
1111
{
1212
\\"remainingRequest\\": \\"test/fixtures/basic/file_1.js\\",
13+
\\"source\\": {
14+
\\"type\\": \\"Buffer\\",
15+
\\"data\\": \\"base64:LyogZXNsaW50LWRpc2FibGUgKi8KY29uc29sZS5sb2coJ2ZpbGVfMScpOwo=\\"
16+
},
1317
\\"dependencies\\": [
1418
{
1519
\\"path\\": \\"test/fixtures/basic/file_1.js\\",
@@ -30,6 +34,10 @@ exports[`cacheContext option should generate relative paths to the project root:
3034
},
3135
{
3236
\\"remainingRequest\\": \\"test/fixtures/basic/file_2.js\\",
37+
\\"source\\": {
38+
\\"type\\": \\"Buffer\\",
39+
\\"data\\": \\"base64:LyogZXNsaW50LWRpc2FibGUgKi8KY29uc29sZS5sb2coJ2ZpbGVfMicpOwo=\\"
40+
},
3341
\\"dependencies\\": [
3442
{
3543
\\"path\\": \\"test/fixtures/basic/file_2.js\\",
@@ -50,6 +58,10 @@ exports[`cacheContext option should generate relative paths to the project root:
5058
},
5159
{
5260
\\"remainingRequest\\": \\"test/fixtures/basic/index.js\\",
61+
\\"source\\": {
62+
\\"type\\": \\"Buffer\\",
63+
\\"data\\": \\"base64:LyogZXNsaW50LWRpc2FibGUgKi8KcmVxdWlyZSgnLi9maWxlXzEuanMnKTsKcmVxdWlyZSgnLi9maWxlXzIuanMnKTsKCmNvbnNvbGUubG9nKCdiYXNpY19lbnRyeScpOwo=\\"
64+
},
5365
\\"dependencies\\": [
5466
{
5567
\\"path\\": \\"test/fixtures/basic/index.js\\",

test/compare-option.test.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const mockWebpackConfig = {
1212
loader: {
1313
options: {
1414
cacheDirectory: mockRandomTmpDir,
15-
compare: (stats, dep) => {
16-
mockCacheLoaderCompareFn(stats, dep);
15+
compare: (stats, dep, cacheData) => {
16+
mockCacheLoaderCompareFn(stats, dep, cacheData);
1717
return true;
1818
},
1919
},
@@ -26,8 +26,8 @@ const mockRelativeWebpackConfig = {
2626
options: {
2727
cacheContext: path.resolve('./'),
2828
cacheDirectory: mockRandomTmpDir,
29-
compare: (stats, dep) => {
30-
mockCacheLoaderCompareOnRelativeFn(stats, dep);
29+
compare: (stats, dep, cacheData) => {
30+
mockCacheLoaderCompareOnRelativeFn(stats, dep, cacheData);
3131
return true;
3232
},
3333
},
@@ -50,12 +50,12 @@ describe('compare option', () => {
5050
expect(mockCacheLoaderCompareFn).toHaveBeenCalled();
5151
});
5252

53-
it('should call compare function with 2 args', async () => {
53+
it('should call compare function with 3 args', async () => {
5454
const testId = './basic/index.js';
5555
await webpack(testId, mockWebpackConfig);
5656
await webpack(testId, mockWebpackConfig);
5757
expect(mockCacheLoaderCompareFn).toHaveBeenCalled();
58-
expect(mockCacheLoaderCompareFn.mock.calls[0].length).toBe(2);
58+
expect(mockCacheLoaderCompareFn.mock.calls[0].length).toBe(3);
5959
});
6060

6161
it('should call compare function with correct args', async () => {
@@ -68,11 +68,19 @@ describe('compare option', () => {
6868
const stats = mockCacheLoaderCompareFn.mock.calls[0][0];
6969
// eslint-disable-next-line
7070
const dep = mockCacheLoaderCompareFn.mock.calls[0][1];
71+
// eslint-disable-next-line
72+
const cacheData = mockCacheLoaderCompareFn.mock.calls[0][2];
7173
expect(stats).toBeDefined();
7274
expect(stats instanceof fs.Stats);
7375
expect(dep).toBeDefined();
7476
expect(dep.mtime).toBeDefined();
7577
expect(dep.path).toBeDefined();
78+
expect(cacheData).toBeDefined();
79+
expect(cacheData.remainingRequest).toBeDefined();
80+
expect(cacheData.source).toBeDefined();
81+
expect(cacheData.dependencies).toBeDefined();
82+
expect(cacheData.contextDependencies).toBeDefined();
83+
expect(cacheData.result).toBeDefined();
7684
});
7785

7886
it('should call compare with contextualized dep', async () => {

test/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function compile(fixture, config = {}, options = {}) {
5858
// eslint-disable-next-line no-param-reassign
5959
config = {
6060
mode: 'development',
61-
devtool: config.devtool || 'sourcemap',
61+
devtool: config.devtool || 'source-map',
6262
context: path.resolve(__dirname, 'fixtures'),
6363
entry: path.resolve(__dirname, 'fixtures', fixture),
6464
output: outputConfig(config),

0 commit comments

Comments
 (0)