Does grunt-contrib-uglify not parse "let" keywords?

Michael M picture Michael M · Aug 17, 2015 · Viewed 9.1k times · Source

I am getting an error,

'Unexpected token: name (bazz)'

when my grunt task is running uglify. The only thing I noticed on that line was that I was using the 'let' keyword instead of 'var', so I wasn't sure why that error was getting thrown.

I have an if else statement with let varName in each, i.e.:

function foo (bar) {
    if (condition) {
        let bazz = fn();
        //doSomething with bazz
        _.assign(bar, bazz);
    } else {
        let bazz = fn2();
        //doSomething different with bazz
        _.assign(bar, bazz);
    }
}

I could alter it by having a var bazz = {}; before the if else clause, but I wanted to avoid that because I'd have to assign bazz to fn() and fn2() regardless.

Wanted to know if anybody else has run into this and what they did to fix it. Thanks in advance!

Answer

Timo picture Timo · Aug 20, 2015

After researching this topic a little bit more I found out that grunt-contrib-uglify and also gulp-uglify has UglifyJS as a dependency and it does not support ES6 "Harmony" yet. Follow https://github.com/mishoo/UglifyJS2/issues/448 for updates.

You can also use tools like grunt-babel to compile your ES6 code to ES5.