Entries tagged ‹ rails ›
Rails 3.x Assets Pipeline Tip - Any file can be a manifest
posted 4 months agoI’ve just started playing with Ruby and Rails, and one of the cool things I’ve been tinkering with is the new assets pipeline. It does a few neat things:
- Compiles multiple JS/CSS files into a single file and minifies it
- Evaluates things like Coffeescript or SASS/LESS
- Allows you to logically store your assets whether they be images, JS or CSS files where they belong - be it with a vendor, plugin, or right in your app, but access them all off the root of /assets.
- and more
The RailsGuide does a good job of going over the basics.
However, one thing that concerned me was that there didn’t seem to be a way to group things like JS files into separate compiled files. A good reason to want to do this, for example, would be if you had an admin section of your site with its own JS that you don’t want to pointlessly load on the front end.
The documentation lead me to believe that the application.js file was the singular manifest file. However, after poking through the sprockets code, I’ve learned that ANY file can be a manifest file, so you can pop an admin.js file in your app/assets/javascripts folder with
//= require_tree ./admin
This tells sprockets to compile all the js files in the admin directory into the admin.js file at the root. Then you can include that in your application via lazy loading or via
<%= javascript_include_tag “admin” %>
in your admin section layout.