@@ -33,14 +33,19 @@ class HtmlWebpackChildCompiler {
33
33
*/
34
34
this . compilationPromise ;
35
35
/**
36
- * @type {Date }
36
+ * @type {number }
37
37
*/
38
- this . compilationStarted ;
38
+ this . compilationStartedTimestamp ;
39
39
/**
40
40
* All file dependencies of the child compiler
41
41
* @type {string[] }
42
42
*/
43
43
this . fileDependencies = [ ] ;
44
+ /**
45
+ * Store if the cache was already verified for the given compilation
46
+ * @type {WeakMap<WebpackCompilation, boolean> }}
47
+ */
48
+ this . cacheVerifiedForCompilation = new WeakMap ( ) ;
44
49
}
45
50
46
51
/**
@@ -62,6 +67,7 @@ class HtmlWebpackChildCompiler {
62
67
// Add the template to the childCompiler
63
68
const newTemplateId = this . templates . length ;
64
69
this . templates . push ( template ) ;
70
+ // Mark the cache invalid
65
71
return newTemplateId ;
66
72
}
67
73
@@ -116,7 +122,7 @@ class HtmlWebpackChildCompiler {
116
122
new SingleEntryPlugin ( childCompiler . context , template , `HtmlWebpackPlugin_${ index } ` ) . apply ( childCompiler ) ;
117
123
} ) ;
118
124
119
- this . compilationStarted = new Date ( ) ;
125
+ this . compilationStartedTimestamp = new Date ( ) . getTime ( ) ;
120
126
this . compilationPromise = new Promise ( ( resolve , reject ) => {
121
127
childCompiler . runAsChild ( ( err , entries , childCompilation ) => {
122
128
// Extract templates
@@ -158,6 +164,37 @@ class HtmlWebpackChildCompiler {
158
164
159
165
return this . compilationPromise ;
160
166
}
167
+
168
+ /**
169
+ * Returns `false` if any template file depenendencies has changed
170
+ * for the given main compilation
171
+ *
172
+ * @param {WebpackCompilation } mainCompilation
173
+ * @returns {boolean }
174
+ */
175
+ hasOutDatedTemplateCache ( mainCompilation ) {
176
+ // Check if cache validation was already computed
177
+ const isCacheValid = this . cacheVerifiedForCompilation . get ( mainCompilation ) ;
178
+ if ( isCacheValid !== undefined ) {
179
+ return isCacheValid ;
180
+ }
181
+ // If the compilation was never run there is no invalid cache
182
+ if ( ! this . compilationStartedTimestamp ) {
183
+ this . cacheVerifiedForCompilation . set ( mainCompilation , false ) ;
184
+ return false ;
185
+ }
186
+ // Check if any dependent file was changed after the last compilation
187
+ const fileTimestamps = mainCompilation . fileTimestamps ;
188
+ const isCacheOutOfDate = this . fileDependencies . some ( ( fileDependency ) => {
189
+ const timestamp = fileTimestamps . get ( fileDependency ) ;
190
+ // If the timestamp is not known the file is new
191
+ // If the timestamp is larger then the file has changed
192
+ // Otherwise the file is still the same
193
+ return ! timestamp || timestamp > this . compilationStartedTimestamp ;
194
+ } ) ;
195
+ this . cacheVerifiedForCompilation . set ( mainCompilation , isCacheOutOfDate ) ;
196
+ return isCacheOutOfDate ;
197
+ }
161
198
}
162
199
163
200
/**
@@ -214,10 +251,13 @@ const childCompilerCache = new WeakMap();
214
251
* @param {WebpackCompiler } mainCompiler
215
252
*/
216
253
function getChildCompiler ( mainCompiler ) {
217
- if ( ! childCompilerCache [ mainCompiler ] ) {
218
- childCompilerCache [ mainCompiler ] = new HtmlWebpackChildCompiler ( ) ;
254
+ const cachedChildCompiler = childCompilerCache . get ( mainCompiler ) ;
255
+ if ( cachedChildCompiler ) {
256
+ return cachedChildCompiler ;
219
257
}
220
- return childCompilerCache [ mainCompiler ] ;
258
+ const newCompiler = new HtmlWebpackChildCompiler ( ) ;
259
+ childCompilerCache . set ( mainCompiler , newCompiler ) ;
260
+ return newCompiler ;
221
261
}
222
262
223
263
/**
@@ -226,7 +266,7 @@ function getChildCompiler (mainCompiler) {
226
266
* @param {WebpackCompiler } mainCompiler
227
267
*/
228
268
function clearCache ( mainCompiler ) {
229
- delete ( childCompilerCache [ mainCompiler ] ) ;
269
+ childCompilerCache . delete ( mainCompiler ) ;
230
270
}
231
271
232
272
/**
@@ -252,6 +292,7 @@ function addTemplateToCompiler (mainCompiler, templatePath) {
252
292
function compileTemplate ( templatePath , outputFilename , mainCompilation ) {
253
293
const childCompiler = getChildCompiler ( mainCompilation . compiler ) ;
254
294
return childCompiler . compileTemplates ( mainCompilation ) . then ( ( compiledTemplates ) => {
295
+ if ( ! compiledTemplates [ templatePath ] ) console . log ( Object . keys ( compiledTemplates ) , templatePath ) ;
255
296
const compiledTemplate = compiledTemplates [ templatePath ] ;
256
297
// Replace [hash] placeholders in filename
257
298
const outputName = mainCompilation . mainTemplate . hooks . assetPath . call ( outputFilename , {
@@ -269,8 +310,20 @@ function compileTemplate (templatePath, outputFilename, mainCompilation) {
269
310
} ) ;
270
311
}
271
312
313
+ /**
314
+ * Returns false if the cache is not valid anymore
315
+ *
316
+ * @param {WebpackCompilation } compilation
317
+ * @returns {boolean }
318
+ */
319
+ function hasOutDatedTemplateCache ( compilation ) {
320
+ const childCompiler = childCompilerCache . get ( compilation . compiler ) ;
321
+ return childCompiler ? childCompiler . hasOutDatedTemplateCache ( compilation ) : false ;
322
+ }
323
+
272
324
module . exports = {
273
325
addTemplateToCompiler,
274
326
compileTemplate,
327
+ hasOutDatedTemplateCache,
275
328
clearCache
276
329
} ;
0 commit comments