calin24
calin24

Reputation: 981

Unexpected token: on compressing js file

I have tried compressing a JavaScript file, with both http://jscompress.com and "uglifyjs" (uglifyjs main.js --compress --mangle > main.min.js), but I get the following error:

File main.js: Unexpected token: name «sidebarSubcategory», expected: punc «;» (line: 17, col: 8)

In the main.js I have the following function:

function openSubcategory(el, id) {
  let sidebarSubcategory = document.getElementById("sidebar-subcategory-" + id);
  if (el.innerHTML === '+') {
    el.innerHTML = '-';
    sidebarSubcategory.setAttribute('style', 'display: block;');
  } else {
    el.innerHTML = '+';
    sidebarSubcategory.setAttribute('style', 'display: none;');
  }
}

I don't get it.

Upvotes: 0

Views: 261

Answers (1)

Kevin.a
Kevin.a

Reputation: 4286

The compression tool you're using doesnt support the new syntax. Changing let to var or checking the "Use ecmascript 2019" checkbox will solve your issue.

And as for uglifyjs, you can use this tool its from the same developers of uglifyjs but for es6 syntax :

https://www.npmjs.com/package/uglify-es

Upvotes: 2

Related Questions