Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache chunks result between multiple entry point html page #802

Closed
wants to merge 9 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ var prettyError = require('./lib/errors.js');
var chunkSorter = require('./lib/chunksorter.js');
Promise.promisifyAll(fs);

/**
* Cache compilation stats between multiple entry point html page
*/
var compilationHashToStatsMap = {};

function HtmlWebpackPlugin (options) {
// Default options
this.options = _.extend({
Expand Down Expand Up @@ -65,7 +70,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 = self.getCompilationStats(compilation).chunks;
// Filter chunks (options.chunks and options.excludeCHunks)
var chunks = self.filterChunks(allChunks, self.options.chunks, self.options.excludeChunks);
// Sort chunks
Expand Down Expand Up @@ -252,7 +257,7 @@ HtmlWebpackPlugin.prototype.executeTemplate = function (templateFunction, chunks
.then(function () {
var templateParams = {
compilation: compilation,
webpack: compilation.getStats().toJson(),
webpack: self.getCompilationStats(compilation),
webpackConfig: compilation.options,
htmlWebpackPlugin: {
files: assets,
Expand Down Expand Up @@ -652,4 +657,16 @@ HtmlWebpackPlugin.prototype.applyPluginsAsyncWaterfall = function (compilation)
};
};

/**
* Get stats from compilation
*/
HtmlWebpackPlugin.prototype.getCompilationStats = function (compilation) {
var stats = compilationHashToStatsMap[compilation.hash];
if (!stats) {
stats = compilation.getStats().toJson();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This toJson call can be passed a number of false options for specific keys to save more time, and limit generation to only the chunk information (which is all that's used by this plugin). You can see an example of that here in another perf PR: https://github.com/jantimon/html-webpack-plugin/pull/825/files#diff-168726dbe96b3ce427e7fedce31bb0bcL67

compilationHashToStatsMap[compilation.hash] = stats;
}
return stats;
};

module.exports = HtmlWebpackPlugin;