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