webpack-custom-stats-patch

Patches webpack's stats to include custom attributes.

MIT License

Downloads
910
Stars
3

webpack-custom-stats-patch

This module provides a patched copy of webpack's Stats prototype, it extends it to allow you to inject your own custom attributes.

This could be useful when you are writing webpack plugins that generate a specific set of information that you would to pass down to your stat plugins that could be generating things like asset manifests.

For example, you may be writing a webpack plugin to generate subresource integrity hashes that get saved into some mapping into webpack's compilation, but your stats plugin has no way of retrieving the mapping because stats.toJSON() returns only a specific set of the compilation by default [1].

This plugin patches compilation.getStats() and stats.toJson().

You can also reference your custom stats via compilation.__CUSTOM_STATS.

Versioning

If you are using Webpack v1, you should use version 1.x of this package.

If you are using Webpack v2.2.0-rc4 or higher, you should use version 2.x of this package.

Install

# webpack v1.13
npm install [email protected] --save-dev

# webpack v2.2.0-rc.4
npm install webpack-custom-stats-patch --save-dev

Usage

var CustomStats = require('webpack-custom-stats-patch');

var customStats = new CustomStats(compilation);

// Add a custom stat
customStats.addCustomStat('sris', { 'main-123456789.js': 'sha512-9000' });

// Now when `stats.toJson()` is called in your stats plugins, the custom stat
// is available
customStats.toJson().sris
// # => { 'main-123456789.js': 'sha512-9000' });

// You can also view the custom stats directly in compilation
compilation.__CUSTOM_STATS
// # => {
//    sris: {
//      'main-123456789': 'sha512-9000'
//    }
// };

// Replace custom stats in its entirety
// Pass in a plain object
customStats.replaceCustomStats({
  sris: {
    ...
  },

  rails: {
    ...
  }
});

Special Thanks

LICENSE

MIT.