Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.2 Asset Pipeline and RequireJS

I'm going to start a rich client-side web application with Ruby on Rails 3.2. I intended to use RequireJS, but it seems to collide with the Asset Pipeline. As far as I know, what the latter basically does is concatenating dependent assets, minifiying and compressing them (correct me if I'm wrong), which does not seem very compatible with loading JavaScript files asychronously.

At a first glance, the Asset Pipeline seems to have much better performance. However, RequireJS lets you organize the JavaScript code in modules easy to reuse and mange its dependencies.

Is there any way to combine both of them? In case there isn't, which one would you choose?

like image 329
davids Avatar asked Feb 15 '13 17:02

davids


2 Answers

You might want to have a look at this gem https://github.com/jwhitley/requirejs-rails/

Seems to be doing what you want - which is to use requirejs for loading client side while still taking advantage of some of the asset pipeline.

I would be tempted to suggest that I guess that in most cases the asset pipeline would be much faster as it loads a single minified js resource. The dependency management isn't as good though, so it would very much depend on the app.

like image 185
jimcode Avatar answered Oct 02 '22 18:10

jimcode


I would suggest to download the RequireJS library and toss it to vendor/assets/javascripts. Then in your application.js file :

//= require require

(funny , yes?) , and that should be enough.

This is the easiest way to combine the asset-pipeline and a modular js library . I am not aware of any additional settings this particular library needs , but you can take a look at this Railscast , which describes something similar.

like image 23
R Milushev Avatar answered Oct 02 '22 20:10

R Milushev