25

Question

If you've used RequireJS with a Rails 3 (esp 3.1) app, how is that working for you? Any configuration tricks or other gotchas that I need to watch out for?

Background

I'm contemplating using RequireJS over the Sprockets-based Asset Pipeline in Rails 3.1, specifically for JavaScript code. I have two motivators for this choice:

  • I want to leverage RequireJS' module management for my JS client-side code.
  • I'd like a precompilation system that can follow my JS library code into other contexts. To my surprise, the Asset Pipeline precompiler is a baked-in part of Rails, not a part of Sprockets itself.

All feedback appreciated, thanks!

John Whitley
  • 2,030
  • 18
  • 22
  • 1
    Can you tell more about why you would want RequireJS' module management over Sprockets' one? I'm thinking about doing the same thing for our Rails app — replacing Sprockets with RequireJS. – kangax Dec 04 '11 at 17:08
  • 4
    @kangax, Sprockets doesn't really provide module management. It just conglomerates a bunch of JavaScript into a built file via Sprockets' directives. As it happens, I've published the [requirejs-rails](https://github.com/jwhitley/requirejs-rails/) gem to integrate RequireJS into Rails, leveraging Sprockets for CoffeeScript conversions. Nearing 0.5.0 release with precompliation support. – John Whitley Dec 05 '11 at 22:54

1 Answers1

48

For posterity, here's where I've come to on this question:

  • RequireJS provides an implementation of the Asynchronous Module Definition API. RequireJS' Why AMD? page lays out the case as to why you'd want to use this.

  • Sprockets and the Rails 3 Asset Pipeline allow for simple structuring of JavaScript/CoffeeScript code, but don't provide any true module support. For example, there's no namespace control whatsoever in Sprockets.

  • jQuery (as of 1.7), Underscore, Dojo and numerous other major libraries have implemented AMD support. Several other major JS libraries seem to have AMD support on the near-term horizion (e.g. Backbone.js).

It's certainly possible to create a Rails app that integrates RequireJS. To simplify that process, I've created the requirejs-rails gem on github, with straightforward configuration and Asset Pipeline-aware precompilation for AMD-based code via r.js. The current release is available via:

gem install requirejs-rails

John Whitley
  • 2,030
  • 18
  • 22
  • 1
    Also for posterity, Underscore no longer supports AMD. However, Lodash, a drop-in replacement for Underscore, does. http://lodash.com – leppert May 20 '12 at 19:30
  • 2
    Thanks for this gem. Is there code Rails app I can see that shows how it's all set up, preferably with Backbone. I've read the docs, but I'm new to require.js and not that experienced/skilled with javascript, so I get along much better with a working example. – Leahcim Jan 26 '13 at 22:24
  • Here is an example Rails 4 application which integrates requirejs-rails: https://github.com/pboling/require-rails-example – rubiii Sep 25 '13 at 18:45
  • 1
    @leppert You can always use shim configuration and export "_". – Chad Johnson Oct 14 '13 at 21:42