3 ms·
As the creator of both Sprockets, which underlies Rails 3.1's asset pipeline, and Stitch, which the poster of this article ported beautifully from Node.js to Ru
by sstephenson 15y ago
As the creator of both Sprockets, which underlies Rails 3.1's asset pipeline, and Stitch, which the poster of this article ported beautifully from Node.js to Ruby, I feel obligated to reply here :)
While CommonJS modules are undeniably elegant, they carry with them a high conceptual overhead. It would be unreasonable to expect Rails developers to rewrite all their existing JavaScript in order to take advantage of the asset pipeline. Most people do not want to manually import every dependency into every source file (and in fact Rails goes to great lengths to make use of Ruby's "require" method largely unnecessary).
Sprockets solves this with the "require_tree" directive that automatically pulls in all source files in a given directory. If you've been keeping your JavaScript source files in public/javascripts, it's trivial to upgrade your app to 3.1 and take advantage of the asset pipeline — just move your source files into app/assets/javascripts. Then when you do need more specific control over source file ordering, the "require" and "include" directives are there for you. Combine this with the implicit closure around each CoffeeScript source file and Sprockets gives you 80% of the benefit of CommonJS modules with 20% of the complexity.
Stitch (and CommonJS in general) does not address the problem of CSS preprocessing or concatenation, and does not provide a mechanism for managing other assets like images and sounds. Sprockets supports Sass and SCSS, and provides a global load path which lets you depend on JS, CSS and images from a single logical package — even a Ruby gem — with essentially no configuration.
I hope this clarifies why we chose to go with the Sprockets approach rather than CommonJS for Rails 3.1.
- rasmusrn 15y agoSprockets can be used for JS, CSS, images, and so on, because it is so simple: it is "just" concatenation. I understand and agree on the benefit/complexity argument. But the fact that this method works universally, is also the reason it cannot accomodate individual needs for each "asset type" (css, js): * For Javascript we cannot use CommonJS (or something similar) * For SCSS we cannot use variables, mixins For me, this means a lot. Traditionally, Rails has been giving me the best web environment experience I could imagine. With Rails 3.1, I can't help thinking: Why did they leave some of the best parts out? I know I actually CAN use SCSS variables and mixins - I just have to use SCSS' @import rather than Sprockets own =require. But that leads to my stance in this matter: If we need to bypass Sprockets in order to get the cool features, then why use it in the first place? We should use a system designed specifically for JS to management our JS, and a system specifically for CSS to manage CSS. Stitch and SASS comes to mind of course. While I understand the benefits of the "Sprockets way", I'd like to see Rails use specific tools for specific asset types in order to give developers easy access to all of the available features instead of just 80% of them.