Rollup
ES module bundler that tree-shakes unused code and produces compact library bundles in ES, CommonJS, and UMD formats.
Official Documentation ↗v4. Check the official docs for the latest API surface.Installation
npm install --save-dev rolluppnpm add -D rollupMethods
No methods match this filter.
Overview
Rollup is an ES module bundler focused on tree-shaking and clean library output. It is the standard choice for publishing packages as ES, CommonJS, and UMD. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
rollup— Builds an in-memory bundle from the given entry module and options.bundle.generate— Generates bundle code and source maps in memory without writing to disk.bundle.write— Writes the generated chunks and assets to the configured output directory.
Example
const { rollup } = require('rollup');
const bundle = await rollup({ input: 'src/main.js' });
const { output } = await bundle.generate({ format: 'es' });
console.log(output[0].code);
await bundle.close();
Notes
Rollup relies on static analysis, so dynamic require and side-effectful imports weaken tree-shaking. Mark peer dependencies as external to avoid bundling them twice.
Verification
Generate a bundle and confirm unused exports are absent from the output, then assert that external peer packages appear only as import statements.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://rollupjs.org/javascript-api/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:15.539Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- If an error occurs at either stage, it will return a Promise rejected with an Error, which you can identify via their code property. (https://rollupjs.org/javascript-api/)
- Besides code and message , many errors have additional properties you can use for custom reporting, see utils/logs.ts for a complete list of errors and logs together with their codes and properties. (https://rollupjs.org/javascript-api/)
FAQ
- Why does Rollup warn about external dependencies?
- Unresolved imports are treated as external by default. Declare them in the external option or install a resolver plugin such as @rollup/plugin-node-resolve.
- How do I build both ESM and CommonJS outputs?
- Call bundle.generate or bundle.write once per format with { format: "es" } and { format: "cjs" }, or list both in the CLI output config.
- When should I use Rollup instead of webpack?
- Rollup is optimized for libraries and clean ES module output with aggressive tree-shaking, while webpack targets full application bundling with loaders and code splitting.
Report a problem with this API docOpens a pre-filled issue on GitHub.