Gulp concat,uglify 处理 ES6 导出的库

Gulp concat, uglify handling ES6 exported libraries

我使用Gulp to merge a few javascripts as well as uglify them. When I tried to do it with countUp(也可以是其他脚本)我遇到了一个错误。

Uncaught SyntaxError: Unexpected token 'export'

它试图将 javascript 导出回脚本标签。现在我的 javascript 文件不仅仅是一个脚本,而是很多。

我怎样才能让它发挥作用?有没有工具可以将它转换为普通的 js 或者更好的方法将它包含在 gulp?

脚本

我的 gulp 文件的一部分如下所示:

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));
}

您可以使用 gulp 任务将 es6 模块转换为其他类型:

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'));
});

然后在您的 gulp 管道中使用此任务。 More info here.