問題描述
運行此命令時遇到錯誤
gulp.task('minify', function () {返回一口.src('public/app/js/myapp.bundle.js').pipe(丑化()).pipe(gulp.dest('public/app/js/myapp.bundle.min.js'));});
<塊引用>
GulpUglifyError: 無法縮小 JavaScript 原因:SyntaxError: Unexpected token: name (MenuItem) (line: 1628, col: 18, pos: 53569)
該位置的代碼是這樣的
設(shè)置器:[],執(zhí)行:函數(shù)(){class MenuItem {//<-- 第 1628 行
怎么了?
UglifyJS 目前不支持 EcmaScript 6 結(jié)構(gòu) 類似類.
您可能需要首先通過轉(zhuǎn)譯器步驟運行您的 JavaScript,或者找到一個知道如何處理 ES6 代碼的壓縮程序.
2017-06-17 更新
設(shè)計用于 ES6 的 UglifyJS 分支現(xiàn)已發(fā)布為 uglify-es
在 npm 上.
2018 年 9 月 10 日更新
terser
是新的 uglify-es
,uglify-es
不再維護.
如果使用 gulp 兩個 npmjs gulp-uglify-es 和 npmjs gulp-terser 軟件包支持 terser.
npm install gulp-terser --save-dev
const gulp = require('gulp');const terser = require('gulp-terser');函數(shù) es(){返回 gulp.src('./src/index.js').pipe(terser()).pipe(gulp.dest('./build'))}gulp.task('default', es);
I'm getting below error running this command
gulp.task('minify', function () {
return gulp
.src('public/app/js/myapp.bundle.js')
.pipe(uglify())
.pipe(gulp.dest('public/app/js/myapp.bundle.min.js'));
});
GulpUglifyError: unable to minify JavaScript Caused by: SyntaxError: Unexpected token: name (MenuItem) (line: 1628, col: 18, pos: 53569)
Code on that location is this
setters: [],
execute: function () {
class MenuItem { // <-- line 1628
What's wrong?
UglifyJS does not currently support EcmaScript 6 structures like classes.
You'll probably need to run your JavaScript through a transpiler step first, or find a minifier that knows what to do with ES6 code.
Update 2017-06-17
The branch of UglifyJS that is designed to work with ES6 is now published as uglify-es
on npm.
Update 2018-09-10
terser
is the new uglify-es
, uglify-es
is no longer maintained.
If using gulp both npmjs gulp-uglify-es and npmjs gulp-terser packages support terser.
npm install gulp-terser --save-dev
const gulp = require('gulp');
const terser = require('gulp-terser');
function es(){
return gulp.src('./src/index.js')
.pipe(terser())
.pipe(gulp.dest('./build'))
}
gulp.task('default', es);
這篇關(guān)于如何解決 Gulp 上的這個縮小錯誤?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!