2

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

Necrogoru
  • 108
  • 2
  • 9
  • 1
    I think you need to add `.es6` to the file extension for Sprockets to run the JS through babel. – max Oct 25 '17 at 17:42
  • @max I changed for a .es6 extension, but I got other error: `ExecJS::RuntimeError: SyntaxError: 'return' outside of function` – Necrogoru Oct 26 '17 at 14:11

2 Answers2

4

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)

adc
  • 771
  • 4
  • 12
1

The accepted answer worked till now but precompile with ES6 and uglifier compression stopped working after the release of new version execjs 2.8 after doing bundle update a few days back.

ERROR: JSON::ParserError: 439: unexpected token at '{"code":"/*! ...

To make it work again just had to fix the version of execjs to 2.7

Dharman
  • 30,962
  • 25
  • 85
  • 135
anoop0302
  • 21
  • 3