Extending asset pipeline with custom preprocessors
If you’ve read the Rails 3.1 asset pipeline docs, you’re probably aware that you can add preprocessors to your asset files by appending extra file extensions. For example, to write your JS files in CoffeeScript you need to add the suffix .coffee
, and if you also want to pass something from Rails to those files, like paths to image files, you also need to add the .erb
suffix. All the extensions are added together, so you end up with e.g. profile.js.coffee.erb
(it’s simpler with stylesheets, because by adding a Sass preprocessor you get a bunch of asset path helpers for free).
What the docs don’t tell you is that Sprockets can also be configured to include preprocessors implicitly based on a content type.