Grunt Rollup Plugin
grunt-rollup is a Grunt plugin that provides an interface to Rollup.js, a next-generation ES6 module bundler, within the Grunt build system. It enables developers to integrate Rollup's advanced tree-shaking and module bundling capabilities directly into their existing Grunt workflows. The package is currently stable at version 12.0.0, released recently with updates to Rollup v2.71.1 and Grunt v1.5.2, reflecting a cadence of updates to stay current with its core dependencies. Its primary function is to abstract the Rollup JavaScript API into Grunt tasks, allowing full configuration of Rollup options, sourcemap generation, and integration of Rollup plugins. This makes it a suitable choice for projects already heavily invested in Grunt that require modern JavaScript module bundling without migrating to a different task runner.
Common errors
-
TypeError: (0 , _pluginBabel.default) is not a function
cause Incorrectly importing a scoped Rollup plugin (like `@rollup/plugin-babel`) in a CommonJS Gruntfile, missing the `.default` property.fixUse `const plugin = require('@rollup/plugin-name').default;` to correctly access the plugin's main function from scoped packages within a CommonJS environment. -
Error: [!] RollupError: Could not resolve 'module-name' from 'path/to/file.js'
cause Rollup cannot find an imported module, often because the module is not installed or the `@rollup/plugin-node-resolve` is missing from the plugins array.fixFirst, ensure all project dependencies are installed (`npm install`). Then, include `@rollup/plugin-node-resolve` in your `options.plugins` array within `Gruntfile.js` to enable Node.js style module resolution. -
Warning: Task "rollup" failed. Use --force to continue.
cause A general Grunt task failure, typically indicating underlying configuration errors in the Rollup setup, such as invalid options, non-existent entry files, or issues with Rollup plugins.fixRun Grunt with verbose logging (`grunt rollup --verbose`) to reveal the detailed Rollup error messages, which will pinpoint the exact issue in your `Gruntfile.js` or Rollup configuration. -
ReferenceError: grunt is not defined
cause The `Gruntfile.js` is not correctly structured, or the `grunt-rollup` plugin (or Grunt itself) has not been properly initialized or loaded.fixEnsure your `Gruntfile.js` adheres to the standard Grunt structure: `module.exports = function(grunt) { ... };` and that `grunt.loadNpmTasks('grunt-rollup');` is called within this function.
Warnings
- breaking The `namespaceToStringTag` output option has been removed in Rollup v2.71.1.
- breaking The `dynamicImportFunction` option has been deprecated, and options like `inlineDynamicImports`, `manualChunks`, and `preserveModules` have been moved to the `output` configuration object.
- breaking Version 11.0.0 updated the underlying Rollup dependency to v2.x. This introduced significant breaking changes within Rollup itself.
- gotcha Some Rollup plugins are stateful (e.g., keeping track of used Babel helpers) and can cause unexpected behavior when reused across multiple bundles in a single 'grunt-rollup' task.
- gotcha The `package.json` for `grunt-rollup` specifies a minimum Node.js version of `>=12`, which is higher than what older `README.md` versions might state (`>=8.6.0`).
Install
-
npm install grunt-rollup -
yarn add grunt-rollup -
pnpm add grunt-rollup
Imports
- grunt.loadNpmTasks
import rollup from 'grunt-rollup';
grunt.loadNpmTasks('grunt-rollup'); - babel
import babel from '@rollup/plugin-babel';
const babel = require('@rollup/plugin-babel').default; - commonjs
import commonjs from '@rollup/plugin-commonjs';
const commonjs = require('@rollup/plugin-commonjs');
Quickstart
const babel = require('@rollup/plugin-babel').default;
module.exports = function(grunt) {
grunt.initConfig({
rollup: {
options: {
// Common Rollup output options, often recommended to be explicit
output: {
format: 'es', // or 'cjs', 'umd', 'iife'
sourcemap: true
},
// Use a function to return plugins for stateful plugins or multiple bundles
plugins: function() {
return [
babel({
babelHelpers: 'bundled',
exclude: './node_modules/**' // Exclude node_modules to speed up transpilation
})
// Add other Rollup plugins here, e.g., require('@rollup/plugin-node-resolve').default()
];
}
},
// Define multiple targets/bundles
app: {
files: {
'dest/bundle.js': 'src/entry.js' // Bundle src/entry.js into dest/bundle.js
}
},
anotherApp: {
files: {
'dest/bundle2.js': 'src/entry2.js' // Bundle src/entry2.js into dest/bundle2.js
}
}
}
});
// Load the grunt-rollup plugin
grunt.loadNpmTasks('grunt-rollup');
// Register a default task to run rollup
grunt.registerTask('default', ['rollup']);
};