Skip to content

Commit 27c3e72

Browse files
committedJul 11, 2018
fix: Add dependencies from the child compilation to the main compilation
1 parent 1422664 commit 27c3e72

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed
 

‎index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,22 @@ class HtmlWebpackPlugin {
9797
// Clear the cache once a new HtmlWebpackPlugin is added
9898
childCompiler.clearCache(compiler);
9999

100-
// Clear the cache if the child compiler is outdated
100+
// Register all HtmlWebpackPlugins instances at the child compiler
101101
compiler.hooks.thisCompilation.tap('HtmlWebpackPlugin', (compilation) => {
102+
// Clear the cache if the child compiler is outdated
102103
if (childCompiler.hasOutDatedTemplateCache(compilation)) {
103104
childCompiler.clearCache(compiler);
104105
}
106+
// Add this instances template to the child compiler
105107
childCompiler.addTemplateToCompiler(compiler, this.options.template);
108+
// Add file dependencies of child compiler to parent compiler
109+
// to keep them watched even if we get the result from the cache
110+
compilation.hooks.additionalChunkAssets.tap('HtmlWebpackPlugin', () => {
111+
const childCompilerDependencies = childCompiler.getFileDependencies(compiler);
112+
childCompilerDependencies.forEach(fileDependency => {
113+
compilation.compilationDependencies.add(fileDependency);
114+
});
115+
});
106116
});
107117

108118
// setup hooks for third party plugins

‎lib/compiler.js

+48-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class HtmlWebpackChildCompiler {
3636
* @type {number}
3737
*/
3838
this.compilationStartedTimestamp;
39+
/**
40+
* @type {number}
41+
*/
42+
this.compilationEndedTimestamp;
3943
/**
4044
* All file dependencies of the child compiler
4145
* @type {string[]}
@@ -52,23 +56,38 @@ class HtmlWebpackChildCompiler {
5256
* Add a templatePath to the child compiler
5357
* The given template will be compiled by `compileTemplates`
5458
* @param {string} template - The webpack path to the template e.g. `'!!html-loader!index.html'`
59+
* @returns {boolean} true if the template is new
5560
*/
5661
addTemplate (template) {
5762
const templateId = this.templates.indexOf(template);
5863
// Don't add the template to the compiler if a similar template was already added
5964
if (templateId !== -1) {
60-
return templateId;
65+
return false;
6166
}
6267
// A child compiler can compile only once
6368
// throw an error if a new template is added after the compilation started
64-
if (this.compilationPromise) {
69+
if (this.isCompiling()) {
6570
throw new Error('New templates can only be added before `compileTemplates` was called.');
6671
}
6772
// Add the template to the childCompiler
68-
const newTemplateId = this.templates.length;
6973
this.templates.push(template);
7074
// 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;
7291
}
7392

7493
/**
@@ -158,6 +177,7 @@ class HtmlWebpackChildCompiler {
158177
entry: entries[entryIndex]
159178
};
160179
});
180+
this.compilationEndedTimestamp = new Date().getTime();
161181
resolve(result);
162182
});
163183
});
@@ -266,7 +286,12 @@ function getChildCompiler (mainCompiler) {
266286
* @param {WebpackCompiler} mainCompiler
267287
*/
268288
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+
}
270295
}
271296

272297
/**
@@ -275,7 +300,11 @@ function clearCache (mainCompiler) {
275300
* @param {string} templatePath
276301
*/
277302
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+
}
279308
}
280309

281310
/**
@@ -321,9 +350,21 @@ function hasOutDatedTemplateCache (compilation) {
321350
return childCompiler ? childCompiler.hasOutDatedTemplateCache(compilation) : false;
322351
}
323352

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+
324364
module.exports = {
325365
addTemplateToCompiler,
326366
compileTemplate,
327367
hasOutDatedTemplateCache,
328-
clearCache
368+
clearCache,
369+
getFileDependencies
329370
};

0 commit comments

Comments
 (0)
Please sign in to comment.