5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
+ import { createHash } from 'crypto' ;
8
9
import * as vm from 'vm' ;
9
10
import { Compilation , EntryPlugin , NormalModule , library , node , sources } from 'webpack' ;
10
11
import { normalizePath } from './ivy/paths' ;
@@ -20,10 +21,18 @@ export class WebpackResourceLoader {
20
21
private _fileDependencies = new Map < string , Set < string > > ( ) ;
21
22
private _reverseDependencies = new Map < string , Set < string > > ( ) ;
22
23
23
- private cache = new Map < string , CompilationOutput > ( ) ;
24
+ private fileCache ?: Map < string , CompilationOutput > ;
25
+ private inlineCache ?: Map < string , CompilationOutput > ;
24
26
private modifiedResources = new Set < string > ( ) ;
25
27
private outputPathCounter = 1 ;
26
28
29
+ constructor ( shouldCache : boolean ) {
30
+ if ( shouldCache ) {
31
+ this . fileCache = new Map ( ) ;
32
+ this . inlineCache = new Map ( ) ;
33
+ }
34
+ }
35
+
27
36
update (
28
37
parentCompilation : Compilation ,
29
38
changedFiles ?: Iterable < string > ,
@@ -35,12 +44,12 @@ export class WebpackResourceLoader {
35
44
if ( changedFiles ) {
36
45
for ( const changedFile of changedFiles ) {
37
46
for ( const affectedResource of this . getAffectedResources ( changedFile ) ) {
38
- this . cache . delete ( normalizePath ( affectedResource ) ) ;
47
+ this . fileCache ? .delete ( normalizePath ( affectedResource ) ) ;
39
48
this . modifiedResources . add ( affectedResource ) ;
40
49
}
41
50
}
42
51
} else {
43
- this . cache . clear ( ) ;
52
+ this . fileCache ? .clear ( ) ;
44
53
}
45
54
}
46
55
@@ -236,15 +245,15 @@ export class WebpackResourceLoader {
236
245
237
246
async get ( filePath : string ) : Promise < string > {
238
247
const normalizedFile = normalizePath ( filePath ) ;
239
- let compilationResult = this . cache . get ( normalizedFile ) ;
248
+ let compilationResult = this . fileCache ? .get ( normalizedFile ) ;
240
249
241
250
if ( compilationResult === undefined ) {
242
251
// cache miss so compile resource
243
252
compilationResult = await this . _compile ( filePath ) ;
244
253
245
254
// Only cache if compilation was successful
246
- if ( compilationResult . success ) {
247
- this . cache . set ( normalizedFile , compilationResult ) ;
255
+ if ( this . fileCache && compilationResult . success ) {
256
+ this . fileCache . set ( normalizedFile , compilationResult ) ;
248
257
}
249
258
}
250
259
@@ -256,7 +265,16 @@ export class WebpackResourceLoader {
256
265
return '' ;
257
266
}
258
267
259
- const compilationResult = await this . _compile ( undefined , data , mimeType ) ;
268
+ const cacheKey = createHash ( 'md5' ) . update ( data ) . digest ( 'hex' ) ;
269
+ let compilationResult = this . inlineCache ?. get ( cacheKey ) ;
270
+
271
+ if ( compilationResult === undefined ) {
272
+ compilationResult = await this . _compile ( undefined , data , mimeType ) ;
273
+
274
+ if ( this . inlineCache && compilationResult . success ) {
275
+ this . inlineCache . set ( cacheKey , compilationResult ) ;
276
+ }
277
+ }
260
278
261
279
return compilationResult . content ;
262
280
}
0 commit comments