Configuration Files and Basic Options

Use rollup.config.mjs to configure entry points, output, source maps, and watch mode.

🎯 Introduction

By the end of this article, you will be able to manage build options with rollup.config.mjs, use the most common configuration options, and enable watch mode to rebuild automatically when your code changes.


🧱 Why Use a Configuration File?

In the previous article, we built our code from the command line:

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

This works well for a quick introduction. As you add options for formats, plugins, and minification, however, the command becomes long and hard to maintain, and teammates cannot easily inspect the configuration.

In real projects, we put these options in a configuration file. Rollup looks for rollup.config.mjs in the project root by default.


🧱 Creating rollup.config.mjs

Create rollup.config.mjs in the project root with this basic configuration:

rollup.config.mjs
export default {
    input: 'src/index.js',
    output: {
        file: 'dist/bundle.js',
        format: 'es'
    }
};

There are two core fields:

FieldPurpose
inputThe entry file where Rollup starts analyzing dependencies
outputWhere to write the bundle and which format to use

Then run the build:

npx rollup -c

-c is short for --config and tells Rollup to use a configuration file. This produces the same result as the command in the previous article.

Use the .mjs extension so you can write export default without changing the type field in package.json.

🛠 Common output Options

A few additional output options cover most everyday needs:

rollup.config.mjs
export default {
    input: 'src/index.js',
    output: {
        file: 'dist/bundle.js',
        format: 'es',
        sourcemap: true,
        banner: '/* my-utils v1.0.0 */'
    }
};
  • sourcemap: Generates a .map file that maps bundled code back to its source for debugging.
  • banner: Inserts a comment at the top of the bundle, typically for version and copyright information.
  • format: Sets the output format, here es (ES modules). We will explore the available formats in the next article.

After building, open dist/bundle.js to see the banner comment at the top. A bundle.js.map file will also appear alongside it.


👀 Watch Mode

Running the build manually after every change is tedious. Add -w to enable watch mode:

npx rollup -c -w

Rollup keeps watching your source files and automatically rebuilds whenever you save changes, printing build results in the terminal. Press Ctrl + C to stop watching.

Watch mode only builds your code; it does not start a local server to preview pages. For a development setup that refreshes the browser when code changes, use a tool such as Vite. Rollup focuses on generating build output.

📦 Adding npm Scripts

Instead of typing npx rollup -c each time, add it to the scripts in package.json:

package.json
{
    "scripts": {
        "build": "rollup -c",
        "dev": "rollup -c -w"
    }
}

Then you only need:

# During development: watch and rebuild
npm run dev

# Before publishing: build the output
npm run build

These shorter commands also give everyone on the team the same workflow, reducing mistakes.


❓ Questions and Answers

Q1: Can I name the file rollup.config.js?

Yes, but Node.js must interpret it as an ES module, for example by setting "type": "module" in package.json. Using .mjs is the simplest approach.

Q2: What is the difference between output.file and output.dir?

file writes the bundled code to a single file. dir specifies an output directory where Rollup can write multiple files for code splitting. For a small utility library, file is usually enough.

Q3: Does watch mode track newly added files?

Yes. Rollup watches the dependency graph, so a new file referenced indirectly from the entry point will trigger a rebuild when saved.

Q4: Can I add comments to the configuration file?

Yes. rollup.config.mjs is a regular JavaScript file and supports both // and /* */ comments.


🧪 Exercise

Complete these tasks in the rollup-demo project from the previous article:

  1. Create rollup.config.mjs, set src/index.js as the entry point and dist/bundle.js as the output, and enable source maps.
  2. Add build and dev scripts to package.json.
  3. Start watch mode, edit and save src/index.js, and observe the automatic rebuild.
rollup.config.mjs
export default {
    input: 'src/index.js',
    output: {
        file: 'dist/bundle.js',
        format: 'es'
        // Add the sourcemap option here
    }
};

🎉 You now know Rollup's basic configuration! Next, we will explore output formats so your library can run in different environments.