Posted 5 months ago. Visible to the public. Repeats.

Using ES6 without a transpiler

If your app does not need to support IE11, you can use most ES6 features without a build step. Just deliver your plain JavaScript without transpilation through Babel or TypeScript, and modern browsers will run them natively.

Features supported by all modern browsers include:

  • fat arrow functions (() => { expr })
  • let / const
  • class
  • async / await
  • Promises
  • Generators
  • Symbols
  • Rest arguments (...args)
  • Destructuring

You won't be able to use import and export, or use npm modules.

See this ES6 compatibility matrix for details.

ES6 and Uglifier

You are probably going to run into an issue if you try to deploy a rails app which uses the assets pipeline (sprockets) and Uglifier to minify your JavaScript.

Copy
The asset "application.js" is not present in the asset pipeline. [...]

The error would be an ActionView::Template::Error with the message above, because rails fails at precompiling your ES6 javascripts.

You can check, if you ran into that issue by running bundle exec rails assets:precompile eiher on the server or in your development environment (don't forget to run bundle exec assets:clobber after you precompiled the assets on your local machine).

If the process fails due to an error similar to the one below, you can simply replace

Copy
config.assets.js_compressor = :uglifier

with

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

in config/environments/production.rb and you should be good. :harmony => true simply enables the ES6 mode for Uglifier.

Copy
Uglifier::Error: Unexpected token operator «=», expected punc «,». To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Henning Koch
Last edit:
3 months ago
by Jakob Scholz
Keywords:
table
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more