简体   繁体   English

grunt uglify js 无法理解某些 es6 功能?

[英]some es6 features are not understood by grunt uglify js?

I used grunt-uglify in my project and I'm working with es6.我在我的项目中使用了 grunt-uglify,我正在使用 es6。 for some es6 features, such as fat arrow function, it's throwing an error.对于某些 es6 功能,例如粗箭头函数,它会引发错误。 I think the uglify don't understant that syntax.我认为 uglify 不理解该语法。 so do i have a way to fix this issue.所以我有办法解决这个问题吗? I mean can i use anything to make uglify understand all the es6 syntaxes?我的意思是我可以使用任何东西让 uglify 理解所有 es6 语法吗?

You can use the harmony branch of grunt-contrib-uglify .您可以使用grunt-contrib-uglifyharmony分支。

npm install git://github.com/gruntjs/grunt-contrib-uglify.git#harmony --save-dev

works perfectly with es6.与es6完美配合。 No additional settings for grunt are required.不需要额外的 grunt 设置。

Officially there's no tool that minifies ES6 yet (Other tools like Google Closure compiler supports ES6 but it transpile it into "traditional js" and then it's minified).官方上还没有缩小 ES6 的工具(其他工具,如 Google Closure 编译器支持 ES6,但它会将其转换为“传统 js”,然后进行缩小)。

Although not all the es6 features are supported in UglifyJs, the Harmony branch of UglifyJS2 seems to support arrow functions and other features.虽然不是所有的 es6 特性在 UglifyJs 中都支持,但 UglifyJS2 的 Harmony 分支似乎支持箭头函数和其他特性。

You can read the discussion about it in the issue of github here . 您可以在此处阅读 github 问题中有关它的讨论 It seems there's not another option to wait (or create your own ES6 minifier).似乎没有其他选择可以等待(或创建自己的 ES6 压缩器)。

Update更新

Now you have 2 options to minify ES6:现在你有两个选项来缩小 ES6:

A Grunt plugin ES6 parser, mangler/compressor and beautifier toolkit for ES6+ is now available:用于 ES6+Grunt 插件 ES6 解析器、编辑器/压缩器和美化工具包现已可用:

https://github.com/adascal/grunt-terser https://github.com/adascal/grunt-terser

Great job, it just works as expected.很棒的工作,它按预期工作。

The harmony branch of grunt-contrib-uglify is deprecated , it's not a sustainable solution. grunt-contrib-uglifyharmony分支弃用,它不是一个可持续的解决方案。

If the resulting code does not need to use ES6 syntax, it is also possible to transpile ES6 to ES5, before uglifying your code.如果生成的代码不需要使用 ES6 语法,也可以在丑化代码之前将 ES6 转换为 ES5。 This can for example be done with the Grunt task grunt-es6-transpiler .例如,这可以通过 Grunt 任务grunt-es6-transpiler 来完成

我刚刚更新到uglify-js@2.8.29并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM