published on in development
tags: es6 node gulp

Use ES6 in Gulpfile.js

Gulp doesn’t natively support Ecma Script 6 (ES6), so in order to get it working we need to first transpile our gulpfile.js file before it can be read by gulp.

A clean way of acheiving this is to have the default gulp file gulpfile.js that acts as a shim to load and transpile the ES6 version (in my case gulfile.es6.js).

Babel has a neat hook into npm that allows us to automatically transpile required statements on the fly. To install simply add the package:

npm install babel --save-dev

Gulpfile.js

require('babel/register');
require('./gulpfile.es6.js');

The require('babel/register') statement above is the hook that makes all of this possible.

Gulpfile.es6.js

import gulp from 'gulp';

// uses ES6 lambda
gulp.task('hello', () => { console.log('hello'); });

gulp.task('default', ['hello']);

You can find the full example on GitHub that shows the use of external files, generators and classes.