5

I'm trying to use ES6 syntax in my Javascript files, in a Rails project. I also need to minify the Javascript files (mine, and the ones included by libraries like rails-ujs) for performance reasons.

When I first generated the project and ran it locally, it worked fine. However, this is because the environment configurations are different for development vs. production. In the development configuration, there is no minification. In the production configuration, Uglifier is used to minify Javascript.

Once I tried to deploy to production, Uglifier and ExecJS started throwing up all kinds of syntax errors, such as not recognizing the const keyword or not recognizing the > in the => operator of arrow functions.

After spending a few hours searching for a fix, I resorted to simply disabling Uglifier so that I could deploy successfully. However, now that my site is in production and working, I would like to make this optimization work.

I'm familiar with Babel and Webpack, and I've used them in other projects (along with some custom scripts) to generate browser-ready ES5 code (for Internet Explorer) and browser-ready ES6 code (for every other browser). If I wasn't using Rails, this would be trivial for me to solve.

However, since I'm using Rails, I need to figure out how to do this in the context of the Rails Asset Pipeline. Surprisingly, there doesn't seem to be any standard way to do this, despite ES6 being 4 years old and supported by every modern browser. It's really weird that Microsoft Edge is way ahead of Rails on this one.

Here's a few suggestions that I've seen, and the problems with them.

Andrew Koster
  • 1,550
  • 1
  • 21
  • 31
  • What version of Rails are you using? Have you looked at https://github.com/rails/webpacker – Dylan Pinn Aug 21 '19 at 22:51
  • I was using Rails 5.2. Someone offsite told me that Rails 6 (released 5 days ago) comes with Webpacker built in, so I'm exploring that. – Andrew Koster Aug 21 '19 at 22:55
  • 1
    It looks like you should be able to use it with your version of Rails. That way you can run all of JS through Webpacker and styles through Sprockets or webpack if you wish. – Dylan Pinn Aug 21 '19 at 22:57
  • I've tried both `config.assets.js_compressor = Uglifier.new(harmony: true)` and the alternate syntax where you just do `{harmony: true}` in one place and `:uglifier` in the other place. Neither syntax had any visible effect. – Andrew Koster Aug 21 '19 at 23:47

0 Answers0