Using ES6 with gulp
- As of node 4, we're now able to use ES2015 without the need for Babel. However modules are not currently supported so you'll need to use `require()` still. Checkout the node docs for more info on what's supported. If you'd like module support and to utilise Babel, read on.
- Post updated to use Babel 6.
With gulp 3.9, we are now able to use ES6 (or ES2015 as it's now named) in our gulpfile—thanks to the awesome Babel transpiler.
Firstly make sure you have at least version 3.9 of both the CLI and local version of gulp. To check which version you have, open up terminal and type:
This should return:
If you get any versions lower than 3.9, update gulp in your package.json
file, and run the following to update both versions:
Creating an ES6 gulpfile
To leverage ES6 you will need to install Babel (make sure you have Babel 6) as a dependency to your project, along with the es2015 plugin preset:
Once this has finished, we need to create a .babelrc
config file to enable the es2015 preset:
And add the following to the file:
We then need to instruct gulp to use Babel. To do this, we need to rename the gulpfile.js
to gulpfile.babel.js
:
We can now use ES6 via Babel! An example of a typical gulp task using new ES6 features:
Here we have utilised ES6 import/modules, arrow functions, template strings and constants. If you’d like to check out more ES6 features, es6-features.org is a handy resource.