Skip to content

Commit 025b778

Browse files
fix: cache invalidation
1 parent 6cf83aa commit 025b778

15 files changed

+3155
-3232
lines changed

package-lock.json

Lines changed: 2922 additions & 3013 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,36 @@
4141
"webpack": "^4.0.0 || ^5.0.0"
4242
},
4343
"dependencies": {
44-
"cacache": "^15.0.3",
44+
"cacache": "^15.0.5",
4545
"find-cache-dir": "^3.3.1",
46-
"schema-utils": "^2.6.6",
47-
"serialize-javascript": "^3.0.0",
46+
"schema-utils": "^2.7.0",
47+
"serialize-javascript": "^4.0.0",
4848
"webpack-sources": "^1.4.3"
4949
},
5050
"devDependencies": {
51-
"@babel/cli": "^7.8.4",
52-
"@babel/core": "^7.9.6",
53-
"@babel/preset-env": "^7.9.6",
54-
"@commitlint/cli": "^8.3.5",
55-
"@commitlint/config-conventional": "^8.3.4",
51+
"@babel/cli": "^7.10.5",
52+
"@babel/core": "^7.10.5",
53+
"@babel/preset-env": "^7.10.4",
54+
"@commitlint/cli": "^9.1.2",
55+
"@commitlint/config-conventional": "^9.1.1",
5656
"@gfx/zopfli": "^1.0.14",
5757
"@webpack-contrib/defaults": "^6.3.0",
5858
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
59-
"babel-jest": "^26.0.1",
59+
"babel-jest": "^26.1.0",
6060
"cross-env": "^7.0.2",
6161
"del": "^5.1.0",
62-
"del-cli": "^3.0.0",
63-
"eslint": "^7.0.0",
62+
"del-cli": "^3.0.1",
63+
"eslint": "^7.4.0",
6464
"eslint-config-prettier": "^6.11.0",
65-
"eslint-plugin-import": "^2.20.2",
65+
"eslint-plugin-import": "^2.22.0",
6666
"file-loader": "^6.0.0",
6767
"husky": "^4.2.5",
68-
"jest": "^26.0.1",
69-
"lint-staged": "^10.2.2",
70-
"memfs": "^3.1.2",
68+
"jest": "^26.1.0",
69+
"lint-staged": "^10.2.11",
70+
"memfs": "^3.2.0",
7171
"npm-run-all": "^4.1.5",
7272
"prettier": "^2.0.5",
73-
"standard-version": "^8.0.0",
73+
"standard-version": "^8.0.2",
7474
"webpack": "^4.43.0"
7575
},
7676
"keywords": [

src/index.js

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,27 @@ class CompressionPlugin {
4848
deleteOriginalAssets,
4949
};
5050

51-
if (typeof algorithm === 'string') {
51+
this.algorithm = this.options.algorithm;
52+
this.compressionOptions = this.options.compressionOptions;
53+
54+
if (typeof this.algorithm === 'string') {
5255
// eslint-disable-next-line global-require
5356
const zlib = require('zlib');
5457

55-
this.options.algorithm = zlib[this.options.algorithm];
58+
this.algorithm = zlib[this.algorithm];
5659

57-
if (!this.options.algorithm) {
58-
throw new Error('Algorithm not found in zlib');
60+
if (!this.algorithm) {
61+
throw new Error(
62+
`Algorithm "${this.options.algorithm}" is not found in zlib`
63+
);
5964
}
6065

6166
const defaultCompressionOptions = { level: 9 };
6267

63-
this.options.compressionOptions = {
68+
// TODO change this behaviour in the next major release
69+
this.compressionOptions = {
6470
...defaultCompressionOptions,
65-
...this.options.compressionOptions,
71+
...this.compressionOptions,
6672
};
6773
}
6874

@@ -158,7 +164,7 @@ class CompressionPlugin {
158164

159165
compress(input) {
160166
return new Promise((resolve, reject) => {
161-
const { algorithm, compressionOptions } = this.options;
167+
const { algorithm, compressionOptions } = this;
162168

163169
algorithm(input, compressionOptions, (error, result) => {
164170
if (error) {
@@ -170,7 +176,7 @@ class CompressionPlugin {
170176
});
171177
}
172178

173-
async runTasks(assetNames) {
179+
async runTasks(assetNames, getTaskForAsset, cache) {
174180
const scheduledTasks = [];
175181

176182
for (const assetName of assetNames) {
@@ -185,8 +191,8 @@ class CompressionPlugin {
185191
taskResult = { error };
186192
}
187193

188-
if (this.cache.isEnabled() && !taskResult.error) {
189-
taskResult = await this.cache.store(task, taskResult).then(
194+
if (cache.isEnabled() && !taskResult.error) {
195+
taskResult = await cache.store(task, taskResult).then(
190196
() => taskResult,
191197
() => taskResult
192198
);
@@ -199,14 +205,14 @@ class CompressionPlugin {
199205

200206
scheduledTasks.push(
201207
new Promise((resolve) => {
202-
const task = this.getTaskForAsset(assetName).next().value;
208+
const task = getTaskForAsset(assetName).next().value;
203209

204210
if (!task) {
205211
return resolve();
206212
}
207213

208-
if (this.cache.isEnabled()) {
209-
return this.cache.get(task).then(
214+
if (cache.isEnabled()) {
215+
return cache.get(task).then(
210216
(taskResult) => {
211217
task.callback(taskResult);
212218

@@ -229,34 +235,42 @@ class CompressionPlugin {
229235
}
230236

231237
apply(compiler) {
238+
const matchObject = ModuleFilenameHelpers.matchObject.bind(
239+
// eslint-disable-next-line no-undefined
240+
undefined,
241+
this.options
242+
);
243+
232244
compiler.hooks.emit.tapPromise(
233245
{ name: 'CompressionPlugin' },
234246
async (compilation) => {
235-
const assetNames = Object.keys(compilation.assets).filter((assetName) =>
236-
ModuleFilenameHelpers.matchObject(this.options, assetName)
247+
const { assets } = compilation;
248+
249+
const assetNames = Object.keys(assets).filter((assetName) =>
250+
matchObject(assetName)
237251
);
238252

239253
if (assetNames.length === 0) {
240254
return Promise.resolve();
241255
}
242256

257+
const getTaskForAsset = this.taskGenerator.bind(
258+
this,
259+
compiler,
260+
compilation
261+
);
243262
const CacheEngine = CompressionPlugin.isWebpack4()
244263
? // eslint-disable-next-line global-require
245264
require('./Webpack4Cache').default
246265
: // eslint-disable-next-line global-require
247266
require('./Webpack5Cache').default;
248-
249-
this.cache = new CacheEngine(compilation, {
267+
const cache = new CacheEngine(compilation, {
250268
cache: this.options.cache,
251269
});
252270

253-
this.getTaskForAsset = this.taskGenerator.bind(
254-
this,
255-
compiler,
256-
compilation
257-
);
271+
await this.runTasks(assetNames, getTaskForAsset, cache);
258272

259-
return this.runTasks(assetNames);
273+
return Promise.resolve();
260274
}
261275
);
262276
}

test/__snapshots__/CompressionPlugin.test.js.snap.webpack5

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ Array [
1616
],
1717
Array [
1818
"async.async.735f6a02a2e820243759.js",
19-
350,
19+
269,
2020
],
2121
Array [
2222
"async.async.735f6a02a2e820243759.js.gz",
23-
205,
23+
181,
2424
],
2525
Array [
26-
"main.f210d52065e5e6030f3a.js",
27-
13052,
26+
"main.29849e221d2dd6cc8f8e.js",
27+
14182,
2828
],
2929
Array [
30-
"main.f210d52065e5e6030f3a.js.gz",
31-
3195,
30+
"main.29849e221d2dd6cc8f8e.js.gz",
31+
3477,
3232
],
3333
]
3434
`;
@@ -45,11 +45,11 @@ Array [
4545
],
4646
Array [
4747
"async.async.735f6a02a2e820243759.js",
48-
350,
48+
269,
4949
],
5050
Array [
51-
"main.f210d52065e5e6030f3a.js",
52-
13052,
51+
"main.29849e221d2dd6cc8f8e.js",
52+
14182,
5353
],
5454
]
5555
`;
@@ -77,20 +77,20 @@ Array [
7777
393,
7878
],
7979
Array [
80-
"async.async.js.gz?ver=5f4efddfc3b0d0f33fc4",
81-
205,
80+
"async.async.js.gz?ver=e67b836a71a5cf62a680",
81+
181,
8282
],
8383
Array [
84-
"async.async.js?ver=5f4efddfc3b0d0f33fc4",
85-
350,
84+
"async.async.js?ver=e67b836a71a5cf62a680",
85+
269,
8686
],
8787
Array [
88-
"main.js.gz?var=5f4efddfc3b0d0f33fc4",
89-
3222,
88+
"main.js.gz?var=e67b836a71a5cf62a680",
89+
3508,
9090
],
9191
Array [
92-
"main.js?var=5f4efddfc3b0d0f33fc4",
93-
13211,
92+
"main.js?var=e67b836a71a5cf62a680",
93+
14341,
9494
],
9595
]
9696
`;

test/__snapshots__/algorithm.test.js.snap.webpack4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports[`"algorithm" option matches snapshot for \`gzip\` value ({String}): erro
4141

4242
exports[`"algorithm" option matches snapshot for \`gzip\` value ({String}): warnings 1`] = `Array []`;
4343

44-
exports[`"algorithm" option matches snapshot for \`unknown\` value ({String}) 1`] = `"Algorithm not found in zlib"`;
44+
exports[`"algorithm" option matches snapshot for \`unknown\` value ({String}) 1`] = `"Algorithm \\"unknown\\" is not found in zlib"`;
4545

4646
exports[`"algorithm" option matches snapshot for custom function ({Function}): assets 1`] = `
4747
Array [

test/__snapshots__/algorithm.test.js.snap.webpack5

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ Array [
2020
],
2121
Array [
2222
"async.async.735f6a02a2e820243759.js",
23-
350,
23+
269,
2424
],
2525
Array [
2626
"async.async.735f6a02a2e820243759.js.gz",
27-
205,
27+
181,
2828
],
2929
Array [
30-
"main.f210d52065e5e6030f3a.js",
31-
13052,
30+
"main.29849e221d2dd6cc8f8e.js",
31+
14182,
3232
],
3333
Array [
34-
"main.f210d52065e5e6030f3a.js.gz",
35-
3195,
34+
"main.29849e221d2dd6cc8f8e.js.gz",
35+
3477,
3636
],
3737
]
3838
`;
@@ -41,7 +41,7 @@ exports[`"algorithm" option matches snapshot for \`gzip\` value ({String}): erro
4141

4242
exports[`"algorithm" option matches snapshot for \`gzip\` value ({String}): warnings 1`] = `Array []`;
4343

44-
exports[`"algorithm" option matches snapshot for \`unknown\` value ({String}) 1`] = `"Algorithm not found in zlib"`;
44+
exports[`"algorithm" option matches snapshot for \`unknown\` value ({String}) 1`] = `"Algorithm \\"unknown\\" is not found in zlib"`;
4545

4646
exports[`"algorithm" option matches snapshot for custom function ({Function}): assets 1`] = `
4747
Array [
@@ -63,19 +63,19 @@ Array [
6363
],
6464
Array [
6565
"async.async.735f6a02a2e820243759.js",
66-
350,
66+
269,
6767
],
6868
Array [
6969
"async.async.735f6a02a2e820243759.js.gz",
70-
350,
70+
269,
7171
],
7272
Array [
73-
"main.f210d52065e5e6030f3a.js",
74-
13052,
73+
"main.29849e221d2dd6cc8f8e.js",
74+
14182,
7575
],
7676
Array [
77-
"main.f210d52065e5e6030f3a.js.gz",
78-
13052,
77+
"main.29849e221d2dd6cc8f8e.js.gz",
78+
14182,
7979
],
8080
]
8181
`;
@@ -104,11 +104,11 @@ Array [
104104
],
105105
Array [
106106
"async.async.735f6a02a2e820243759.js",
107-
350,
107+
269,
108108
],
109109
Array [
110-
"main.f210d52065e5e6030f3a.js",
111-
13052,
110+
"main.29849e221d2dd6cc8f8e.js",
111+
14182,
112112
],
113113
]
114114
`;

0 commit comments

Comments
 (0)