rollup-plugin-stats
raw JSON → 2.1.1 verified Mon Apr 27 auth: no javascript
A Vite/Rolldown/Rollup plugin that generates a bundle stats JSON file (stats.json) containing detailed information about assets, chunks, and modules. Current stable version is 2.1.1. The plugin is actively maintained, with regular releases (multiple per month). Key differentiators: works across Vite (v5–v8), Rolldown (v1), and Rollup (v3–v4); supports selective asset/module inclusion/exclusion; integrates with RelativeCI and bundle-stats ecosystem for further analysis. Requires Node >=18.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/rollup-plugin-stats/dist/index.mjs not supported. ↓
cause Using CommonJS require() on an ESM-only version before v2.0.0.
fix
Upgrade to v2.0.0+ or use dynamic import:
const pluginStats = (await import('rollup-plugin-stats')).default. error TypeError: pluginStats is not a function ↓
cause Using `require('rollup-plugin-stats').default` on v2.0.0+ where the module.exports is already the function.
fix
Use
const pluginStats = require('rollup-plugin-stats') (no .default) for CommonJS. error The stats file is empty or missing assets. ↓
cause Plugin not placed as the last plugin in the plugins array.
fix
Move pluginStats() to the end of the plugins array.
error Cannot find module 'rollup-plugin-stats' or its corresponding type declarations. ↓
cause Missing dev dependency or TypeScript not configured to resolve types.
fix
Install with
npm install --save-dev rollup-plugin-stats and ensure tsconfig includes node_modules or has moduleResolution: 'node' or 'bundler'. Warnings
breaking v2.0.0 changed default export: previously required require().default, now require() returns the function directly. ↓
fix Use `import pluginStats from 'rollup-plugin-stats'` or `const pluginStats = require('rollup-plugin-stats')` (no .default).
gotcha Plugin must be added as the last plugin in the plugins array to capture all build output correctly. ↓
fix Place pluginStats() after all other plugins in the plugins array.
deprecated v1.x CommonJS usage with require('rollup-plugin-stats').default is deprecated since v2.0.0; will be removed in future major. ↓
fix Use `const pluginStats = require('rollup-plugin-stats')` directly.
gotcha The stats.json file is written by default with `fs.write` which may overwrite existing files without warning. ↓
fix Either back up existing stats.json or use a custom `write` option to handle overwriting.
gotcha Setting `stats.source: true` includes full source code in the output, potentially increasing file size significantly and exposing source code. ↓
fix Only enable `stats.source` if required for analysis; avoid in production CI artifacts.
Install
npm install rollup-plugin-stats yarn add rollup-plugin-stats pnpm add rollup-plugin-stats Imports
- pluginStats (default) wrong
const pluginStats = require('rollup-plugin-stats').defaultcorrectimport pluginStats from 'rollup-plugin-stats' - PluginStatsOptions
import { PluginStatsOptions } from 'rollup-plugin-stats' - Stats wrong
import { Stats } from 'rollup-plugin-stats'correctimport type { Stats } from 'rollup-plugin-stats'
Quickstart
import pluginStats from 'rollup-plugin-stats';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
pluginStats({
fileName: 'stats.json',
stats: {
source: false,
map: false
}
})
]
});