Gathering detailed insights and metrics for webpack-stats-plugin
Gathering detailed insights and metrics for webpack-stats-plugin
Gathering detailed insights and metrics for webpack-stats-plugin
Gathering detailed insights and metrics for webpack-stats-plugin
Webpack stats plugin for build information, file manifests, etc.
npm install webpack-stats-plugin
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
353 Stars
123 Commits
33 Forks
32 Watching
13 Branches
76 Contributors
Updated on 17 Oct 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-3.9%
96,464
Compared to previous day
Last week
-1.8%
508,278
Compared to previous week
Last month
6.1%
2,231,785
Compared to previous month
Last year
-5.2%
24,978,781
Compared to previous year
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:
1$ npm install --save-dev webpack-stats-plugin 2$ 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:
1$ webpack-cli --plugin webpack-stats-plugin/lib/stats-writer-plugin
A basic webpack.config.js
-based integration:
1const { StatsWriterPlugin } = require("webpack-stats-plugin"); 2 3module.exports = { 4 plugins: [ 5 // Everything else **first**. 6 7 // Write out stats file to build directory. 8 new StatsWriterPlugin({ 9 filename: "stats.json", // Default 10 }), 11 ], 12};
stats
ConfigurationThis option is passed to the webpack compiler's getStats().toJson()
method.
1const { StatsWriterPlugin } = require("webpack-stats-plugin"); 2 3module.exports = { 4 plugins: [ 5 new StatsWriterPlugin({ 6 stats: { 7 all: false, 8 assets: true, 9 }, 10 }), 11 ], 12};
The transform function has a signature of:
1/** 2 * Transform skeleton. 3 * 4 * @param {Object} data Stats object 5 * @param {Object} opts Options 6 * @param {Object} opts.compiler Current compiler instance 7 * @returns {String} String to emit to file 8 */ 9function (data, opts) {}
which you can use like:
1const { StatsWriterPlugin } = require("webpack-stats-plugin"); 2 3module.exports = { 4 plugins: [ 5 new StatsWriterPlugin({ 6 transform(data, opts) { 7 return JSON.stringify( 8 { 9 main: data.assetsByChunkName.main[0], 10 css: data.assetsByChunkName.main[1], 11 }, 12 null, 13 2 14 ); 15 }, 16 }), 17 ], 18};
You can use an asynchronous promise to transform as well:
1const { StatsWriterPlugin } = require("webpack-stats-plugin"); 2 3module.exports = { 4 plugins: [ 5 new StatsWriterPlugin({ 6 filename: "stats-transform-promise.json", 7 transform(data) { 8 return Promise.resolve().then(() => 9 JSON.stringify( 10 { 11 main: data.assetsByChunkName.main, 12 }, 13 null, 14 INDENT 15 ) 16 ); 17 }, 18 }), 19 ], 20};
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):
1"assetsByChunkName": { 2 "main": [ 3 "cd6371d4131fbfbefaa7.bundle.js", 4 "../js-map/cd6371d4131fbfbefaa7.bundle.js.map" 5 ] 6},
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:
1new StatsWriterPlugin({ 2 transform(data) { 3 return JSON.stringify(data, null, 2); 4 }, 5});
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.
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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/13 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
33 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More