ScriptsApr 12, 2026·3 min read

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.

TL;DR
Rollup compiles ES modules into optimized bundles with best-in-class tree-shaking, built for library authors.
§01

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.

§02

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.

§03

How to use

  1. Install Rollup: npm install -D rollup
  2. Create a config file rollup.config.js
  3. Bundle: npx rollup -c
  4. Use watch mode during development: npx rollup -c --watch
§04

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
§05

Related on TokRepo

§06

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

How is Rollup different from webpack?+

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.

Why does Vite use Rollup?+

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.

Does Rollup support TypeScript?+

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.

What is tree-shaking?+

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.

Can Rollup output multiple formats?+

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)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets