Jens Törnell
Jens Törnell

Reputation: 24798

Gulp concat, uglify handling ES6 exported libraries

I use Gulp to merge a few javascripts as well as uglify them. When I tried to do it with countUp (can be other script as well) I've got an error.

Uncaught SyntaxError: Unexpected token 'export'

It tries to export the javascript back to the script tag. Now my javascript file is not just one script, but many.

How can I make it work? Is there a tool to convert it to common js or maybe a better way to include it with gulp?

Script

A part of my gulp-file looks like below:

function script() {
  return gulp
    .src(js.src)
    .pipe(concat(js.filename))
    .pipe(gulp.dest(js.dest))
    .pipe(uglify())
    .pipe(rename({ extname: ".min.js" }))
    .pipe(gulp.dest(js.dest));
}

Upvotes: 0

Views: 303

Answers (1)

inorganik
inorganik

Reputation: 25535

You can use a gulp task to convert es6 modules to other types:

const babel = require('gulp-babel'),

gulp.task('es6-commonjs',['clean-temp'], function(){
  return gulp.src(['app/*.js','app/**/*.js'])
    .pipe(babel({ modules: 'common' }))
    .pipe(gulp.dest('dest/temp'));
});

Then use this task in your gulp pipe. More info here.

Upvotes: 1

Related Questions