-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Webpack4Cache.js
36 lines (29 loc) · 890 Bytes
/
Webpack4Cache.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
import os from 'os';
import cacache from 'cacache';
import findCacheDir from 'find-cache-dir';
import serialize from 'serialize-javascript';
export default class Webpack4Cache {
constructor(compiler, compilation, options) {
this.options = options;
this.cacheDir =
options.cache === true
? Webpack4Cache.getCacheDirectory()
: options.cache;
}
static getCacheDirectory() {
return findCacheDir({ name: 'terser-webpack-plugin' }) || os.tmpdir();
}
isEnabled() {
return !!this.cacheDir;
}
get(task) {
// eslint-disable-next-line no-param-reassign
task.cacheIdent = task.cacheIdent || serialize(task.cacheKeys);
return cacache
.get(this.cacheDir, task.cacheIdent)
.then(({ data }) => JSON.parse(data));
}
store(task, data) {
return cacache.put(this.cacheDir, task.cacheIdent, JSON.stringify(data));
}
}