Search code examples
ruby-on-railsecmascript-6uglifier

Rails 5 don't precompile JS files with ES6 code in production


I am deploying an app with some javascript that contain ES6 code.

When I did run bundle exec rake assets:precompile RAILS_ENV=production, I got:

ExecJS::RuntimeError: SyntaxError: Unexpected token: punc ())

This is because prueba.js has the following:

var greetings = () => {
  let saludo = 'tio';
  console.log('saludo' + saludo)
};
greetings();

And Uglify don't recognize ES6. For fix this, I had installed and setting the gems:

gem 'sprockets-es6', '~> 0.9.2'
gem 'babel-transpiler', '~> 0.7.0'

But isn't working,

In application.rb, I have:

require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
require 'sprockets/es6'

Note: If i run rake assets:precompile without RAILS_ENV=production, I don't get errors


Solution

  • When using ES6, if you use uglifier (3.2.0) you can change the file production.rb

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