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

Uses a singleton cache to store the compilation stats #723

Merged
merged 6 commits into from
Jul 17, 2017
Merged
Changes from 2 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
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ 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({
Expand Down Expand Up @@ -65,7 +75,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;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can be replaced by compilation.chunks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing this will change the api of chunksSortMode. Before it received two objects with an array property names. Now they will have a string property name. Example:

diff --git a/spec/BasicSpec.js b/spec/BasicSpec.js
index 27ea085..093f85d 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;

Is not documented in the docs (https://github.com/jantimon/html-webpack-plugin#writing-your-own-templates), but it could break some uses. Are you ok with this change?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah probably not :D

I was wrong here. So is everything still works with getStats() then without the need for toJSON()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually a few properties will change:

Before

{ id: 0,
  rendered: true,
  initial: true,
  entry: true,
  extraAsync: false,
  size: 165,
  names: [ 'main' ],
  files: [ 'bundle.js', 'styles.css' ],
  hash: '6db016c6e99e5efcf9e6',
  parents: [],
  origins: [ [Object] ] }

After

Chunk {
  id: 0,
  ids: [ 0 ],
  name: 'main',
  modules: [ [Object], [Object] ],
  chunks: [],
  parents: [],
  blocks: [],
  origins: [ [Object] ],
  files: [ 'bundle.js', 'styles.css' ],
  rendered: true,
  entry: true,
  initial: true,
  hash: '6db016c6e99e5efcf9e636c868282a89',
  renderedHash: '6db016c6e99e5efcf9e6' }

// 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 +262,7 @@ HtmlWebpackPlugin.prototype.executeTemplate = function (templateFunction, chunks
.then(function () {
var templateParams = {
compilation: compilation,
webpack: compilation.getStats().toJson(),
webpack: getStats(compilation),
Copy link
Collaborator

@mastilver mastilver Jun 29, 2017

Choose a reason for hiding this comment

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

I think this is equivalent to just passing compilation

I believe, the only difference between compilation.getStats().toJson() and compilation is that compilation have webpack types while compilation.getStats().toJson() gets us native js types

Not 100% sure on this one thought

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the doc, this should be compilation.getStats(), right?

The following variables are available in the template:

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep you are right 👍

webpackConfig: compilation.options,
htmlWebpackPlugin: {
files: assets,
Expand Down Expand Up @@ -384,7 +394,7 @@ HtmlWebpackPlugin.prototype.isHotUpdateCompilation = function (assets) {

HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function (compilation, chunks) {
var self = this;
var webpackStatsJson = compilation.getStats().toJson();
var webpackStatsJson = getStats(compilation);
Copy link
Collaborator

@mastilver mastilver Jun 29, 2017

Choose a reason for hiding this comment

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

only needed to get the hash, which we can get by doing compilation.hash


// Use the configured public path or build a relative path
var publicPath = typeof compilation.options.output.publicPath !== 'undefined'
Expand Down