如何uglify javascript类? [英] how to uglify javascript classes?
问题描述
我使用类语法撰写基本的javascript < a>并尝试使用gulp 修正它,但遇到此错误:
I'm writing basic javascript using the classes syntax and trying to uglify it with gulp, but getting this error:
events.js:72
throw er; //未处理的错误事件
events.js:72
throw er; // Unhandled 'error' event
我只是使用mozilla网站的示例代码:
I simply used the example code from mozilla's website:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
我的gulp文件没有什么特别的任何非类js:
And nothing special about my gulp file - works perfectly for any non-class js:
// Include gulp
var gulp = require('gulp');
// Include Our Plugins
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
// Lint Task
gulp.task('lint', function() {
return gulp.src('js/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// Concatenate & Minify JS
gulp.task('scripts', function() {
return gulp.src('js/*.js')
.pipe(concat('all.js'))
.pipe(gulp.dest('dist/js'))
.pipe(rename('all.min.js'))
.pipe(uglify())
.pipe(gulp.dest('dist/js'));
});
// Watch Files For Changes
gulp.task('watch', function() {
gulp.watch('js/*.js', ['lint', 'scripts']);
gulp.watch('scss/*.scss', ['sass']);
});
// Default Task
gulp.task('default', ['lint', 'scripts', 'watch']);
推荐答案
稍后我发现这不是我的Node环境的问题,因为评论让我相信,但实际上是因为 gulp-uglify中使用的默认uglify分支不支持ECMA 6。
Later I discovered it was not a problem with my Node environment as the comments led me to believe, but in fact it was because the default uglify branch used in gulp-uglify doesn't support ECMA 6 yet.
正确的解决方案是改为请使用UglifyJS2的 harmony
分支或使用npm包装 uglify-js-harmony 。
The correct solution was to instead use the harmony
branch of UglifyJS2 or to use the npm wrapper uglify-js-harmony.
这篇关于如何uglify javascript类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!