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