Menu

Babel

April 13, 2017 - javascript

Install packages

npm install --save -dev gulp-babel
npm install --save -dev babel-core
npm install --save-dev babel-preset-env

// optional
// npm install -g babel-cli
npm install --save -dev gulp-cached

Add to gulpfile.js

var babel = require('gulp-babel');
var cached = require('gulp-cached');

gulp.task('babel', function() {
    return gulp.src(src + 'js/babel/babel.js')
    .pipe(cached('js'))
    .pipe(babel())
    .pipe(plugins.rename({
        basename: 'transpiled'
    }))
    .pipe(gulp.dest(src + 'js/babel'));
});

Create file .babelrc in the project folder

Add to .babelrc

{
    "presets": ["env"]
}

Create a file for ES6 code babel.js

Sample code

[1, 2, 3].map(n => n ** 2);

run gulp task

gulp babel

See the changes in transpiled.js

"use strict";

[1, 2, 3].map(function (n) {
  return Math.pow(n, 2);
});
Tags: ,