Search code examples
javascriptgulpecmascript-6traceur

How to use gulp-traceur and gulp-webpack?


I'm trying to compile ES6 js files. I use gulp-traceur and gulp-wepback in gulp pipe line.

gulp.task('default', function () {
return gulp.src('js/app.js')
    .pipe(traceur())
        .pipe(webpack())
        .pipe(concat('app.js'))
            .pipe(rename({suffix: '.min'}))
            .pipe(uglify())
        .pipe(gulp.dest('build/js'));

When running gulp. I take error: Unexpected reserved word. You may need an appropriate loader to handle this file type. On the line which contains a "class" word. (ES6 Syntax)

I can't figure out how to use these plugins together?


Solution

  • This setup would pass 'js/app.js' to traceur, but none of the related files, and webpack will then start from that transpiled file and process the rest as normal JS. I'm actually not even sure webpack will get the transpile version of app.js.

    The proper way is to use webpack for the main entry point, and tell webpack to transpile all files it comes across. I'd also recommend using Webpack's uglifyjs logic instead of tacking it on after-the-fact with gulp.

    gulp.task('default', function () {
        return gulp.src('js/app.js')
            .pipe(webpack({
                module: {
                    loaders: [{
                        test: /^(?!.*(bower_components|node_modules))+.+\.js$/,
                        loader: 'traceur'
                    }]
                },
                plugins: [
                    new webpack.optimize.UglifyJsPlugin()
                ]
            })
            .pipe(concat('app.js'))
            .pipe(rename({suffix: '.min'}))
            .pipe(gulp.dest('build/js'));
    });