Search code examples
lambdagruntjsgrunt-contrib-uglifyarrow-functions

grunt uglify does not accept lambda or arrow functions


I am running grunt-contrib-uglify v 2.15.1. It fails when I use a lambda or arrow function with:

Running "uglify:admin" (uglify) task
{ message: 'SyntaxError: Unexpected token: operator (>)',
  filename: 'admin-app-ng.js',
  line: 4927,
  col: 50,
  pos: 216049,
  stack: 'Error\n    at new JS_Parse_Error (eval at <anonymous>

If I remove the lambda then the uglify functions. Is there a way to use uglify when using lambdas/arrow functions?

This is the arrow function:

myService.getActions($scope.myVar, (type) => (type === 'myType')?true:false);

Solution

  • Arrow functions are an ES6 feature and there is no support in Uglify for ES6, so your ES6 will need to be transformed to ES5 before it can be uglified.

    As you are using grunt, you can add another task - before grunt-contrib-uglify - to transform your JavaScript source from ES6 to ES5. Babel can be used to do this and there is a Babel grunt plugin.