When use js_compressor compile stimulus controller

I’m using hotwire-rails with assets pipeline, it works fine when js_compressor is off but raised an Uglifier::Error Unexpected token: keyword (extends) Error if js_compressor is on. I knew I can’t do concatenation or transpiling, but I should be able to do compression?

I knew this should be a Uglifier issue, but all Uglifier solution is suggested pass harmony: true, I already did it, event tried terser, still got the same error, I can’t figure it out.

# config/environments/development.rb
config.assets.js_compressor = Uglifier.new(harmony: true) 

bundle exec assets:precompile Exception backtrace

Uglifier::Error: Unexpected token: keyword (extends)
--
 1 import { Controller } from "stimulus"
 2
 => export default class extends Controller {
 4   connect() {
 5     this.element.textContent = "Hello World!"
 6   }
 7 };
/Users/fangxing/.rvm/gems/ruby-2.6.6/gems/uglifier-4.2.0/lib/uglifier.rb:291:in `parse_result'
/Users/fangxing/.rvm/gems/ruby-2.6.6/gems/uglifier-4.2.0/lib/uglifier.rb:221:in `run_uglifyjs'
/Users/fangxing/.rvm/gems/ruby-2.6.6/gems/uglifier-4.2.0/lib/uglifier.rb:166:in `compile'
/Users/fangxing/.rvm/gems/ruby-2.6.6/gems/sprockets-4.0.2/lib/sprockets/compressing.rb:84:in `block in js_compressor='

Solved by switching to terser-ruby, and add a class name after the class keyword

export default class Hello extends Controller
1 Like

@Xing_Fang thank you so much for posting your solution once you figured it out - I ran into the same issue and it doesn’t seem to come up often

Don’t need classname anymore, terser team fixed this bug Error when export default anonymous class with extends · Issue #924 · terser/terser · GitHub