|
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 | | - |
6 | 1 | export default class Cache { |
7 | 2 | // eslint-disable-next-line no-unused-vars |
8 | 3 | constructor(compilation, ignored) { |
9 | | - this.compilation = compilation; |
| 4 | + this.cache = compilation.getCache('CompressionWebpackPlugin'); |
10 | 5 | } |
11 | 6 |
|
| 7 | + // eslint-disable-next-line class-methods-use-this |
12 | 8 | 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; |
33 | 10 | } |
34 | 11 |
|
35 | | - get(task) { |
36 | | - // eslint-disable-next-line no-param-reassign |
37 | | - task.cacheIdent = task.cacheIdent || this.createCacheIdent(task); |
| 12 | + async get(task) { |
38 | 13 | // 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); |
40 | 15 |
|
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); |
56 | 17 | } |
57 | 18 |
|
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); |
73 | 21 | } |
74 | 22 | } |
0 commit comments