@@ -5,31 +5,114 @@ import findCacheDir from 'find-cache-dir';
5
5
import serialize from 'serialize-javascript' ;
6
6
7
7
export default class Webpack4Cache {
8
- constructor ( compilation , options ) {
9
- this . cacheDir =
8
+ constructor ( compilation , options , weakCache ) {
9
+ this . cache =
10
10
options . cache === true
11
11
? Webpack4Cache . getCacheDirectory ( )
12
12
: options . cache ;
13
+ this . weakCache = weakCache ;
13
14
}
14
15
15
16
static getCacheDirectory ( ) {
16
17
return findCacheDir ( { name : 'terser-webpack-plugin' } ) || os . tmpdir ( ) ;
17
18
}
18
19
19
- isEnabled ( ) {
20
- return Boolean ( this . cacheDir ) ;
21
- }
20
+ async get ( cacheData , { RawSource, ConcatSource, SourceMapSource } ) {
21
+ if ( ! this . cache ) {
22
+ // eslint-disable-next-line no-undefined
23
+ return undefined ;
24
+ }
25
+
26
+ const weakOutput = this . weakCache . get ( cacheData . inputSource ) ;
27
+
28
+ if ( weakOutput ) {
29
+ return weakOutput ;
30
+ }
22
31
23
- async get ( task ) {
24
32
// eslint-disable-next-line no-param-reassign
25
- task . cacheIdent = task . cacheIdent || serialize ( task . cacheKeys ) ;
33
+ cacheData . cacheIdent =
34
+ cacheData . cacheIdent || serialize ( cacheData . cacheKeys ) ;
35
+
36
+ let cachedResult ;
37
+
38
+ try {
39
+ cachedResult = await cacache . get ( this . cache , cacheData . cacheIdent ) ;
40
+ } catch ( ignoreError ) {
41
+ // eslint-disable-next-line no-undefined
42
+ return undefined ;
43
+ }
44
+
45
+ cachedResult = JSON . parse ( cachedResult . data ) ;
46
+
47
+ if ( cachedResult . target === 'comments' ) {
48
+ return new ConcatSource ( cachedResult . value ) ;
49
+ }
26
50
27
- const { data } = await cacache . get ( this . cacheDir , task . cacheIdent ) ;
51
+ const {
52
+ code,
53
+ name,
54
+ map,
55
+ input,
56
+ inputSourceMap,
57
+ extractedComments,
58
+ } = cachedResult ;
28
59
29
- return JSON . parse ( data ) ;
60
+ if ( map ) {
61
+ cachedResult . source = new SourceMapSource (
62
+ code ,
63
+ name ,
64
+ map ,
65
+ input ,
66
+ inputSourceMap ,
67
+ true
68
+ ) ;
69
+ } else {
70
+ cachedResult . source = new RawSource ( code ) ;
71
+ }
72
+
73
+ if ( extractedComments ) {
74
+ cachedResult . extractedCommentsSource = new RawSource ( extractedComments ) ;
75
+ }
76
+
77
+ return cachedResult ;
30
78
}
31
79
32
- async store ( task , data ) {
33
- return cacache . put ( this . cacheDir , task . cacheIdent , JSON . stringify ( data ) ) ;
80
+ async store ( cacheData ) {
81
+ if ( ! this . cache ) {
82
+ // eslint-disable-next-line no-undefined
83
+ return undefined ;
84
+ }
85
+
86
+ if ( ! this . weakCache . has ( cacheData . inputSource ) ) {
87
+ if ( cacheData . target === 'comments' ) {
88
+ this . weakCache . set ( cacheData . inputSource , cacheData . output ) ;
89
+ } else {
90
+ this . weakCache . set ( cacheData . inputSource , cacheData ) ;
91
+ }
92
+ }
93
+
94
+ let data ;
95
+
96
+ if ( cacheData . target === 'comments' ) {
97
+ data = {
98
+ target : cacheData . target ,
99
+ value : cacheData . output . source ( ) ,
100
+ } ;
101
+ } else {
102
+ data = {
103
+ code : cacheData . code ,
104
+ name : cacheData . name ,
105
+ map : cacheData . map ,
106
+ input : cacheData . input ,
107
+ inputSourceMap : cacheData . inputSourceMap ,
108
+ } ;
109
+
110
+ if ( cacheData . extractedCommentsSource ) {
111
+ data . extractedComments = cacheData . extractedCommentsSource . source ( ) ;
112
+ data . commentsFilename = cacheData . commentsFilename ;
113
+ }
114
+ }
115
+
116
+ return cacache . put ( this . cache , cacheData . cacheIdent , JSON . stringify ( data ) ) ;
34
117
}
35
118
}
0 commit comments