Skip to content

Commit 1f9674e

Browse files
feat: improve compatibility with webpack@5
BREAKING CHANGE: use `processAssets` hook for webpack@5 compatibility, it can create incompatibility with plugins that do not support webpack@5, please open an issue in their repositories
1 parent 7de0317 commit 1f9674e

36 files changed

+1515
-548
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/coverage
22
/dist
33
/node_modules
4-
/test/fixtures
4+
/test/fixtures
5+
/test/outputs

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ npm-debug.log*
77
/local
88
/reports
99
/node_modules
10+
/test/outputs
1011
.DS_Store
1112
Thumbs.db
1213
.idea

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/dist
33
/node_modules
44
/test/fixtures
5-
CHANGELOG.md
5+
/test/outputs
6+
CHANGELOG.md

src/Webpack4Cache.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,27 @@ export default class Webpack4Cache {
2020
return Boolean(this.cacheDir);
2121
}
2222

23-
get(task) {
23+
async get(task) {
2424
// eslint-disable-next-line no-param-reassign
2525
task.cacheIdent = task.cacheIdent || serialize(task.cacheKeys);
2626

27-
return cacache.get(this.cacheDir, task.cacheIdent).then(({ data }) => {
28-
const result = JSON.parse(data);
27+
let cachedResult;
2928

30-
result.output = Buffer.from(result.output);
29+
try {
30+
cachedResult = await cacache.get(this.cacheDir, task.cacheIdent);
31+
} catch (ignoreError) {
32+
// eslint-disable-next-line no-undefined
33+
return undefined;
34+
}
3135

32-
return result;
33-
});
36+
return Buffer.from(JSON.parse(cachedResult.data).data);
3437
}
3538

36-
store(task, data) {
37-
return cacache.put(this.cacheDir, task.cacheIdent, JSON.stringify(data));
39+
async store(task) {
40+
return cacache.put(
41+
this.cacheDir,
42+
task.cacheIdent,
43+
JSON.stringify(task.output)
44+
);
3845
}
3946
}

src/Webpack5Cache.js

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,22 @@
1-
// eslint-disable-next-line import/extensions,import/no-unresolved
2-
import getLazyHashedEtag from 'webpack/lib/cache/getLazyHashedEtag';
3-
4-
import { util } from 'webpack';
5-
61
export default class Cache {
72
// eslint-disable-next-line no-unused-vars
83
constructor(compilation, ignored) {
9-
this.compilation = compilation;
4+
this.cache = compilation.getCache('CompressionWebpackPlugin');
105
}
116

7+
// eslint-disable-next-line class-methods-use-this
128
isEnabled() {
13-
return Boolean(this.compilation.cache);
14-
}
15-
16-
createCacheIdent(task) {
17-
const {
18-
outputOptions: { hashSalt, hashDigest, hashDigestLength, hashFunction },
19-
} = this.compilation;
20-
21-
const hash = util.createHash(hashFunction);
22-
23-
if (hashSalt) {
24-
hash.update(hashSalt);
25-
}
26-
27-
hash.update(JSON.stringify(task.cacheKeys));
28-
29-
const digest = hash.digest(hashDigest);
30-
const cacheKeys = digest.substr(0, hashDigestLength);
31-
32-
return `${this.compilation.compilerPath}/CompressionPlugin/${cacheKeys}/${task.filename}`;
9+
return true;
3310
}
3411

35-
get(task) {
36-
// eslint-disable-next-line no-param-reassign
37-
task.cacheIdent = task.cacheIdent || this.createCacheIdent(task);
12+
async get(task) {
3813
// eslint-disable-next-line no-param-reassign
39-
task.cacheETag = task.cacheETag || getLazyHashedEtag(task.assetSource);
14+
task.eTag = task.eTag || this.cache.getLazyHashedEtag(task.assetSource);
4015

41-
return new Promise((resolve, reject) => {
42-
this.compilation.cache.get(
43-
task.cacheIdent,
44-
task.cacheETag,
45-
(err, result) => {
46-
if (err) {
47-
reject(err);
48-
} else if (result) {
49-
resolve(result);
50-
} else {
51-
reject();
52-
}
53-
}
54-
);
55-
});
16+
return this.cache.getPromise(task.assetName, task.eTag);
5617
}
5718

58-
store(task, data) {
59-
return new Promise((resolve, reject) => {
60-
this.compilation.cache.store(
61-
task.cacheIdent,
62-
task.cacheETag,
63-
data,
64-
(err) => {
65-
if (err) {
66-
reject(err);
67-
} else {
68-
resolve(data);
69-
}
70-
}
71-
);
72-
});
19+
async store(task) {
20+
return this.cache.storePromise(task.assetName, task.eTag, task.output);
7321
}
7422
}

0 commit comments

Comments
 (0)