From cb150718a3a4b0324bab7a0c5bf37e5340e35847 Mon Sep 17 00:00:00 2001 From: Sergio Cinos Date: Mon, 17 Jul 2017 15:30:18 +1000 Subject: [PATCH] Uses a singleton cache to store the compilation stats (#723) * Uses a singleton cache to store the compilation stats * Use compilation.hash as the index for the cached stats * Remove calls to compilation.getStats().toJson() because it is expensive and we actually don't need it * Remove debugger statement * Make changes compatible with webpack2 * Use json representation for chunks to avoid breaking `chunksSortMode` --- index.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index b6f9578..30c04ca 100644 --- a/index.js +++ b/index.js @@ -252,7 +252,7 @@ HtmlWebpackPlugin.prototype.executeTemplate = function (templateFunction, chunks .then(function () { var templateParams = { compilation: compilation, - webpack: compilation.getStats().toJson(), + webpack: compilation.getStats(), webpackConfig: compilation.options, htmlWebpackPlugin: { files: assets, @@ -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 @@ -384,12 +388,12 @@ HtmlWebpackPlugin.prototype.isHotUpdateCompilation = function (assets) { HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chunks) { var self = this; - var webpackStatsJson = compilation.getStats().toJson(); + 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('/'); @@ -415,8 +419,8 @@ 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++) { @@ -433,7 +437,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); }); }