Rollup
Rollup is a next-generation ES module bundler that compiles small pieces of code into larger, more complex libraries or applications. It uses the standardized ES module format, allowing for efficient tree-shaking and optimized bundles. The current stable version is 4.60.1, and it maintains an active release cadence with frequent bug fixes and minor feature additions.
Common errors
-
Error: You must supply a name for UMD bundles
cause The `umd` output format requires a global variable name for the bundle to attach to.fixAdd the `--name "myBundleName"` option when using `output.format: 'umd'` or `rollup --format umd`. -
Error: The generated bundle contains paths that would leave the output directory.
cause Rollup's new validation prevents output files from being written outside the specified `output.dir`.fixAdjust your output configuration (`output.assetFileNames`, `output.chunkFileNames`) to ensure all paths are relative and contained within the `output.dir`. -
rollup: command not found
cause The Rollup CLI is not installed globally or not accessible in the current shell's PATH.fixInstall Rollup globally with `npm install --global rollup` or run it locally using `npx rollup`.
Warnings
- deprecated Returning import attributes from `load` or `transform` hooks is deprecated and will not be supported in Rollup 5.
- breaking Rollup now throws an error if the generated bundle contains paths that would escape the specified output directory.
- gotcha Rollup requires Node.js v18.0.0 or higher.
- gotcha Versions prior to 4.60.1 could incorrectly drop side effect imports due to a caching issue.
Install
-
npm install rollup -
yarn add rollup -
pnpm add rollup
Imports
- rollup
const { rollup } = require('rollup');import { rollup } from 'rollup';
Quickstart
import { rollup } from 'rollup';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
async function build() {
const bundle = await rollup({
input: 'src/main.js',
plugins: [resolve(), commonjs()]
});
await bundle.write({
file: 'dist/bundle.js',
format: 'iife',
name: 'myBundle',
sourcemap: true
});
console.log('Bundle created successfully!');
}
build().catch(console.error);