Rollup — Next-Generation ES Module Bundler for Libraries
Rollup is an ES module bundler designed for building JavaScript libraries and packages. It produces clean, efficient output with excellent tree-shaking, making it the preferred choice for library authors and the production bundler used by Vite.
Ready-to-run agent install
This asset can be installed after the agent chooses its runtime, checks the plan, and runs the matching command.
npx -y tokrepo@latest install e9883503-366b-11f1-9bc6-00163e2b0d79 --target codexRun after dry-run confirms the install plan.
What it is
Rollup is an ES module bundler designed specifically for building JavaScript libraries and packages. Unlike webpack, which targets applications, Rollup produces clean, readable output that uses ES modules natively. It performs aggressive tree-shaking by statically analyzing imports and exports to eliminate dead code.
Rollup is the de facto standard for building JavaScript libraries. React, Vue, Svelte, D3, Three.js, and hundreds of other major libraries use Rollup. It also serves as the production bundler inside Vite.
How it saves time or tokens
Rollup's tree-shaking removes unused code at build time, producing smaller bundles than alternatives. For library authors, this means end users download less code. The clean ESM output is easier to debug than webpack's runtime wrapper. Code splitting lets you ship only what each consumer imports.
How to use
- Install Rollup:
npm install -D rollup - Create a config file
rollup.config.js - Bundle:
npx rollup -c - Use watch mode during development:
npx rollup -c --watch
Example
// rollup.config.js
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import terser from '@rollup/plugin-terser';
export default {
input: 'src/index.js',
output: [
{ file: 'dist/bundle.esm.js', format: 'esm' },
{ file: 'dist/bundle.cjs.js', format: 'cjs' },
{ file: 'dist/bundle.min.js', format: 'iife', name: 'MyLib', plugins: [terser()] }
],
plugins: [resolve(), commonjs()]
};
# Bundle with config
npx rollup -c
# Quick one-liner without config
npx rollup src/index.js --file dist/bundle.js --format esm
Related on TokRepo
- AI Tools for Coding -- Developer tools for building and bundling
- Featured Workflows -- Top-rated workflows on TokRepo
Common pitfalls
- CommonJS modules require the @rollup/plugin-commonjs plugin; without it,
require()calls fail silently - Circular dependencies produce warnings and can cause runtime errors; refactor to break cycles
- The default output format is ESM; specify
format: 'cjs'explicitly if your consumers need CommonJS
Frequently Asked Questions
Rollup is optimized for libraries and produces clean ES module output with aggressive tree-shaking. Webpack is designed for applications with features like hot module replacement, dev server, and code splitting for lazy loading. Most library authors choose Rollup; most app developers choose webpack or Vite.
Vite uses esbuild for development (fast transforms) and Rollup for production builds (mature plugin ecosystem and reliable tree-shaking). This combination gives developers fast dev feedback and optimized production output.
Yes, through plugins. Use @rollup/plugin-typescript or rollup-plugin-ts to compile TypeScript during the bundle step. Rollup itself only handles JavaScript, but the plugin ecosystem covers TypeScript, CSS, JSON, and more.
Tree-shaking is the process of removing unused code from the final bundle. Rollup analyzes static ES module imports and exports to determine which functions and variables are actually used, then eliminates everything else.
Yes. A single rollup.config.js can output ESM, CommonJS, UMD, IIFE, and AMD formats simultaneously. This is standard practice for library authors who need to support different module systems.
Citations (3)
- Rollup GitHub— Rollup is the ES module bundler used by Vite for production builds
- Rollup Documentation— Tree-shaking via static analysis of ES module imports
- Vite Documentation— Vite uses Rollup for production bundling
Related on TokRepo
Discussion
Related Assets
Rolldown — Rust-Powered JavaScript Bundler and the Future of Vite
Rolldown is a Rust-based Rollup-compatible bundler built by the Vite team. It replaces esbuild + Rollup in Vite with a single fast bundler, bringing dramatic speed improvements while keeping the plugin API developers already know.
esbuild — Extremely Fast JavaScript Bundler
esbuild is an extremely fast JavaScript/TypeScript bundler written in Go. 10-100x faster than webpack/Rollup. Powers Vite, Bun, and many modern build tools as their underlying transform engine.
tsup — Bundle Your TypeScript Library with Zero Config
tsup is the zero-config TypeScript bundler built on esbuild. It emits ESM, CJS, IIFE, and DTS files from a single command — the fastest way to ship a polished TypeScript package without writing Rollup config.
zkSync Era — ZK Rollup for Scalable Ethereum Smart Contracts
zkSync Era is a Layer 2 zero-knowledge rollup on Ethereum that provides EVM-compatible smart contract execution with lower gas costs and higher throughput while inheriting Ethereum's security through validity proofs.