From 4114403df2620c83b42e4cd0398d945167541088 Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Mon, 21 Mar 2016 00:10:43 +0100 Subject: [PATCH] Count built files --- spec/CachingSpec.js | 52 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/spec/CachingSpec.js b/spec/CachingSpec.js index a70723d..0e02659 100644 --- a/spec/CachingSpec.js +++ b/spec/CachingSpec.js @@ -31,11 +31,46 @@ function setUpCompiler (htmlWebpackPlugin) { return compiler; } +function getCompiledModuleCount (statsJson) { + return statsJson.modules.filter(function (webpackModule) { + return webpackModule.built; + }).length + statsJson.children.reduce(function (sum, childCompilationStats) { + return sum + getCompiledModuleCount(childCompilationStats); + }, 0); +} + describe('HtmlWebpackPluginCaching', function () { beforeEach(function (done) { rm_rf(OUTPUT_DIR, done); }); + it('should compile nothing if no file was changed', function (done) { + var template = path.join(__dirname, 'fixtures/plain.html'); + var htmlWebpackPlugin = new HtmlWebpackPlugin({ + template: template + }); + var childCompilerHash; + var compiler = setUpCompiler(htmlWebpackPlugin); + compiler.run() + // Change the template file and compile again + .then(function () { + childCompilerHash = htmlWebpackPlugin.childCompilerHash; + return compiler.run(); + }) + .then(function (stats) { + // Verify that no file was built + expect(getCompiledModuleCount(stats.toJson())) + .toBe(0); + // Verify that the html was processed only during the inital build + expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count()) + .toBe(1); + // Verify that the child compilation was executed twice + expect(htmlWebpackPlugin.childCompilerHash) + .toBe(childCompilerHash); + }) + .then(done); + }); + it('should not compile the webpack html file if only a javascript file was changed', function (done) { var htmlWebpackPlugin = new HtmlWebpackPlugin(); var compiler = setUpCompiler(htmlWebpackPlugin); @@ -47,8 +82,11 @@ describe('HtmlWebpackPluginCaching', function () { compiler.simulateFileChange(path.join(__dirname, 'fixtures/index.js'), {footer: '//1'}); return compiler.run(); }) - .then(function () { - // Verify that the html was processed only once + .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 inital build expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count()) .toBe(1); // Verify that the child compilation was executed only once @@ -71,7 +109,10 @@ describe('HtmlWebpackPluginCaching', function () { compiler.simulateFileChange(path.join(__dirname, 'fixtures/index.js'), {footer: '//1'}); return compiler.run(); }) - .then(function () { + .then(function (stats) { + // Verify that only one file was built + expect(getCompiledModuleCount(stats.toJson())) + .toBe(1); // Verify that the html was processed on every run expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count()) .toBe(2); @@ -96,7 +137,10 @@ describe('HtmlWebpackPluginCaching', function () { compiler.simulateFileChange(template, {footer: ''}); return compiler.run(); }) - .then(function () { + .then(function (stats) { + // Verify that only one file was built + expect(getCompiledModuleCount(stats.toJson())) + .toBe(1); // Verify that the html was processed twice expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count()) .toBe(2);