Scripts2026年4月12日·1 分钟阅读

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.

SC
Script Depot · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

# Install Rollup
npm install -D rollup

# Bundle a file
npx rollup src/index.js --file dist/bundle.js --format esm

# With config file
npx rollup -c

# Watch mode
npx rollup -c --watch

Introduction

Rollup is an ES module bundler that compiles small pieces of code into larger, more complex bundles. Unlike webpack which was designed for applications, Rollup was built specifically for libraries — producing clean, readable output that uses the ES module format natively.

With 26,000+ GitHub stars, Rollup is the de facto standard for building JavaScript libraries. React, Vue, Svelte, D3, Three.js, and hundreds of other major libraries use Rollup for their builds. It also serves as the production bundler inside Vite, giving it enormous indirect adoption.

What Rollup Does

Rollup takes ES modules as input and bundles them into a single file (or multiple chunks) while performing aggressive tree-shaking. Because it works with ES modules natively, it can statically analyze imports and exports to eliminate dead code more effectively than other bundlers.

Architecture Overview

[ES Module Source Files]
         |
    [Module Resolution]
    Follow import statements
    to build dependency graph
         |
    [Tree Shaking]
    Static analysis of
    imports/exports to
    remove unused code
         |
    [Chunk Generation]
    Code splitting based
    on dynamic imports
         |
    [Output Plugins]
    Format: ESM, CJS, UMD, IIFE
         |
[Optimized Bundle(s)]

Self-Hosting & Configuration

// rollup.config.js — library build config
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import terser from "@rollup/plugin-terser";

export default {
  input: "src/index.ts",
  output: [
    {
      file: "dist/index.esm.js",
      format: "esm",
      sourcemap: true
    },
    {
      file: "dist/index.cjs.js",
      format: "cjs",
      sourcemap: true
    }
  ],
  plugins: [
    resolve(),
    commonjs(),
    typescript(),
    terser()
  ],
  external: ["react", "react-dom"]
};

Key Features

  • ES Module Native — designed from the ground up for ES modules
  • Superior Tree Shaking — static analysis eliminates more dead code than alternatives
  • Multiple Output Formats — ESM, CommonJS, UMD, IIFE, AMD, SystemJS
  • Code Splitting — automatic chunking via dynamic imports
  • Plugin Ecosystem — rich plugin system that Vite also supports
  • Clean Output — produces readable, debuggable bundles
  • Scope Hoisting — flattens modules into a single scope for smaller bundles
  • Preserves Modules — option to keep file structure for tree-shakeable packages

Comparison with Similar Tools

Feature Rollup Webpack esbuild Vite Parcel
Primary Use Libraries Applications Speed-critical Applications Applications
Output Quality Excellent Good Good Excellent (uses Rollup) Good
Tree Shaking Best Good Good Best (Rollup) Good
Dev Server No Yes No Yes Yes
Config Style Explicit Explicit Minimal Convention Zero-config
Speed Moderate Slow Very Fast Fast Fast
Adoption for Libs Very High Low Growing N/A Low

FAQ

Q: When should I use Rollup vs webpack? A: Use Rollup for libraries and packages — it produces cleaner output with better tree-shaking. Use webpack for applications that need features like HMR, dev server, and Module Federation.

Q: Why does Vite use Rollup for production builds? A: Rollup produces the highest quality bundles with the best tree-shaking. Vite uses esbuild for fast dev transforms but Rollup for production where output quality matters more than build speed.

Q: What is Rolldown? A: Rolldown is a Rust-based reimplementation of Rollup being developed for Vite. It aims to provide Rollup-compatible output at native speed, potentially replacing both esbuild and Rollup in the Vite build pipeline.

Q: How do I publish a library with both ESM and CJS? A: Configure Rollup with multiple outputs (format: "esm" and format: "cjs"), then use the "exports" field in package.json to map import and require to the correct files.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产