Skip to content

Commit 4249585

Browse files
committed
fix: Add dependencies from the child compilation to the main compilation
1 parent 3f1b97b commit 4249585

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
@@ -87,12 +87,22 @@ class HtmlWebpackPlugin {
8787
// Clear the cache once a new HtmlWebpackPlugin is added
8888
childCompiler.clearCache(compiler);
8989

90-
// Clear the cache if the child compiler is outdated
90+
// Register all HtmlWebpackPlugins instances at the child compiler
9191
compiler.hooks.thisCompilation.tap('HtmlWebpackPlugin', (compilation) => {
92+
// Clear the cache if the child compiler is outdated
9293
if (childCompiler.hasOutDatedTemplateCache(compilation)) {
9394
childCompiler.clearCache(compiler);
9495
}
96+
// Add this instances template to the child compiler
9597
childCompiler.addTemplateToCompiler(compiler, this.options.template);
98+
// Add file dependencies of child compiler to parent compiler
99+
// to keep them watched even if we get the result from the cache
100+
compilation.hooks.additionalChunkAssets.tap('HtmlWebpackPlugin', () => {
101+
const childCompilerDependencies = childCompiler.getFileDependencies(compiler);
102+
childCompilerDependencies.forEach(fileDependency => {
103+
compilation.compilationDependencies.add(fileDependency);
104+
});
105+
});
96106
});
97107

98108
// setup hooks for third party plugins

lib/compiler.js

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

7594
/**
@@ -159,6 +178,7 @@ class HtmlWebpackChildCompiler {
159178
entry: entries[entryIndex]
160179
};
161180
});
181+
this.compilationEndedTimestamp = new Date().getTime();
162182
resolve(result);
163183
});
164184
});
@@ -267,7 +287,12 @@ function getChildCompiler (mainCompiler) {
267287
* @param {WebpackCompiler} mainCompiler
268288
*/
269289
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+
}
271296
}
272297

273298
/**
@@ -276,7 +301,11 @@ function clearCache (mainCompiler) {
276301
* @param {string} templatePath
277302
*/
278303
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+
}
280309
}
281310

282311
/**
@@ -322,9 +351,21 @@ function hasOutDatedTemplateCache (compilation) {
322351
return childCompiler ? childCompiler.hasOutDatedTemplateCache(compilation) : false;
323352
}
324353

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+
325365
module.exports = {
326366
addTemplateToCompiler,
327367
compileTemplate,
328368
hasOutDatedTemplateCache,
329-
clearCache
369+
clearCache,
370+
getFileDependencies
330371
};

0 commit comments

Comments
 (0)