Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating ActionCable with Webpacker in Rails 5.1

I am upgrading my app from Rails 5.0 to 5.1, and there is one thing I can't find being mentioned anywhere - usage of Webpacker to process ActionCable JavaScript files.

I realise that Webpacker and the asset pipeline are supposed to work side by side, but now that we have Webpack build into Rails it would be a waste not to use it for files located in assets directory (which is where ActionCable lives), yet I can't find any information on how to do that.

I'm specifically interested in Babel, so I could use ES6. Any ideas?

like image 317
Aigars Dzerviniks Avatar asked May 31 '17 14:05

Aigars Dzerviniks


1 Answers

The problem here is that you need to replace the ActionCable implementation of rails, which is written for sprockets with something that is written in module syntax, to make proper use of webpacker. Specifically this line in your cable.js require action_cable Everything else should be easily portable. There seem to be a couple of implementations for ActionCable and modules around: https://www.npmjs.com/search?q=keywords:ActionCable

Specifically this looks exactly what you are searching for: https://www.npmjs.com/package/actioncable-modules

There seems to be some movement on the side of Rails to use Webpacker as a complete sprocket replacement in Rails 6, so it might be worth waiting, instead of creating something that builds on third party modules.

like image 55
smallbutton Avatar answered Oct 16 '22 05:10

smallbutton