Provide option to set toJson options #18

Open
chrisbateman opened this Issue Dec 21, 2016 · 4 comments

Comments

Projects
None yet
2 participants
@chrisbateman

When you generate stats with the Webpack CLI - it passes a particular set of "outputOptions" to toJson(). This results in different output than calling it without any options, which this plugin currently does.

Providing the ability to pass the custom outputOptions into the toJson() call would allow users to customize the results, or make them match the default Webpack results.

@ryan-roemer

This comment has been minimized.

Show comment
Hide comment
@ryan-roemer

ryan-roemer Dec 21, 2016

Member

Hmm... Already you could do something like:

var StatsWriterPlugin = require("webpack-stats-plugin").StatsWriterPlugin;

module.exports = {
  plugins: [
    new StatsWriterPlugin({
      transform: function (data, opts) {
        var stats = opts.compiler.getStats().toJson(/* YOUR OPTIONS */)
        return DO_SOMETHING_WITH_CUSTOMIZED_STATS(stats);
      }
    })
  ]
};

to get you by, bypassing the data and using the compiler directly.

But, I would potentially be open to a PR with an additional options field to punch into the toJson() call. Feel free to strawman it here first!

Member

ryan-roemer commented Dec 21, 2016

Hmm... Already you could do something like:

var StatsWriterPlugin = require("webpack-stats-plugin").StatsWriterPlugin;

module.exports = {
  plugins: [
    new StatsWriterPlugin({
      transform: function (data, opts) {
        var stats = opts.compiler.getStats().toJson(/* YOUR OPTIONS */)
        return DO_SOMETHING_WITH_CUSTOMIZED_STATS(stats);
      }
    })
  ]
};

to get you by, bypassing the data and using the compiler directly.

But, I would potentially be open to a PR with an additional options field to punch into the toJson() call. Feel free to strawman it here first!

@chrisbateman

This comment has been minimized.

Show comment
Hide comment
@chrisbateman

chrisbateman Dec 22, 2016

Shoot, I didn't notice that the transform function provides the compiler. So yeah, this could work:

new StatsWriterPlugin({
  transform: function(data, opts) {
    let stats = opts.compiler.getStats().toJson({chunkModules: true});
    return JSON.stringify(stats, null, 2); 
  }
});

But it does require you to learn about compiler.getStats().toJson() and JSON.stringify(stats, null, 2) which this plugin covers for you otherwise.

An option to pass in the output options might look like this:

new StatsWriterPlugin({
  fields: null,
  stats: {chunkModules: true}
});

Webpack's docs/config uses the property stats but internally it's called outputOptions. Either could work.

Shoot, I didn't notice that the transform function provides the compiler. So yeah, this could work:

new StatsWriterPlugin({
  transform: function(data, opts) {
    let stats = opts.compiler.getStats().toJson({chunkModules: true});
    return JSON.stringify(stats, null, 2); 
  }
});

But it does require you to learn about compiler.getStats().toJson() and JSON.stringify(stats, null, 2) which this plugin covers for you otherwise.

An option to pass in the output options might look like this:

new StatsWriterPlugin({
  fields: null,
  stats: {chunkModules: true}
});

Webpack's docs/config uses the property stats but internally it's called outputOptions. Either could work.

@chrisbateman

This comment has been minimized.

Show comment
Hide comment
@chrisbateman

chrisbateman Dec 22, 2016

Either way, thanks for the quick response!

Either way, thanks for the quick response!

@ryan-roemer

This comment has been minimized.

Show comment
Hide comment
@ryan-roemer

ryan-roemer Jan 11, 2017

Member

Yeah, I'd be open to a PR that adds a field stats that is straight passed to toJson() with links to the documentation you linked in. Thanks!

Member

ryan-roemer commented Jan 11, 2017

Yeah, I'd be open to a PR that adds a field stats that is straight passed to toJson() with links to the documentation you linked in. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment