0

I am using the following gulpfile to compile my javascript code from ES6 to ES5.

var gulp = require('gulp');
var gutil = require('gulp-util');
var cssnano = require('gulp-cssnano');
var autoprefixer = require('gulp-autoprefixer');
var notify = require("gulp-notify");
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var watchify = require('watchify');
var babel = require('gulp-babel');
var babelify = require('babelify');
var uglify = require('gulp-uglify');

//...

gulp.task('js', function () {
    return buildScript('index.js', false);
});


function buildScript(file, watch) {

    var props = {
        entries: [folder_source + '/javascript/' + file],
        debug : true,
        transform:  [babelify]
    };

    // watchify() if watch requested, otherwise run browserify() once
    var bundler = watch ? watchify(browserify(props)) : browserify(props);

    function rebundle() {
        var stream = bundler.bundle();
        return stream
            .on('error', handleErrors)
            .pipe(source(file))
            .pipe(gulp.dest(folder_dest + '/javascript/build/'));
    }

    // listen for an update and run rebundle
    bundler.on('update', function() {
        rebundle();
        gutil.log('Rebundle...');
    });

    // run it once the first time buildScript is called
    return rebundle();
}


function handleErrors() {
    var args = Array.prototype.slice.call(arguments);
    notify.onError({
        title: 'Compile Error',
        message: '<%= error.message %>'
    }).apply(this, args);
    this.emit('end'); // Keep gulp from hanging on this task
}

Everything works fine until Im starting to actually use ES6.

Example Source (index.js):

class Car{

}

Example Compiled (app.js):

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
class Car {}

},{}]},{},[1])

As you can see there is still ES6 code in the compiled file. It seems like babelify is not doing its jop. I already tried several other gulp files from around the web, but I always got the same result.

Thank you!

Simon Schneider
  • 1,216
  • 1
  • 15
  • 23
  • 1
    Not sure if it qualifies as a dup since it's not about the CLI, but http://stackoverflow.com/questions/33440405/babel-file-is-copied-without-being-transformed among others. – loganfsmyth Jan 11 '16 at 17:40

1 Answers1

0

Thanks to loganfsmyth I was able to solve the problem by adding the following to the props array:

  transform: [[babelify, {presets: ["es2015"]}]]
Simon Schneider
  • 1,216
  • 1
  • 15
  • 23