Can't precompile production assets when using ES6

2.6k views Asked by At

I have some code using template strings which works in development, but the push to Heroku fails with this error:

 ExecJS::RuntimeError: SyntaxError: Unexpected character '`'

Running bundle exec rake assets:precompile RAILS_ENV=production shows the same thing.

The code is something like this:

`1 + 1 is ${1 + 1}`

I wonder if the Heroku Node version is too low to support this. I'm not customizing this at all. Just pushing a Rails 4 app with the default configuration.

2

There are 2 answers

1
max pleaner On BEST ANSWER

I eventually found this thread: https://github.com/browserify-rails/browserify-rails/issues/137

where I found the recommendation to comment out the line:

config.assets.js_compressor = :uglifier

The problem is that the 'uglifier' (minifier) was not working correctly with the ES6 syntax.

As the thread mentions, using this fix means that the scripts will no longer be minified, so I'd still be curious if there's a better solution.

0
Derek Fan On

Uglifier now has experimental ES6 support, but you have to config it first:

in config/environments/production.rb

replace

config.assets.js_compressor = :uglifier

with

config.assets.js_compressor = Uglifier.new(harmony: true)


However, ES6 is not extensively tested. More stable alternatives for working with ES6 code is to first transpile to ES5 with e.g. babel-transpiler or using Closure Compiler to directly minify ES6 code.

Document: https://github.com/lautis/uglifier#user-content-es6--es2015--harmony-mode