From 0b4d7383ea42829b7eaa190e900a5f36a270b84f Mon Sep 17 00:00:00 2001 From: Ma Cheng Date: Tue, 23 Feb 2016 19:09:18 +0800 Subject: [PATCH 1/2] Add chunk hash to `assets.chunks` Add chunk hash to `assets.chunks` so can use it in template. For some version control requirements, chunk hash may be more useful than compilation hash. For example, if there are two chunks in page, but only one of them is changed, chunk hash is more cache-friendly. --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 9932dd8..1af1f30 100644 --- a/index.js +++ b/index.js @@ -378,6 +378,7 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chu var entry = chunkFiles[0]; assets.chunks[chunkName].size = chunk.size; assets.chunks[chunkName].entry = entry; + assets.chunks[chunkName].hash = chunk.hash; assets.js.push(entry); // Gather all css files From 313a83115adde72f2c9f11ecb4f92ebabf4d9331 Mon Sep 17 00:00:00 2001 From: mc-zone Date: Fri, 26 Feb 2016 20:57:49 +0800 Subject: [PATCH 2/2] add test --- spec/HtmlWebpackPluginSpec.js | 43 +++++++++++++++++++++++++++++++- spec/fixtures/webpackconfig.html | 2 +- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/spec/HtmlWebpackPluginSpec.js b/spec/HtmlWebpackPluginSpec.js index 873952a..58b5167 100644 --- a/spec/HtmlWebpackPluginSpec.js +++ b/spec/HtmlWebpackPluginSpec.js @@ -38,11 +38,19 @@ function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expec return done(); } var htmlContent = fs.readFileSync(path.join(OUTPUT_DIR, outputFile)).toString(); - + var chunksInfo; for (var i = 0; i < expectedResults.length; i++) { var expectedResult = expectedResults[i]; if (expectedResult instanceof RegExp) { expect(htmlContent).toMatch(expectedResult); + } else if (typeof expectedResult === 'object') { + if (expectedResult.type === 'chunkhash') { + if (!chunksInfo) { + chunksInfo = getChunksInfoFromStats(stats); + } + var chunkhash = chunksInfo[expectedResult.chunkName].hash; + expect(htmlContent).toContain(expectedResult.containStr.replace('%chunkhash%', chunkhash)); + } } else { expect(htmlContent).toContain(expectedResult.replace('%hash%', stats.hash)); } @@ -51,6 +59,19 @@ function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expec }); } +function getChunksInfoFromStats (stats) { + var chunks = stats.compilation.getStats().toJson().chunks; + var chunksInfo = {}; + for (var i = 0; i < chunks.length; i++) { + var chunk = chunks[i]; + var chunkName = chunk.names[0]; + if (chunkName) { + chunksInfo[chunkName] = chunk; + } + } + return chunksInfo; +} + describe('HtmlWebpackPlugin', function () { beforeEach(function (done) { rm_rf(OUTPUT_DIR, done); @@ -258,6 +279,26 @@ describe('HtmlWebpackPlugin', function () { }, [' +