Skip to content

Instantly share code, notes, and snippets.

@bynaki
Last active December 8, 2015 03:09
Show Gist options
  • Save bynaki/0b4504bc2873c417dd22 to your computer and use it in GitHub Desktop.
Save bynaki/0b4504bc2873c417dd22 to your computer and use it in GitHub Desktop.
Gulp :: gulp-babel - Install - Usage - API - babel([options]

:origin:

Gulp :: gulp-babel

Use next generation JavaScript, today, with Babel

Issues with the output should be reported on the Babel issue tracker.

Install

$ npm install --save-dev gulp-babel babel-preset-es2015

Usage

const gulp = require('gulp');
const babel = require('gulp-babel');

gulp.task('default', () => {
	return gulp.src('src/app.js')
		.pipe(babel({
			presets: ['es2015']
		}))
		.pipe(gulp.dest('dist'));
});

API

babel([options])

options

See the Babel options, except for sourceMap and filename which is handled for you.

Source Maps

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', () => {
	return gulp.src('src/**/*.js')
		.pipe(sourcemaps.init())
		.pipe(babel({
			presets: ['es2015']
		}))
		.pipe(concat('all.js'))
		.pipe(sourcemaps.write('.'))
		.pipe(gulp.dest('dist'));
});

Babel Metadata

Files in the stream are annotated with a babel property, which contains the metadata from babel.transform().

Example

const gulp = require('gulp');
const babel = require('gulp-babel');
const through = require('through2');

function logFileHelpers() {
	return through.obj((file, enc, cb) => {
		console.log(file.babel.usedHelpers);
		cb(null, file);
	});
}

gulp.task('default', () => {
	return gulp.src('src/**/*.js')
		.pipe(babel({
			presets: ['es2015']
		}))
		.pipe(logFileHelpers);
})

Runtime

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', () => {
	return gulp.src('src/app.js')
		.pipe(babel({
			plugins: ['transform-runtime']
		}))
		.pipe(gulp.dest('dist'));
});

License

MIT © Sindre Sorhus

{"noteId":"1517f8f4391-eb5d08ab","main":"1517f8f4391-eb5d08ab.md","title":"Gulp :: gulp-babel - Install - Usage - API - babel([options]) - options - Source Maps - Babel Metadata - Example - Runtime - License"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment