Unexpected token: operator (=) on ES6 class when using Uglifier

665 Views Asked by At

I have a JavaScript class in my Rails application that uses Uglifier inside Sprockets to compress the JS (under the hood I believe it uses: https://github.com/mishoo/UglifyJS)

class FormValidation {
    connect() {
        this.element.setAttribute('novalidate', true)
        this.element.addEventListener('blur', this.onBlur, true)
    }
    onBlur = (event) => {
        this.validateField(event.target)
    }
... rest of code hidden for benefit of question.

And UglifyJS is disliking the = in my onBlur named method... with Parse error: Unexpected token: operator (=).

Harmony is set to true to support ES6... but still getting this error... How can I get around this? As I have had to remove the compression so I can use my class in my code...

1

There are 1 best solutions below

4
RodCardenas On

Without looking at your tooling (babel, packing, etc.), it's hard to say how to get it to build right. but you can always just do the following:

class FormValidation {
  connect() {
    this.element.setAttribute('novalidate', true)
    this.element.addEventListener('blur', this.onBlur, true)
  }
}
FormValidation.prototype.onBlur = function(event) {
 this.validateField(event.target)
}