rollup-plugin-webpack-stats
Generate rollup stats JSON file with a bundle-stats webpack supported structure.
Install
npm install --dev rollup-plugin-webpack-stats
or
yarn add --dev rollup-plugin-webpack-stats
or
pnpm add -D rollup-plugin-webpack-stats
Configure
import webpackStatsPlugin from 'rollup-plugin-webpack-stats';
export default {
plugins: [
webpackStatsPlugin(),
],
};
import { defineConfig } from 'vite';
import webpackStatsPlugin from 'rollup-plugin-webpack-stats';
export default defineConfig((env) => ({
plugins: [
webpackStatsPlugin(),
],
}));
Options
fileName
- JSON stats file inside rollup/vite output directoryexcludeAssets
- exclude matching assets: string | RegExp | ((filepath: string) => boolean) | Array<string | RegExp | ((filepath: string) => boolean)>
excludeModules
- exclude matching modules: string | RegExp | ((filepath: string) => boolean) | Array<string | RegExp | ((filepath: string) => boolean)>
transform
- access and mutate the resulting stats after the conversion: (stats: WebpackStatsFilterd) => WebpackStatsFilterd
Examples
Output to a custom filename
import webpackStatsPlugin from 'rollup-plugin-webpack-stats';
module.exports = {
plugins: [
webpackStatsPlugin({
filename: 'artifacts/stats.json',
}),
],
};
Exclude .map
files
import webpackStatsPlugin from 'rollup-plugin-webpack-stats';
export default {
plugins: [
webpackStatsPlugin({
excludeAssets: /\.map$/,
}),
],
};
Vite.js - multiple stats files when using plugin-legacy
import { defineConfig } from 'vite';
import legacy from '@vitejs/plugin-legacy';
import webpackStatsPlugin from 'rollup-plugin-webpack-stats';
export default defineConfig((env) => ({
build: {
rollupOptions: {
output: {
plugins: [
webpackStatsPlugin((options) => {
const isLegacy = options.format === 'system';
return {
fileName: `webpack-stats${isLegacy ? '-legacy' : '-modern'}.json`,
};
}),
],
},
},
},
plugins: [
legacy({
}),
],
}));
Vite.js - update initial flag for chunks where the inital flag is incorrectly set to false
import { defineConfig } from 'vite';
import webpackStatsPlugin from 'rollup-plugin-webpack-stats';
export default defineConfig((env) => ({
build: {
rollupOptions: {
output: {
plugins: [
webpackStatsPlugin({
transform: (stats) => {
const mainChunkIndex = stats.chunks?.findIndex((chunk) => chunk.names?.includes("main"));
if (typeof mainChunkIndex !== 'undefined' && stats?.chunks?.[mainChunkIndex]) {
stats.chunks[mainChunkIndex] = {
...stats.chunks[mainChunkIndex],
initial: true,
};
}
return stats;
},
}),
],
},
},
},
}));
Resources