Introduction to Rollup and Getting Started
🎯 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:
export function add(a, b) {
return a + b;
}
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:
| Aspect | Webpack | Rollup |
|---|---|---|
| Focus | Bundling applications | Bundling libraries |
| Bundle size | Injects runtime code, making bundles slightly larger | Clean output with very little overhead |
| Tree shaking | Supported | Pioneered the approach, with more thorough removal |
| Configuration | More features and relatively complex configuration | Simple 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.
🚀 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:
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
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:
function add(a, b) {
return a + b;
}
console.log(add(1, 2));
export { add };
Notice two things:
- The entire
addfunction is included, and theimportstatement is gone. - The
subtractfunction 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:
- Create a
rollup-demoproject and installrollup@4.62.2. - In
src/utils.js, write and export two functions:capitalize, which capitalizes the first letter, andtruncate, which truncates a string. - Use only
capitalizeinsrc/index.js, then run the build command. - Open the output file and check whether
truncatehas 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.
