Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't precompile production assets when using ES6

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.

like image 1000
max pleaner Avatar asked Jan 08 '17 10:01

max pleaner


2 Answers

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.

like image 126
max pleaner Avatar answered Nov 01 '22 08:11

max pleaner


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

like image 11
Derek Fan Avatar answered Nov 01 '22 07:11

Derek Fan