Gulp Babel Transpiler
gulp-babel is a Gulp plugin that seamlessly integrates Babel into your Gulp build pipeline, enabling the transpilation of modern JavaScript features to widely supported versions. The current stable release is v8.0.0, which provides full compatibility with Babel 7.x. This package is actively maintained by the Babel team, with major version bumps often aligning with core Babel releases, ensuring up-to-date support for the latest JavaScript syntax. It serves as a thin wrapper around Babel's transformation API, exposing a flexible `.pipe()` method for Gulp streams. Key differentiators include its tight integration with the Gulp ecosystem, handling aspects like source map generation via `gulp-sourcemaps`, and providing access to Babel's transformation metadata on processed files. It relies on `@babel/core` as a crucial peer dependency for its core functionality, distinguishing it from direct Babel CLI usage by stream-centric processing.
Common errors
-
Error: Cannot find module '@babel/core'
cause The `@babel/core` package, a required peer dependency, is not installed.fixRun `npm install --save-dev @babel/core` to install the core Babel package. -
ReferenceError: regeneratorRuntime is not defined
cause Using features like async/await or generators without including Babel's runtime transformations.fixInstall `@babel/plugin-transform-runtime` and `@babel/runtime` (`npm install --save-dev @babel/plugin-transform-runtime && npm install --save @babel/runtime`) and add `@babel/plugin-transform-runtime` to your Babel configuration plugins. -
TypeError: (0 , _gulpBabel.default) is not a function
cause Attempting to destructure a default export in CommonJS when the module's default export is the function itself.fixCorrect the import statement to `const babel = require('gulp-babel');` instead of `const { babel } = require('gulp-babel');`.
Warnings
- breaking Starting with v8.0.0 (and its beta releases), the peer dependency for Babel's core was changed from `babel-core` to the scoped package `@babel/core`.
- breaking Version 7.0.0 dropped support for older Node.js versions (0.10 and 0.12) and transitioned `babel-core` to a peer dependency instead of a direct dependency.
- breaking Version 6.0.0 marked a significant upgrade to Babel 6.0.0, introducing major changes in Babel's configuration and plugin ecosystem.
- gotcha When using advanced JavaScript features like generators or async/await, you may encounter `regeneratorRuntime is not defined` errors at runtime.
- gotcha Some Babel options, specifically `sourceMap` and `filename`, are handled internally by `gulp-babel` and should not be explicitly set in the Babel options passed to the plugin.
Install
-
npm install gulp-babel -
yarn add gulp-babel -
pnpm add gulp-babel
Imports
- babel
const { babel } = require('gulp-babel');import babel from 'gulp-babel';
Quickstart
const gulp = require('gulp');
const babel = require('gulp-babel');
const sourcemaps = require('gulp-sourcemaps');
const concat = require('gulp-concat');
gulp.task('default', () =>
gulp.src('src/**/*.js')
.pipe(sourcemaps.init())
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(concat('all.js'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
);
// To run this, ensure you have:
// npm install --save-dev gulp gulp-babel @babel/core @babel/preset-env gulp-sourcemaps gulp-concat
// A 'src' directory with some .js files.