npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2020 – Pkg Stats / Ryan Hefner

webpack-stats-plugin

v0.3.2

Published

Webpack stats plugin

Downloads

1,817,951

Readme

Webpack Stats Plugin

npm version Build Status MIT license Maintenance Status

This plugin will ingest the webpack stats object, process / transform the object and write out to a file for further consumption.

The most common use case is building a hashed bundle and wanting to programmatically refer to the correct bundle path in your Node.js server.

Installation

The plugin is available via npm:

$ npm install --save-dev webpack-stats-plugin
$ yarn add --dev webpack-stats-plugin

Examples

We have example webpack configurations for all versions of webpack. See., e.g. test/webpack4/webpack.config.js.

CLI

If you are using webpack-cli, you can enable with:

$ webpack-cli --plugin webpack-stats-plugin/lib/stats-writer-plugin

Basic

A basic webpack.config.js-based integration:

const { StatsWriterPlugin } = require("webpack-stats-plugin")

module.exports = {
  plugins: [
    // Everything else **first**.

    // Write out stats file to build directory.
    new StatsWriterPlugin({
      filename: "stats.json" // Default
    })
  ]
}

Custom stats Configuration

This option is passed to the webpack compiler's getStats().toJson() method.

const { StatsWriterPlugin } = require("webpack-stats-plugin")

module.exports = {
  plugins: [
    new StatsWriterPlugin({
      stats: {
        all: false,
        assets: true
      }
    })
  ]
}

Custom Transform Function

The transform function has a signature of:

/**
 * Transform skeleton.
 *
 * @param {Object} data           Stats object
 * @param {Object} opts           Options
 * @param {Object} opts.compiler  Current compiler instance
 * @returns {String}              String to emit to file
 */
function (data, opts) {}

which you can use like:

const { StatsWriterPlugin } = require("webpack-stats-plugin");

module.exports = {
  plugins: [
    new StatsWriterPlugin({
      transform(data, opts) {
        return JSON.stringify({
          main: data.assetsByChunkName.main[0],
          css: data.assetsByChunkName.main[1]
        }, null, 2);
      }
    })
  ]
}

Promise transform

You can use an asynchronous promise to transform as well:

const { StatsWriterPlugin } = require("webpack-stats-plugin");

module.exports = {
  plugins: [
    new StatsWriterPlugin({
      filename: "stats-transform-promise.json",
      transform(data) {
        return Promise.resolve().then(() => JSON.stringify({
          main: data.assetsByChunkName.main
        }, null, INDENT));
      }
    })
  ]
}

Plugins

StatsWriterPlugin(opts)

  • opts (Object) options
  • opts.filename (String|Function) output file name (Default: "stats.json")
  • opts.fields (Array) fields of stats obj to keep (Default: ["assetsByChunkName"])
  • opts.stats (Object|String) stats config object or string preset (Default: {})
  • opts.transform (Function|Promise) transform stats obj (Default: JSON.stringify())

Stats writer module.

Stats can be a string or array (we'll have an array due to source maps):

"assetsByChunkName": {
  "main": [
    "cd6371d4131fbfbefaa7.bundle.js",
    "../js-map/cd6371d4131fbfbefaa7.bundle.js.map"
  ]
},

Note: The stats object is big. It includes the entire source included in a bundle. Thus, we default opts.fields to ["assetsByChunkName"] to only include those. However, if you want the whole thing (maybe doing an opts.transform function), then you can set fields: null in options to get all of the stats object.

You may also pass a custom stats config object (or string preset) via opts.stats in order to select exactly what you want added to the data passed to the transform. When opts.stats is passed, opts.fields will default to null.

See:

filename: The opts.filename option can be a file name or path relative to output.path in webpack configuration. It should not be absolute. It may also be a function, in which case it will be passed the current compiler instance and expected to return a filename to use.

transform: By default, the retrieved stats object is JSON.stringify'ed but by supplying an alternate transform you can target any output format. See test/webpack4/webpack.config.js for various examples including Markdown output.

  • Warning: The output of transform should be a String, not an object. On Node v4.x if you return a real object in transform, then webpack will break with a TypeError (See #8). Just adding a simple JSON.stringify() around your object is usually what you need to solve any problems.

Contributions

Contributions welcome!

We test against all versions of webpack. For a full explanation of our functional tests, see test/README.md

To get started, first install:

$ yarn

Our tests first do various webpack builds and then run mocha asserts on the real outputted stats files. Inefficient, but for our small sample size efficient enough.

# Lint and tests
$ yarn run lint
$ yarn run test

# All together
$ yarn run check

Maintenance Status

Active: Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.