@@ -37,6 +37,10 @@ class HtmlWebpackChildCompiler {
37
37
* @type {number }
38
38
*/
39
39
this . compilationStartedTimestamp ;
40
+ /**
41
+ * @type {number }
42
+ */
43
+ this . compilationEndedTimestamp ;
40
44
/**
41
45
* All file dependencies of the child compiler
42
46
* @type {string[] }
@@ -53,23 +57,38 @@ class HtmlWebpackChildCompiler {
53
57
* Add a templatePath to the child compiler
54
58
* The given template will be compiled by `compileTemplates`
55
59
* @param {string } template - The webpack path to the template e.g. `'!!html-loader!index.html'`
60
+ * @returns {boolean } true if the template is new
56
61
*/
57
62
addTemplate ( template ) {
58
63
const templateId = this . templates . indexOf ( template ) ;
59
64
// Don't add the template to the compiler if a similar template was already added
60
65
if ( templateId !== - 1 ) {
61
- return templateId ;
66
+ return false ;
62
67
}
63
68
// A child compiler can compile only once
64
69
// throw an error if a new template is added after the compilation started
65
- if ( this . compilationPromise ) {
70
+ if ( this . isCompiling ( ) ) {
66
71
throw new Error ( 'New templates can only be added before `compileTemplates` was called.' ) ;
67
72
}
68
73
// Add the template to the childCompiler
69
- const newTemplateId = this . templates . length ;
70
74
this . templates . push ( template ) ;
71
75
// Mark the cache invalid
72
- return newTemplateId ;
76
+ return true ;
77
+ }
78
+
79
+ /**
80
+ * Returns true if the childCompiler is currently compiling
81
+ * @retuns {boolean}
82
+ */
83
+ isCompiling ( ) {
84
+ return ! this . didCompile ( ) && this . compilationStartedTimestamp !== undefined ;
85
+ }
86
+
87
+ /**
88
+ * Returns true if the childCOmpiler is done compiling
89
+ */
90
+ didCompile ( ) {
91
+ return this . compilationEndedTimestamp !== undefined ;
73
92
}
74
93
75
94
/**
@@ -159,6 +178,7 @@ class HtmlWebpackChildCompiler {
159
178
entry : entries [ entryIndex ]
160
179
} ;
161
180
} ) ;
181
+ this . compilationEndedTimestamp = new Date ( ) . getTime ( ) ;
162
182
resolve ( result ) ;
163
183
} ) ;
164
184
} ) ;
@@ -267,7 +287,12 @@ function getChildCompiler (mainCompiler) {
267
287
* @param {WebpackCompiler } mainCompiler
268
288
*/
269
289
function clearCache ( mainCompiler ) {
270
- childCompilerCache . delete ( mainCompiler ) ;
290
+ const childCompiler = getChildCompiler ( mainCompiler ) ;
291
+ // If this childCompiler was already used
292
+ // remove the entire childCompiler from the cache
293
+ if ( childCompiler . isCompiling ( ) || childCompiler . didCompile ( ) ) {
294
+ childCompilerCache . delete ( mainCompiler ) ;
295
+ }
271
296
}
272
297
273
298
/**
@@ -276,7 +301,11 @@ function clearCache (mainCompiler) {
276
301
* @param {string } templatePath
277
302
*/
278
303
function addTemplateToCompiler ( mainCompiler , templatePath ) {
279
- getChildCompiler ( mainCompiler ) . addTemplate ( templatePath ) ;
304
+ const childCompiler = getChildCompiler ( mainCompiler ) ;
305
+ const isNew = childCompiler . addTemplate ( templatePath ) ;
306
+ if ( isNew ) {
307
+ clearCache ( mainCompiler ) ;
308
+ }
280
309
}
281
310
282
311
/**
@@ -322,9 +351,21 @@ function hasOutDatedTemplateCache (compilation) {
322
351
return childCompiler ? childCompiler . hasOutDatedTemplateCache ( compilation ) : false ;
323
352
}
324
353
354
+ /**
355
+ * Return all file dependencies of the last child compilation
356
+ *
357
+ * @param {WebpackCompiler } compiler
358
+ * @returns {Array<string> }
359
+ */
360
+ function getFileDependencies ( compiler ) {
361
+ const childCompiler = getChildCompiler ( compiler ) ;
362
+ return childCompiler . fileDependencies ;
363
+ }
364
+
325
365
module . exports = {
326
366
addTemplateToCompiler,
327
367
compileTemplate,
328
368
hasOutDatedTemplateCache,
329
- clearCache
369
+ clearCache,
370
+ getFileDependencies
330
371
} ;
0 commit comments