Rails 6 upgrading from turbolinks to turbo. How to require turbo-rails in application.js?

Viewed 1300

I'm working on a legacy application, recently upgrades to Rails 6.0.4. The app uses sprockets, NOT webpacker.

Now I'm trying to move off of turbolinks and onto turbo-rails. The js files in this app all use sprockets require directives such as:

//= require jquery3
//= require turbolinks

I can't seem to figure out how to apply this to turbo-rails. I've installed the gem and removed turbolinks, but all of the documentation I've found has things like import "@hotwired/turbo-rails".

I'm not very knowledgeable about front end stuff, but I believe this is a different (ES6?) syntax. This app is currently on sprockets 3.7.2 and I don't think sprockets supports ES6 until version 4. I'd like to avoid this upgrade touching more dependencies if possible...

is it possible to //= require turbo in the same way I currently have //= require turbolinks?'

1 Answers

In order to use turbo-rails, you have to have the asset pipeline set up to transpile ES6. This isn't possible with my version of sprockets (3.7.2), so I have two options:

  • upgrade to sprockets 4
  • switch to using webpacker for the Javascript portion of the asset pipeline

With one of these in place, I should be able to use import "@hotwired/turbo-rails".

Related