Gulp Plugin for Rollup ES6 Bundler
gulp-best-rollup-2 is a Gulp plugin designed for integrating the Rollup ES6 module bundler into a Gulp build pipeline. As a fork rewritten for Rollup v2.X and modern ES6, it provides a deeper integration than its predecessors and alternatives like `gulp-rollup`. The current stable version is 5.0.3, with maintenance releases occurring as needed for compatibility or bug fixes. Its key differentiator lies in how it handles file paths from `gulp.src()` directly within Rollup, allowing other Gulp plugins to operate effectively in the pipeline before or after the bundling step. This approach deviates from Rollup's typical file-reading mechanism, enabling greater flexibility in complex Gulp workflows by treating Rollup primarily as an in-memory transformation step, rather than a file handler.
Common errors
-
Error: Cannot find module 'rollup'
cause `rollup` is a peer dependency of `gulp-better-rollup-2` and must be installed separately.fixRun `npm install rollup --save-dev` in your project directory. -
Input option must be specified.
cause This error occurs if you are mistakenly passing an `input` option to the `rollup()` plugin call. `gulp-better-rollup-2` expects input to be handled by `gulp.src()`.fixRemove the `input` option from the first argument (Rollup `inputOptions`) passed to the `rollup()` plugin function. -
TypeError: Cannot read properties of undefined (reading 'init')
cause This usually indicates `sourcemaps.init()` was called, but `gulp-sourcemaps` was not properly imported or installed.fixEnsure `gulp-sourcemaps` is installed (`npm install gulp-sourcemaps --save-dev`) and correctly imported (`const sourcemaps = require('gulp-sourcemaps')`).
Warnings
- gotcha Transformations from Gulp plugins executed *before* `gulp-better-rollup` in the pipeline will be lost. The plugin operates on file paths provided by `gulp.src()` directly, not on in-memory buffers transformed by previous plugins.
- gotcha The Rollup `input` option (e.g., `input: 'src/main.js'`) is explicitly unsupported and will be ignored or cause errors.
- gotcha Rollup's native `output.sourcemap` option is unsupported. Sourcemap generation is delegated entirely to `gulp-sourcemaps`.
- gotcha When using `gulp-sourcemaps` with `.mjs` files, the sourcemap linking comment (`//# sourceMappingURL=`) may not be inserted, rendering external sourcemaps ineffective.
- gotcha The Rollup `output.file` argument can only influence the filename if `gulp.dest()` specifies a directory. If `gulp.dest()` specifies a full file path, Rollup's `output.file` is completely ignored.
Install
-
npm install gulp-best-rollup-2 -
yarn add gulp-best-rollup-2 -
pnpm add gulp-best-rollup-2
Imports
- rollup
import { rollup } from 'gulp-better-rollup'const rollup = require('gulp-better-rollup') - gulp
const gulp = require('gulp') - babel
const babel = require('rollup-plugin-babel')
Quickstart
var gulp = require('gulp')
var sourcemaps = require('gulp-sourcemaps')
var rollup = require('gulp-better-rollup')
var babel = require('rollup-plugin-babel')
gulp.task('lib-build', () => {
gulp.src('lib/index.js')
.pipe(sourcemaps.init())
.pipe(rollup({
// There is no `input` option as rollup integrates into the gulp pipeline
plugins: [babel({ babelHelpers: 'bundled' })] // Note: babelHelpers option often needed for babel plugin
}, {
// Rollups `sourcemap` option is unsupported. Use `gulp-sourcemaps` plugin instead
format: 'cjs'
}))
// inlining the sourcemap into the exported .js file
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist'))
})