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) {
|
20 | this.cache = options.cache === true ? Webpack4Cache.getCacheDirectory() : options.cache;
|
21 | this.weakCache = weakCache;
|
22 | }
|
23 |
|
24 | static getCacheDirectory() {
|
25 | return (0, _findCacheDir.default)({
|
26 | name: 'html-minimizer-webpack-plugin'
|
27 | }) || _os.default.tmpdir();
|
28 | }
|
29 |
|
30 | async get(cacheData, sources) {
|
31 | const weakOutput = this.weakCache.get(cacheData.assetSource);
|
32 |
|
33 | if (weakOutput) {
|
34 | return weakOutput;
|
35 | }
|
36 |
|
37 | if (!this.cache) {
|
38 |
|
39 | return undefined;
|
40 | }
|
41 |
|
42 |
|
43 | cacheData.cacheIdent = cacheData.cacheIdent || (0, _serializeJavascript.default)(cacheData.cacheKeys);
|
44 | let cachedResult;
|
45 |
|
46 | try {
|
47 | cachedResult = await _cacache.default.get(this.cache, cacheData.cacheIdent);
|
48 | } catch (ignoreError) {
|
49 |
|
50 | return undefined;
|
51 | }
|
52 |
|
53 | cachedResult.source = new sources.RawSource(JSON.parse(cachedResult.data));
|
54 | return cachedResult;
|
55 | }
|
56 |
|
57 | async store(cacheData) {
|
58 | if (!this.cache) {
|
59 |
|
60 | return undefined;
|
61 | }
|
62 |
|
63 | if (!this.weakCache.has(cacheData.assetSource)) {
|
64 | this.weakCache.set(cacheData.assetSource, cacheData);
|
65 | }
|
66 |
|
67 | const {
|
68 | cacheIdent,
|
69 | source
|
70 | } = cacheData;
|
71 | return _cacache.default.put(this.cache, cacheIdent, JSON.stringify(source.source()));
|
72 | }
|
73 |
|
74 | }
|
75 |
|
76 | exports.default = Webpack4Cache; |
\ | No newline at end of file |