Babel transpilation for Grunt
grunt-babel is a Grunt plugin that integrates Babel, allowing developers to transpile modern JavaScript code (ESNext) into backward-compatible versions for older environments using the Grunt task runner. The current stable version is v8.0.0, which is specifically designed for compatibility with Babel 7.x. While Grunt itself has a slower release cadence compared to modern build tools, `grunt-babel` follows Babel's major version releases closely, necessitating updates to maintain compatibility. Its primary differentiator is its deep integration into the Grunt ecosystem, providing a familiar configuration experience for projects already utilizing Grunt for their build processes. It leverages Babel's extensive plugin and preset system, enabling a wide range of transformations, from JSX and TypeScript syntax stripping to polyfilling modern JavaScript features, making it suitable for projects that prefer a Grunt-centric workflow over newer alternatives like Webpack or Rollup.
Common errors
-
Error: Cannot find module '@babel/core'
cause The `@babel/core` peer dependency is not installed.fixRun `npm install --save-dev @babel/core` or `yarn add --dev @babel/core`. -
Error: You have not specified any presets or plugins for babel.
cause Babel requires at least one preset or plugin to perform transformations. The options are empty or misconfigured.fixAdd a preset like `@babel/preset-env` to your `babel.options.presets` array: `presets: ['@babel/preset-env']`. Ensure the preset package is also installed. -
SyntaxError: 'import' and 'export' may only appear at the top level (when running outputted JS)
cause The transpiled output still contains ES module syntax but is being run in an environment that expects CommonJS, likely because a preset like `@babel/preset-env` was configured not to transform modules.fixEnsure `@babel/preset-env` is configured to transform modules, e.g., `presets: [['@babel/preset-env', { modules: 'commonjs' }]]`.
Warnings
- breaking grunt-babel v8 requires Babel 7.x. Upgrading from v7 (Babel 6.x) requires updating all Babel related packages (e.g., `babel-core` to `@babel/core`, `babel-preset-env` to `@babel/preset-env`).
- breaking Node.js v4 support was dropped in grunt-babel v8. Projects using older Node.js versions must remain on grunt-babel v7.
- breaking The `babel-core` package became a peerDependency in v7.0.0. Failure to install it will lead to runtime errors as `grunt-babel` cannot find the core transpiler.
- gotcha Issues with transpilation output (e.g., incorrect syntax, missing polyfills) are generally problems with Babel itself, not `grunt-babel`. These should be reported to the Babel issue tracker.
Install
-
npm install grunt-babel -
yarn add grunt-babel -
pnpm add grunt-babel
Imports
- babel
import { babel } from 'grunt-babel';grunt.initConfig({ babel: { /* ... */ } }); - options
const babelOptions = require('grunt-babel').options;grunt.initConfig({ babel: { options: { sourceMap: true, presets: ['@babel/preset-env'] }, // ... } }); - default task registration
grunt.registerTask('babel', ['default']);grunt.registerTask('default', ['babel']);
Quickstart
require('load-grunt-tasks')(grunt); // npm install --save-dev load-grunt-tasks
grunt.initConfig({
babel: {
options: {
sourceMap: true,
presets: ['@babel/preset-env'] // Ensure @babel/preset-env is installed
},
dist: {
files: {
'dist/app.js': 'src/app.js'
}
}
}
});
grunt.registerTask('default', ['babel']);