Overall: What are the most effective strategies for structuring JavaScript within the Rails pipeline?
Specifically: My JS files are growing rapidly and while I'm okay with including them in the main application.js
bundle and using Sprockets to minify, each individual file is becoming difficult to manage. I would like to organize them in a way that avoids sifting through numerous lines of utility functions to find the core code. I understand the importance of namespacing/modularity, but I'm unsure of the best practices for implementing this within the asset pipeline, particularly in Sprockets manifests.
Here are some considerations I have already explored:
Yes, I have thoroughly reviewed the Rails asset pipeline guide. I am familiar with how Sprockets directives such as
require
andrequire_tree
function; however, I want to utilize these directives similar to an ES6import
command for better organization, like so:// Example in controller.js //= require 'utilities' ... more code ... // And confidently in application.js //= require 'controller'
I feel that this may not align with the intended use of manifests, potentially causing unnecessary recompilation of assets whenever a single line in
utilities
changes. I also considered individually requiring every file fromapplication.js
, but this does not provide the modularity I seek.Frameworks like Paloma, CommonJS, or RequireJS. These appear to be extensive solutions that aim to replace rather than complement the pipeline.
- "Modern" JS tools like ES6, Babel, or Browserify. While I am still gaining an understanding of their overlaps, they seem excessive at this stage.
- Gulp. Similarly, it seems like overkill and unnecessary duplication of effort compared to the asset pipeline.
- Rails 5 and Sprockets 4. We are currently using Rails 4 and although I know Sprockets 4 supports ES6, I do not plan to upgrade until after Rails 5 is officially launched.
What should be my next step? It appears that I need to make a decision, but I am struggling to determine which approach makes the most sense. While the project does not heavily rely on JS, there is enough present that I believe organizing it now is preferable to doing so later on.