Use next generation JavaScript, today, with Babel
Issues with the output should be reported on the Babel issue tracker.
Install gulp-babel@next
if you want to get the pre-release of the next version of gulp-babel
.
# Babel 7 (use exact dependencies while in beta, not `^`)
$ npm install --save-dev gulp-babel@next @babel/core @babel/preset-env
# Babel 6
$ npm install --save-dev gulp-babel babel-core babel-preset-env
const gulp = require('gulp');
const babel = require('gulp-babel');
gulp.task('default', () =>
gulp.src('src/app.js')
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(gulp.dest('dist'))
);
See the Babel options, except for sourceMap
and filename
which is handled for you.
Use gulp-sourcemaps like this:
const gulp = require('gulp');
const sourcemaps = require('gulp-sourcemaps');
const babel = require('gulp-babel');
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'))
);
Files in the stream are annotated with a babel
property, which contains the metadata from babel.transform()
.
const gulp = require('gulp');
const babel = require('gulp-babel');
const through = require('through2');
function logBabelMetadata() {
return through.obj((file, enc, cb) => {
console.log(file.babel.test); // 'metadata'
cb(null, file);
});
}
gulp.task('default', () =>
gulp.src('src/**/*.js')
.pipe(babel({
// plugin that sets some metadata
plugins: [{
post(file) {
file.metadata.test = 'metadata';
}
}]
}))
.pipe(logBabelMetadta())
)
If you're attempting to use features such as generators, you'll need to add transform-runtime
as a plugin, to include the Babel runtime. Otherwise, you'll receive the error: regeneratorRuntime is not defined
.
Install the runtime:
$ npm install --save-dev @babel/plugin-transform-runtime
Use it as plugin:
const gulp = require('gulp');
const babel = require('gulp-babel');
gulp.task('default', () =>
gulp.src('src/app.js')
.pipe(babel({
plugins: ['@babel/transform-runtime']
}))
.pipe(gulp.dest('dist'))
);
MIT © Sindre Sorhus