From b8454b4a55e9b2831e2ace8805329edc19001ed0 Mon Sep 17 00:00:00 2001 From: Sergio Cinos Date: Thu, 29 Jun 2017 16:12:15 +1000 Subject: [PATCH 1/6] Uses a singleton cache to store the compilation stats --- index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ad1710ba..2c9f08f3 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,14 @@ var prettyError = require('./lib/errors.js'); var chunkSorter = require('./lib/chunksorter.js'); Promise.promisifyAll(fs); +var getStats = (function () { + var cachedStats = null; + return function (compilation) { + cachedStats = cachedStats || compilation.getStats().toJson(); + return cachedStats; + }; +}()); + function HtmlWebpackPlugin (options) { // Default options this.options = _.extend({ @@ -65,7 +73,7 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { compiler.plugin('emit', function (compilation, callback) { var applyPluginsAsyncWaterfall = self.applyPluginsAsyncWaterfall(compilation); // Get all chunks - var allChunks = compilation.getStats().toJson().chunks; + var allChunks = getStats(compilation).chunks; // Filter chunks (options.chunks and options.excludeCHunks) var chunks = self.filterChunks(allChunks, self.options.chunks, self.options.excludeChunks); // Sort chunks @@ -252,7 +260,7 @@ HtmlWebpackPlugin.prototype.executeTemplate = function (templateFunction, chunks .then(function () { var templateParams = { compilation: compilation, - webpack: compilation.getStats().toJson(), + webpack: getStats(compilation), webpackConfig: compilation.options, htmlWebpackPlugin: { files: assets, @@ -384,7 +392,7 @@ HtmlWebpackPlugin.prototype.isHotUpdateCompilation = function (assets) { HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chunks) { var self = this; - var webpackStatsJson = compilation.getStats().toJson(); + var webpackStatsJson = getStats(compilation); // Use the configured public path or build a relative path var publicPath = typeof compilation.options.output.publicPath !== 'undefined' From 842c49e3a913fa32e5b6ffea056d730ad0ca984c Mon Sep 17 00:00:00 2001 From: Sergio Cinos Date: Thu, 29 Jun 2017 16:36:03 +1000 Subject: [PATCH 2/6] Use compilation.hash as the index for the cached stats --- index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2c9f08f3..70a2f5b4 100644 --- a/index.js +++ b/index.js @@ -10,10 +10,12 @@ var chunkSorter = require('./lib/chunksorter.js'); Promise.promisifyAll(fs); var getStats = (function () { - var cachedStats = null; + var cachedStats = {}; + return function (compilation) { - cachedStats = cachedStats || compilation.getStats().toJson(); - return cachedStats; + var hash = compilation.hash; + cachedStats[hash] = cachedStats[hash] || compilation.getStats().toJson(); + return cachedStats[hash]; }; }()); From 4f2e6d0301799a6c3d05da142a45ce23b251caaf Mon Sep 17 00:00:00 2001 From: Sergio Cinos Date: Fri, 30 Jun 2017 06:49:34 +1000 Subject: [PATCH 3/6] Remove calls to compilation.getStats().toJson() because it is expensive and we actually don't need it --- index.js | 31 +++++++++++-------------------- spec/BasicSpec.js | 4 ++-- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 70a2f5b4..567d30bc 100644 --- a/index.js +++ b/index.js @@ -9,16 +9,6 @@ var prettyError = require('./lib/errors.js'); var chunkSorter = require('./lib/chunksorter.js'); Promise.promisifyAll(fs); -var getStats = (function () { - var cachedStats = {}; - - return function (compilation) { - var hash = compilation.hash; - cachedStats[hash] = cachedStats[hash] || compilation.getStats().toJson(); - return cachedStats[hash]; - }; -}()); - function HtmlWebpackPlugin (options) { // Default options this.options = _.extend({ @@ -75,7 +65,8 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { compiler.plugin('emit', function (compilation, callback) { var applyPluginsAsyncWaterfall = self.applyPluginsAsyncWaterfall(compilation); // Get all chunks - var allChunks = getStats(compilation).chunks; + var allChunks = compilation.chunks; + debugger; // Filter chunks (options.chunks and options.excludeCHunks) var chunks = self.filterChunks(allChunks, self.options.chunks, self.options.excludeChunks); // Sort chunks @@ -262,7 +253,7 @@ HtmlWebpackPlugin.prototype.executeTemplate = function (templateFunction, chunks .then(function () { var templateParams = { compilation: compilation, - webpack: getStats(compilation), + webpack: compilation.getStats(), webpackConfig: compilation.options, htmlWebpackPlugin: { files: assets, @@ -364,7 +355,7 @@ HtmlWebpackPlugin.prototype.sortChunks = function (chunks, sortMode) { */ HtmlWebpackPlugin.prototype.filterChunks = function (chunks, includedChunks, excludedChunks) { return chunks.filter(function (chunk) { - var chunkName = chunk.names[0]; + var chunkName = chunk.name; // This chunk doesn't have a name. This script can't handled it. if (chunkName === undefined) { return false; @@ -394,12 +385,12 @@ HtmlWebpackPlugin.prototype.isHotUpdateCompilation = function (assets) { HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chunks) { var self = this; - var webpackStatsJson = getStats(compilation); + var compilationHash = compilation.hash; // Use the configured public path or build a relative path var publicPath = typeof compilation.options.output.publicPath !== 'undefined' // If a hard coded public path exists use it - ? compilation.mainTemplate.getPublicPath({hash: webpackStatsJson.hash}) + ? compilation.mainTemplate.getPublicPath({hash: compilationHash}) // If no public path was set get a relative url path : path.relative(path.resolve(compilation.options.output.path, path.dirname(self.childCompilationOutputName)), compilation.options.output.path) .split(path.sep).join('/'); @@ -425,13 +416,13 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chu // Append a hash for cache busting if (this.options.hash) { - assets.manifest = self.appendHash(assets.manifest, webpackStatsJson.hash); - assets.favicon = self.appendHash(assets.favicon, webpackStatsJson.hash); + assets.manifest = self.appendHash(assets.manifest, compilationHash); + assets.favicon = self.appendHash(assets.favicon, compilationHash); } for (var i = 0; i < chunks.length; i++) { var chunk = chunks[i]; - var chunkName = chunk.names[0]; + var chunkName = chunk.name; assets.chunks[chunkName] = {}; @@ -443,7 +434,7 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chu // Append a hash for cache busting if (this.options.hash) { chunkFiles = chunkFiles.map(function (chunkFile) { - return self.appendHash(chunkFile, webpackStatsJson.hash); + return self.appendHash(chunkFile, compilationHash); }); } @@ -452,7 +443,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.chunks[chunkName].hash = chunk.renderedHash; assets.js.push(entry); // Gather all css files diff --git a/spec/BasicSpec.js b/spec/BasicSpec.js index 27ea0853..093f85d3 100644 --- a/spec/BasicSpec.js +++ b/spec/BasicSpec.js @@ -1414,10 +1414,10 @@ describe('HtmlWebpackPlugin', function () { plugins: [ new HtmlWebpackPlugin({ chunksSortMode: function (a, b) { - if (a.names[0] < b.names[0]) { + if (a.name < b.name) { return 1; } - if (a.names[0] > b.names[0]) { + if (a.name > b.name) { return -1; } return 0; From 2651b287c5b8260e980779b1fd0b01cdcae1fc79 Mon Sep 17 00:00:00 2001 From: Sergio Cinos Date: Fri, 30 Jun 2017 06:58:49 +1000 Subject: [PATCH 4/6] Remove debugger statement --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index 567d30bc..c0ecfa26 100644 --- a/index.js +++ b/index.js @@ -66,7 +66,6 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { var applyPluginsAsyncWaterfall = self.applyPluginsAsyncWaterfall(compilation); // Get all chunks var allChunks = compilation.chunks; - debugger; // Filter chunks (options.chunks and options.excludeCHunks) var chunks = self.filterChunks(allChunks, self.options.chunks, self.options.excludeChunks); // Sort chunks From bcc07221ad7d3b07c75a19f42b39bfcc5ad17490 Mon Sep 17 00:00:00 2001 From: Sergio Cinos Date: Fri, 30 Jun 2017 07:14:07 +1000 Subject: [PATCH 5/6] Make changes compatible with webpack2 --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c0ecfa26..629f96ea 100644 --- a/index.js +++ b/index.js @@ -360,7 +360,11 @@ HtmlWebpackPlugin.prototype.filterChunks = function (chunks, includedChunks, exc return false; } // Skip if the chunk should be lazy loaded - if (!chunk.initial) { + if (typeof chunk.isInitial === 'function') { + if (!chunk.isInitial()) { + return false; + } + } else if (!chunk.initial) { return false; } // Skip if the chunks should be filtered and the given chunk was not added explicity From bfd6f5bb334238ac2c8332a2bdc5d71ce7031cf9 Mon Sep 17 00:00:00 2001 From: Sergio Cinos Date: Fri, 30 Jun 2017 09:00:38 +1000 Subject: [PATCH 6/6] Use json representation for chunks to avoid breaking `chunksSortMode` --- index.js | 8 ++++---- spec/BasicSpec.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 629f96ea..4135b4be 100644 --- a/index.js +++ b/index.js @@ -65,7 +65,7 @@ HtmlWebpackPlugin.prototype.apply = function (compiler) { compiler.plugin('emit', function (compilation, callback) { var applyPluginsAsyncWaterfall = self.applyPluginsAsyncWaterfall(compilation); // Get all chunks - var allChunks = compilation.chunks; + var allChunks = compilation.getStats().toJson().chunks; // Filter chunks (options.chunks and options.excludeCHunks) var chunks = self.filterChunks(allChunks, self.options.chunks, self.options.excludeChunks); // Sort chunks @@ -354,7 +354,7 @@ HtmlWebpackPlugin.prototype.sortChunks = function (chunks, sortMode) { */ HtmlWebpackPlugin.prototype.filterChunks = function (chunks, includedChunks, excludedChunks) { return chunks.filter(function (chunk) { - var chunkName = chunk.name; + var chunkName = chunk.names[0]; // This chunk doesn't have a name. This script can't handled it. if (chunkName === undefined) { return false; @@ -425,7 +425,7 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chu for (var i = 0; i < chunks.length; i++) { var chunk = chunks[i]; - var chunkName = chunk.name; + var chunkName = chunk.names[0]; assets.chunks[chunkName] = {}; @@ -446,7 +446,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.renderedHash; + assets.chunks[chunkName].hash = chunk.hash; assets.js.push(entry); // Gather all css files diff --git a/spec/BasicSpec.js b/spec/BasicSpec.js index 093f85d3..27ea0853 100644 --- a/spec/BasicSpec.js +++ b/spec/BasicSpec.js @@ -1414,10 +1414,10 @@ describe('HtmlWebpackPlugin', function () { plugins: [ new HtmlWebpackPlugin({ chunksSortMode: function (a, b) { - if (a.name < b.name) { + if (a.names[0] < b.names[0]) { return 1; } - if (a.name > b.name) { + if (a.names[0] > b.names[0]) { return -1; } return 0;