From 55c1a46361709e99239f7ed0dfc4d88da0ce3b1b Mon Sep 17 00:00:00 2001 From: Daniel Woznicki Date: Thu, 31 May 2018 16:27:16 -0700 Subject: [PATCH] Add options.onlyBuildOnce to only build html files on initial compilation Add test for onlyBuildOnce option --- index.js | 10 +++++++++- spec/CachingSpec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5d6d28d0..0c558afd 100644 --- a/index.js +++ b/index.js @@ -33,8 +33,10 @@ class HtmlWebpackPlugin { chunksSortMode: 'auto', meta: {}, title: 'Webpack App', - xhtml: false + xhtml: false, + onlyBuildOnce: false }, options); + this.isBuilt = false; } apply (compiler) { @@ -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 => { @@ -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. @@ -223,6 +230,7 @@ class HtmlWebpackPlugin { }).then(() => null)) // Let webpack continue with it .then(() => { + if (self.options.onlyBuildOnce) self.isBuilt = true; callback(); }); }); diff --git a/spec/CachingSpec.js b/spec/CachingSpec.js index 4377f94f..3cdddbad 100644 --- a/spec/CachingSpec.js +++ b/spec/CachingSpec.js @@ -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: ''}); + 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); + }); });