如何使用 gulp-uglify 缩小 ES6 函数?

How to minify ES6 functions with gulp-uglify?(如何使用 gulp-uglify 缩小 ES6 函数?)
本文介绍了如何使用 gulp-uglify 缩小 ES6 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

当我运行 gulp 时,我收到以下错误:

When I run gulp I get the following error:

[12:54:14] { [GulpUglifyError: unable to minify JavaScript]
cause:
{ [SyntaxError: Unexpected token: operator (>)]
 message: 'Unexpected token: operator (>)',
 filename: 'bundle.js',
 line: 3284,
 col: 46,
 pos: 126739 },
plugin: 'gulp-uglify',
fileName: 'C:\servers\vagrant\workspace\awesome\web\tool\bundle.js',
showStack: false }

违规行包含箭头函数:

let zeroCount = numberArray.filter(v => v === 0).length

我知道我可以将其替换为以下内容,以通过放弃 ES6 语法来纠正缩小错误:

I know I can replace it with the following to remedy the minification error by abandoning ES6 syntax:

let zeroCount = numberArray.filter(function(v) {return v === 0;}).length

如何通过 gulp 缩小包含 ES6 功能的代码?

How can I minify code containing ES6 features via gulp?

推荐答案

你可以利用 gulp-babel 这样……

You can leverage gulp-babel as such...

const gulp = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');

gulp.task('minify', () => {
  return gulp.src('src/**/*.js')
    .pipe(babel({
      presets: ['es2015']
    }))
    .pipe(uglify())
    // [...]
});

这将在管道的早期转译您的 es6,并在您缩小时生成广泛支持的纯"javascript.

This will transpile your es6 early in the pipeline and churn out as widely supported "plain" javascript by the time you minify.

可能需要注意 - 正如评论中所指出的 - 核心 babel 编译器作为 对等依赖 在这个插件中.如果核心库没有通过您的存储库中的另一个 dep 被拉下,请确保将其安装在您的最后.

May be important to note - as pointed out in comments - the core babel compiler ships as a peer dependency in this plugin. In case the core lib is not being pulled down via another dep in your repo, ensure this is installed on your end.

查看gulp-babel 作者指定 @babel/core (7.X).不过,稍旧的 babel-core (6.x) 也可以使用.我的猜测是作者(这两个项目都是一样的)正在重新组织他们的模块命名.无论哪种方式,两个 npm 安装端点都指向 https://github.com/babel/babel/tree/master/packages/babel-core,所以你可以使用以下任何一种......

Looking at the peer dependency in gulp-babel the author is specifying @babel/core (7.x). Though, the slightly older babel-core (6.x) will work as well. My guess is the author (who is the same for both projects) is in the midsts of reorganizing their module naming. Either way, both npm installation endpoints point to https://github.com/babel/babel/tree/master/packages/babel-core, so you'll be fine with either of the following...

npm install babel-core --save-dev

npm install @babel/core --save-dev

这篇关于如何使用 gulp-uglify 缩小 ES6 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)