EDIT: Updated Answer 2
The grunt-contrib-uglify-es version has now been published to npm. It’s the same harmony branch on GitHub that I mentioned in my previous updated answer below. However, it can be installed directly via npm running the following command:
npm i -D grunt-contrib-uglify-es
EDIT: Updated Answer
Since answering this question a harmony branch of grunt-contrib-uglify
has been made available. This version handles the ES6 Template Literals and other ES6 syntax features.
Installing the harmony branch of grunt-contrib-uglify
:
Firstly uninstall the current version of grunt-contrib-uglify
by running the following command via your CLI:
npm un -D grunt-contrib-uglify
Next install the harmony branch by running:
npm i -D https://github.com/gruntjs/grunt-contrib-uglify.git#harmony
(Note the harmony
branch in not available via the npm registry so we install it directly via GitHub)
Your package.json
will now include the following snippet:
...
"devDependencies": {
...
"grunt-contrib-uglify-es": "git+https://github.com/gruntjs/grunt-contrib-uglify.git#harmony"
...
}
...
Run your uglify
Task as per normal, and you will not encounter the error message.
Original Answer
Your string utilizes the ES6 Template Literal syntax and this feature is not supported by uglify-js (the JavaScript parser/mangler/compressor used by grunt-contrib-uglify
).
See the answer to this post for further info.
If your project requires the use of ES6 features, then consider transpiling your code, using a JavaScript transpiler such as Babel, then uglifying the resultant ES5 code using grunt-contrib-uglify
.
Alternatively, if ES6 Template Literals are the only part of the ES6 syntax that your project uses, there's always the option of reverting to the older syntax instead for your strings. E.g.
var html = '<div class=\'list-item\'' +
'<span>ABC</span>' +
'</div>';
The example code shown above will be handled successfully by grunt-contrib-uglify
.