Introduction to Rollup and Getting Started

Learn what Rollup is, why it works well for JavaScript libraries, and how to create your first bundle.

🎯 Introduction

By the end of this article, you will understand what Rollup is, how it differs from Webpack, and why developers often choose it for JavaScript libraries. We will also install Rollup and create a first bundle with a single command.


🧱 What Is Rollup?

Rollup is a JavaScript module bundler. It combines ES module code spread across multiple files into one or more files that can run directly.

For example, suppose you have two files:

src/math.js
export function add(a, b) {
    return a + b;
}
src/index.js
import { add } from './math.js';

console.log(add(1, 2));

Rollup combines them into one file that a browser or Node.js can use directly, regardless of how many modules the source contains.

Think of Rollup as a bookbinding machine: you give it loose pages (module files), and it binds them into a book (the bundle) in the right order, removing unused blank pages (unused code) along the way.


⚡ How Does Rollup Differ from Webpack?

We have already covered Webpack, so why learn Rollup too? Here is a quick comparison:

AspectWebpackRollup
FocusBundling applicationsBundling libraries
Bundle sizeInjects runtime code, making bundles slightly largerClean output with very little overhead
Tree shakingSupportedPioneered the approach, with more thorough removal
ConfigurationMore features and relatively complex configurationSimple configuration and easy to get started

As a general rule: use Webpack or Vite for web applications, and Rollup for utility and component libraries.

Well-known frameworks such as Vue and React use Rollup to bundle their source code; Vite also uses Rollup for production builds.

Rollup is not intended to replace Webpack; each suits different use cases. This course focuses on Rollup's core features and does not repeat concepts already covered in the Webpack course, such as entry points and output.

🚀 Installing Rollup

This course uses Node.js v22.0.0 and Rollup 4.62.2.

Create a practice project and install Rollup:

mkdir rollup-demo
cd rollup-demo
npm init -y
npm install rollup@4.62.2 --save-dev

Once installed, run it with npx rollup:

npx rollup --version

If the command prints a version number, the installation succeeded.


📦 Your First Bundle

Let's walk through a complete build using the simplest setup.

1. Prepare the Source Code

Create two files in your project:

src/math.js
export function add(a, b) {
    return a + b;
}

export function subtract(a, b) {
    return a - b;
}
src/index.js
import { add } from './math.js';

console.log(add(1, 2));

export { add };

2. Run the Build

Specify the entry point and output file on the command line:

npx rollup src/index.js --file dist/bundle.js

The message created dist/bundle.js indicates that the build succeeded.

3. Inspect the Output

Open dist/bundle.js. It should look roughly like this:

dist/bundle.js
function add(a, b) {
    return a + b;
}

console.log(add(1, 2));

export { add };

Notice two things:

  • The entire add function is included, and the import statement is gone.
  • The subtract function is missing. Since it is never used, Rollup automatically removes it.

The second behavior is Rollup's well-known tree shaking feature, which we will cover in a later article.


❓ Questions and Answers

Q1: Can Rollup bundle CSS and images?

Rollup's core only understands JavaScript modules. CSS and images require plugins. Libraries typically use fewer of these assets, which helps keep Rollup simple.

Q2: What is npx in npx rollup?

npx is a command included with npm. It runs tools installed in your project's node_modules directory without requiring their full paths.

Q3: Can I use a Rollup bundle directly in a browser?

The default output is an ES module, which modern browsers can load with <script type="module">. Older environments require other output formats, which we will cover in the next article.

Q4: Does Vue really use Rollup?

Yes. Popular open-source libraries such as Vue, React, and D3 use Rollup to build their published bundles. Vite's production builds are also based on Rollup.


🧪 Exercise

Follow these steps to create your first Rollup bundle:

  1. Create a rollup-demo project and install rollup@4.62.2.
  2. In src/utils.js, write and export two functions: capitalize, which capitalizes the first letter, and truncate, which truncates a string.
  3. Use only capitalize in src/index.js, then run the build command.
  4. Open the output file and check whether truncate has been removed.
# Run your commands here

🎉 You have completed your first steps with Rollup! Next, we will manage build options with a configuration file to avoid long command-line arguments.