如何解决 Gulp 上的这个缩小错误?

How to solve this minification error on Gulp?(如何解决 Gulp 上的这个缩小错误?)
本文介绍了如何解决 Gulp 上的这个缩小错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

运行此命令时遇到错误

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)

该位置的代码是这样的

 设置器:[],执行:函数(){class MenuItem {//<-- 第 1628 行

怎么了?

解决方案

UglifyJS 目前不支持 EcmaScript 6 结构 类似类.

您可能需要首先通过转译器步骤运行您的 JavaScript,或者找到一个知道如何处理 ES6 代码的压缩程序.

2017-06-17 更新

设计用于 ES6 的 UglifyJS 分支现已发布为 uglify-es 在 npm 上.

2018 年 9 月 10 日更新

terser 是新的 uglify-esuglify-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');函数 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);

这篇关于如何解决 Gulp 上的这个缩小错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)