Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
webpack-stats-plugin
Advanced tools
The webpack-stats-plugin is a plugin for Webpack that allows you to generate and customize the stats output of your Webpack build process. This can be useful for analyzing the build performance, understanding the structure of your bundles, and debugging issues.
Generate Stats JSON
This feature allows you to generate a JSON file containing detailed statistics about your Webpack build. The `StatsWriterPlugin` is configured to output a `stats.json` file with information about assets, chunks, and modules.
const { StatsWriterPlugin } = require('webpack-stats-plugin');
module.exports = {
plugins: [
new StatsWriterPlugin({
filename: 'stats.json',
stats: {
all: false,
assets: true,
chunks: true,
modules: true,
},
}),
],
};
Custom Stats Output
This feature allows you to customize the output of the stats file. The `transform` function is used to modify the data before it is written to the file. In this example, the output JSON file will contain only the assets and module names.
const { StatsWriterPlugin } = require('webpack-stats-plugin');
module.exports = {
plugins: [
new StatsWriterPlugin({
filename: 'custom-stats.json',
transform(data, opts) {
return JSON.stringify({
assets: data.assetsByChunkName,
modules: data.modules.map(module => module.name),
}, null, 2);
},
}),
],
};
The webpack-bundle-analyzer plugin provides a visual representation of the contents of your bundles. It generates an interactive treemap visualization of the contents of all your bundles, which can help you understand the size and composition of your bundles. Unlike webpack-stats-plugin, which focuses on generating stats files, webpack-bundle-analyzer provides a more visual and interactive way to analyze your bundles.
The webpack-dashboard plugin provides a terminal-based dashboard for your Webpack build process. It displays real-time information about the build, including progress, errors, and warnings. This can be useful for getting a quick overview of the build process without having to dig into stats files. Compared to webpack-stats-plugin, webpack-dashboard is more focused on providing real-time feedback during the build process.
The speed-measure-webpack-plugin measures the build time of your Webpack plugins and loaders. It provides detailed timing information that can help you identify performance bottlenecks in your build process. While webpack-stats-plugin focuses on generating detailed stats about the build output, speed-measure-webpack-plugin is specifically designed to help you optimize the build performance.
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.
The plugin is available via npm:
$ npm install --save-dev webpack-stats-plugin
$ yarn add --dev webpack-stats-plugin
We have example webpack configurations for all versions of webpack. See., e.g. test/scenarios/webpack5/webpack.config.js
.
If you are using webpack-cli
, you can enable with:
$ webpack-cli --plugin webpack-stats-plugin/lib/stats-writer-plugin
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
})
]
}
stats
ConfigurationThis 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
}
})
]
}
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);
}
})
]
}
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));
}
})
]
}
StatsWriterPlugin(opts)
Object
) optionsString|Function
) output file name (Default: "stats.json"
)Array
) fields of stats obj to keep (Default: ["assetsByChunkName"]
)Object|String
) stats config object or string preset (Default: {}
)Function|Promise
) transform stats obj (Default: JSON.stringify()
)Boolean
) add stats file to webpack output? (Default: true
)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"
]
},
fields
, stats
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/scenarios/webpack5/webpack.config.js
for various examples including Markdown output.
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.Internal notes
In modern webpack, the plugin uses the processAssets
compilation hook if available when adding the stats object file to the overall compilation to write out along with all the other webpack-built assets. This is the last possible place to hook in before the compilation is frozen in future webpack releases.
In earlier webpack, the plugin uses the much later emit
compiler hook. There are technically some assets/stats data that could be added after processAssets
and before emit
, but for most practical uses of this plugin users shouldn't see any differences in the usable data produced by different versions of webpack.
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
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.
FAQs
Webpack stats plugin
The npm package webpack-stats-plugin receives a total of 443,121 weekly downloads. As such, webpack-stats-plugin popularity was classified as popular.
We found that webpack-stats-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 20 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.