This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Rollup.js Cheatsheet

A quick reference guide for Rollup.js: ES module bundler, tree-shaking, configuration options, plugins, output formats, and code splitting.

· Published

Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application.

Basic Configuration (rollup.config.mjs)

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.cjs.js',
      format: 'cjs'
    },
    {
      file: 'dist/bundle.esm.js',
      format: 'es'
    },
    {
      file: 'dist/bundle.min.js',
      format: 'umd',
      name: 'MyLibrary',
      plugins: [terser()]
    }
  ],
  plugins: [
    resolve(),
    commonjs()
  ],
  external: ['react', 'react-dom'] // Do not bundle external peer dependencies
};

Output Formats

Format Option Description
ES Module es / esm Modern standard format (import / export)
CommonJS cjs Node.js standard format (require / module.exports)
UMD umd Universal Module Definition (runs in browser, AMD, CJS)
IIFE iife Immediately Invoked Function Expression for <script> tags

Command Line Usage

# Bundle with configuration file
npx rollup -c

# Watch for changes and rebuild automatically
npx rollup -c -w

# Override environment variables
npx rollup -c --environment BUILD:production

Essential Plugins

  • @rollup/plugin-node-resolve: Resolves third-party modules in node_modules.
  • @rollup/plugin-commonjs: Converts CommonJS modules to ES6.
  • @rollup/plugin-typescript: Compiles TypeScript files.
  • @rollup/plugin-terser: Minifies output bundles.

References

Related topics