Broadly: What are the best practices for organizing JS in the Rails pipeline?
Specifically: I have rapidly growing JS files that I'm fine with including in the overarching application.js
manifest and letting Sprockets uglify. However, each individual JS file is starting to become unwieldy, and I'd love to organize them so that I don't have to dig through hundreds of lines of utility functions to get to the meat of the code. I recognize that the goal is some kind of namespacing/modularity, but I don't know what the best practices are for combining this with the asset pipeline, especially Sprockets manifests.
Things I've already considered, and read thoroughly:
Yes, I've read the entire Rails guide on its asset pipeline. I know how Sprockets directives like
require
andrequire_tree
work; the issue is that I want to use those same directives like the equivalent of an ES6import
command so I can do something like// in, say, controller.js //= require 'utilities' ... more code ... // and in application.js, more confidently //= require 'controller'
but I get the feeling that's not how manifests should be used, or at least that it'll needlessly recompile layers of assets every time I change a single line in
utilities
. I've also considered requiring every file individually fromapplication.js
but that doesn't really give the modularity that seems appropriate.Gems like Paloma, CommonJS, or RequireJS. These seem like overkill, and seem meant to replace the pipeline rather than supplement it.
- "Modern" JS like ES6, Babel, or Browserify. (I admit I don't really understand the overlap in these projects yet, but I think I get the gist of their purposes.) Maybe eventually, since JS seems to be moving in that direction, but also seems like overkill.
- Gulp. In the same vein as previous, overkill and an unnecessary rewriting of the asset pipeline.
- Rails 5 and Sprockets 4. We're on Rails 4 right now, and I'm aware that Sprockets 4 has some ES6 built in, but I don't plan on upgrading until at least a while after Rails 5 is publicly released.
So what should I do? I think I need to bite the bullet and go with one of these, but I just can't figure out which makes the most sense. The project isn't especially reliant on JS, but there's enough of it that I want to organize it now, rather than later.
my_map_handler.js
is something I wrote, and huge, and I need submodularity for that file. Basically, I get how to organize my JS in views so they're not redundantly used (which I'd be okay with anyway)--it's more about code hygiene in the JS files I'm writing, combined with working in the pipeline's framework. – Raynor Kuang