1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _os = _interopRequireDefault(require("os"));
|
9 |
|
10 | var _cacache = _interopRequireDefault(require("cacache"));
|
11 |
|
12 | var _findCacheDir = _interopRequireDefault(require("find-cache-dir"));
|
13 |
|
14 | var _serializeJavascript = _interopRequireDefault(require("serialize-javascript"));
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | class Webpack4Cache {
|
19 | constructor(compilation, options, weakCache, loader) {
|
20 | this.cache = options.cache === true ? Webpack4Cache.getCacheDirectory() : options.cache;
|
21 | this.weakCache = weakCache;
|
22 | this.loader = loader;
|
23 | }
|
24 |
|
25 | static getCacheDirectory() {
|
26 | return (0, _findCacheDir.default)({
|
27 | name: 'image-minimizer-webpack-plugin'
|
28 | }) || _os.default.tmpdir();
|
29 | }
|
30 |
|
31 | async get(cacheData, sources) {
|
32 | if (!this.cache) {
|
33 |
|
34 | return undefined;
|
35 | }
|
36 |
|
37 | const weakOutput = this.loader ?
|
38 | undefined : this.weakCache.get(cacheData.inputSource);
|
39 |
|
40 | if (weakOutput) {
|
41 | return weakOutput;
|
42 | }
|
43 |
|
44 |
|
45 | cacheData.cacheIdent = cacheData.cacheIdent || (0, _serializeJavascript.default)(cacheData.cacheKeys);
|
46 | let cachedResult;
|
47 |
|
48 | try {
|
49 | cachedResult = await _cacache.default.get(this.cache, cacheData.cacheIdent);
|
50 | } catch (ignoreError) {
|
51 |
|
52 | return undefined;
|
53 | }
|
54 |
|
55 | const result = JSON.parse(cachedResult.data);
|
56 | result.source = Buffer.from(result.source);
|
57 |
|
58 | if (this.loader) {
|
59 | return result;
|
60 | }
|
61 |
|
62 | result.source = new sources.RawSource(result.source);
|
63 | return result;
|
64 | }
|
65 |
|
66 | async store(cacheData) {
|
67 | if (!this.cache) {
|
68 |
|
69 | return undefined;
|
70 | }
|
71 |
|
72 | if (!this.loader && !this.weakCache.has(cacheData.inputSource)) {
|
73 | this.weakCache.set(cacheData.inputSource, cacheData);
|
74 | }
|
75 |
|
76 | let {
|
77 | source
|
78 | } = cacheData;
|
79 |
|
80 | if (!this.loader) {
|
81 | source = source.source();
|
82 | }
|
83 |
|
84 | const {
|
85 | cacheIdent,
|
86 | warnings
|
87 | } = cacheData;
|
88 | return _cacache.default.put(this.cache, cacheIdent, JSON.stringify({
|
89 | source,
|
90 | warnings
|
91 | }));
|
92 | }
|
93 |
|
94 | }
|
95 |
|
96 | exports.default = Webpack4Cache; |
\ | No newline at end of file |