保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const crypto = require('crypto');
 
const pluginCompat = require('./util/plugin-compat');
const relateContext = require('./util/relate-context');
const { parityCacheFromCache, pushParityWriteOps } = require('./util/parity');
 
function requestHash(request) {
  return crypto
    .createHash('sha1')
    .update(request)
    .digest()
    .hexSlice();
}
 
const relateNormalRequest = relateContext.relateNormalRequest;
 
class AssetCache {
  apply(compiler) {
    const compilerHooks = pluginCompat.hooks(compiler);
 
    let assetCache = {};
    let parityCache = {};
 
    const assetArchetypeCache = {
      _ops: [],
 
      get(id) {
        const hashId = requestHash(relateNormalRequest(compiler, id));
        if (assetCache[hashId]) {
          if (typeof assetCache[hashId] === 'string') {
            assetCache[hashId] = JSON.parse(assetCache[hashId]);
          }
          return assetCache[hashId];
        }
      },
 
      set(id, item) {
        const hashId = requestHash(relateNormalRequest(compiler, id));
        if (item) {
          assetCache[hashId] = item;
          this._ops.push({
            key: hashId,
            value: item,
          });
        } else {
          assetCache[hashId] = null;
          this._ops.push({
            key: hashId,
            value: null,
          });
        }
      },
 
      operations() {
        const ops = this._ops.slice();
        this._ops.length = 0;
        return ops;
      },
    };
 
    compilerHooks._hardSourceArchetypeRegister.call(
      'Asset',
      assetArchetypeCache,
    );
 
    let assetCacheSerializer;
    let assetParityCacheSerializer;
 
    compilerHooks._hardSourceCreateSerializer.tap(
      'HardSource - AssetCache',
      (cacheSerializerFactory, cacheDirPath) => {
        assetCacheSerializer = cacheSerializerFactory.create({
          name: 'assets',
          type: 'file',
          cacheDirPath,
        });
        assetParityCacheSerializer = cacheSerializerFactory.create({
          name: 'assets-parity',
          type: 'data',
          cacheDirPath,
        });
      },
    );
 
    compilerHooks._hardSourceResetCache.tap('HardSource - AssetCache', () => {
      assetCache = {};
      parityCache = {};
    });
 
    compilerHooks._hardSourceReadCache.tapPromise(
      'HardSource - AssetCache',
      () =>
        Promise.all([
          assetCacheSerializer.read().then(_assetCache => {
            assetCache = _assetCache;
          }),
          assetParityCacheSerializer.read().then(_parityCache => {
            parityCache = _parityCache;
          }),
        ]),
    );
 
    compilerHooks._hardSourceParityCache.tap(
      'HardSource - AssetCache',
      parityRoot => {
        parityCacheFromCache('Asset', parityRoot, parityCache);
      },
    );
 
    compilerHooks._hardSourceWriteCache.tapPromise(
      'HardSource - AssetCache',
      compilation => {
        const assetOps = assetArchetypeCache.operations();
 
        const parityOps = [];
        pushParityWriteOps(compilation, parityOps);
 
        return Promise.all([
          assetCacheSerializer.write(assetOps),
          assetParityCacheSerializer.write(parityOps),
        ]);
      },
    );
  }
}
 
module.exports = AssetCache;