some es6 features are not understood by grunt uglify js?
Asked Answered
T

6

24

I used grunt-uglify in my project and I'm working with es6. for some es6 features, such as fat arrow function, it's throwing an error. I think the uglify don't understant that syntax. so do i have a way to fix this issue. I mean can i use anything to make uglify understand all the es6 syntaxes?

Tightlipped answered 18/7, 2016 at 6:5 Comment(0)
M
18

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).

Although not all the es6 features are supported in UglifyJs, the Harmony branch of UglifyJS2 seems to support arrow functions and other features.

You can read the discussion about it in the issue of github here. It seems there's not another option to wait (or create your own ES6 minifier).

Update

Now you have 2 options to minify ES6:

Matzo answered 18/7, 2016 at 7:4 Comment(4)
Any news about that ?Governor
Bump! 2019 now.. Still facing uglify not being able to accept "let".Materially
2021 now and it is partly implemented. There is no support for .? operator but I havent discovered other problems yet...Closegrained
Also * of generator functions seems not to be understood yet.Kinney
T
23

You can use the harmony branch of grunt-contrib-uglify.

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

works perfectly with es6. No additional settings for grunt are required.

Tessatessellate answered 3/7, 2017 at 13:48 Comment(2)
This was the only one that worked for me, but it did only after adding grunt.loadNpmTasks('grunt-contrib-uglify-es'); this javascript line in the Gruntfile, as stated in the official GitHub page.Birthstone
grunt-contrib-uglify.git#harmony is now deprecatedDoralia
M
18

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).

Although not all the es6 features are supported in UglifyJs, the Harmony branch of UglifyJS2 seems to support arrow functions and other features.

You can read the discussion about it in the issue of github here. It seems there's not another option to wait (or create your own ES6 minifier).

Update

Now you have 2 options to minify ES6:

Matzo answered 18/7, 2016 at 7:4 Comment(4)
Any news about that ?Governor
Bump! 2019 now.. Still facing uglify not being able to accept "let".Materially
2021 now and it is partly implemented. There is no support for .? operator but I havent discovered other problems yet...Closegrained
Also * of generator functions seems not to be understood yet.Kinney
D
4

A Grunt plugin ES6 parser, mangler/compressor and beautifier toolkit for ES6+ is now available:

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.

Doralia answered 13/12, 2019 at 8:38 Comment(0)
I
2

If the resulting code does not need to use ES6 syntax, it is also possible to transpile ES6 to ES5, before uglifying your code. This can for example be done with the Grunt task grunt-es6-transpiler.

Inhabited answered 31/1, 2017 at 10:11 Comment(0)
M
1

I just updated to [email protected] and it worked.

Mayers answered 7/8, 2017 at 14:48 Comment(0)
D
0

2024: I used grunt-contrib-uglify-es v3.3.0

from https://github.com/gruntjs/grunt-contrib-uglify/tree/harmony?tab=readme-ov-file#grunt-contrib-uglify-es-v330--

Installed it with install grunt-contrib-uglify-es --save-dev

And enabled inside Gruntfile with grunt.loadNpmTasks('grunt-contrib-uglify-es');

Let, const, destructuring - everything works.

Dauntless answered 8/8 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.