Google News
logo
Gulp - Interview Questions
Can you describe how to use Gulp with Babel for ES6 transpilation?
Gulp, a task runner, can be used with Babel for ES6 transpilation. To do this, first install Gulp and Babel by running ‘npm install –save-dev gulp @babel/core @babel/preset-env gulp-babel’. Create a ‘gulpfile.js’ in your project root directory. In the file, require both Gulp and Babel.

Next, define a task to transpile JavaScript using Babel. The code should look like :

const { src, dest } = require('gulp');
const babel = require('gulp-babel');
function transpile() {
  return src('src/**/*.js')
    .pipe(babel({
      presets: ['@babel/env']
    }))
    .pipe(dest('dist'));
}
exports.default = transpile;​


This script will take all ‘.js’ files from the ‘src’ directory, transpile them using Babel, and output the results into the ‘dist’ directory. Run the task using ‘gulp’ command in terminal.
Advertisement