forked from webpack-contrib/terser-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Webpack5Cache.js
65 lines (57 loc) · 1.59 KB
/
Webpack5Cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// eslint-disable-next-line import/extensions,import/no-unresolved
import getLazyHashedEtag from 'webpack/lib/cache/getLazyHashedEtag';
import serialize from 'serialize-javascript';
import TerserPlugin from './index';
export default class Cache {
constructor(compiler, compilation, options) {
this.compiler = compiler;
this.compilation = compilation;
this.options = options;
}
isEnabled() {
return !!this.compilation.cache;
}
createCacheIdent(task) {
const cacheKeys = TerserPlugin.getHasher(this.compiler)
.update(serialize(task.cacheKeys))
.digest('hex');
return `${this.compilation.compilerPath}/TerserWebpackPlugin/${cacheKeys}/${task.file}`;
}
get(task) {
// eslint-disable-next-line no-param-reassign
task.cacheIdent = task.cacheIdent || this.createCacheIdent(task);
// eslint-disable-next-line no-param-reassign
task.cacheETag = task.cacheETag || getLazyHashedEtag(task.asset);
return new Promise((resolve, reject) => {
this.compilation.cache.get(
task.cacheIdent,
task.cacheETag,
(err, result) => {
if (err) {
reject(err);
} else if (result) {
resolve(result);
} else {
reject();
}
}
);
});
}
store(task, data) {
return new Promise((resolve, reject) => {
this.compilation.cache.store(
task.cacheIdent,
task.cacheETag,
data,
(err) => {
if (err) {
reject(err);
} else {
resolve(data);
}
}
);
});
}
}