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.
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
Flax — Neural Network Library for JAX
A high-performance neural network library built on JAX, providing a flexible module system used extensively across Google DeepMind and the JAX research community.
PyCaret — Low-Code Machine Learning in Python
An open-source AutoML library that wraps scikit-learn, XGBoost, LightGBM, CatBoost, and other ML libraries into a unified low-code interface for rapid experimentation.
DGL — Deep Graph Library for Scalable Graph Neural Networks
A high-performance framework for building graph neural networks on top of PyTorch, TensorFlow, or MXNet, designed for both research prototyping and production-scale graph learning.