Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to only build HTML on initial compile #963

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class HtmlWebpackPlugin {
chunksSortMode: 'auto',
meta: {},
title: 'Webpack App',
xhtml: false
xhtml: false,
onlyBuildOnce: false
}, options);
this.isBuilt = false;
}

apply (compiler) {
Expand Down Expand Up @@ -67,6 +69,10 @@ class HtmlWebpackPlugin {

// Backwards compatible version of: compiler.hooks.make.tapAsync()
(compiler.hooks ? compiler.hooks.make.tapAsync.bind(compiler.hooks.make, 'HtmlWebpackPlugin') : compiler.plugin.bind(compiler, 'make'))((compilation, callback) => {
if (self.options.onlyBuildOnce && self.isBuilt) {
callback();
return;
}
// Compile the template (queued)
compilationPromise = childCompiler.compileTemplate(self.options.template, compiler.context, self.options.filename, compilation)
.catch(err => {
Expand All @@ -88,6 +94,7 @@ class HtmlWebpackPlugin {

// Backwards compatible version of: compiler.plugin.emit.tapAsync()
(compiler.hooks ? compiler.hooks.emit.tapAsync.bind(compiler.hooks.emit, 'HtmlWebpackPlugin') : compiler.plugin.bind(compiler, 'emit'))((compilation, callback) => {
if (self.options.onlyBuildOnce && self.isBuilt) return callback();
const applyPluginsAsyncWaterfall = self.applyPluginsAsyncWaterfall(compilation);
// Get chunks info as json
// Note: we're excluding stuff that we don't need to improve toJson serialization speed.
Expand Down Expand Up @@ -223,6 +230,7 @@ class HtmlWebpackPlugin {
}).then(() => null))
// Let webpack continue with it
.then(() => {
if (self.options.onlyBuildOnce) self.isBuilt = true;
callback();
});
});
Expand Down
30 changes: 30 additions & 0 deletions spec/CachingSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,34 @@ describe('HtmlWebpackPluginCaching', function () {
})
.then(done);
});
it('should not compile the webpack html if any file is changed if only allowed to build on first compile', function (done) {
var template = path.join(__dirname, 'fixtures/plain.html');
var entry = path.join(__dirname, 'fixtures/index.js');
var htmlWebpackPlugin = new HtmlWebpackPlugin({
template: template,
onlyBuildOnce: true
});
var childCompilerHash;
var compiler = setUpCompiler(htmlWebpackPlugin);
compiler.run()
// Change the template and entry files and compile again
.then(function () {
childCompilerHash = htmlWebpackPlugin.childCompilerHash;
compiler.simulateFileChange(template, {footer: '<!-- 1 -->'});
compiler.simulateFileChange(entry, {footer: '//1'});
return compiler.run();
})
.then(function (stats) {
// Verify that only one file was built
expect(getCompiledModuleCount(stats.toJson()))
.toBe(1);
// Verify that the html was processed only during the initial build
expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count())
.toBe(1);
// Verify that the child compilation was executed only once
expect(htmlWebpackPlugin.childCompilerHash)
.toBe(childCompilerHash);
})
.then(done);
});
});