Dependency management with CoffeeScript and Sprockets

Sprockets makes it really damn easy in a few lines of ruby to concatenate and serve your coffee script files. It supports multiple template engines ERB, JavaScript, CSS, SCSS, etc you can even add a dash of ERB <%= %> to your coffeescript source files by naming your file and sprockets will automagically compile it down with ERB then CoffeeScript and concatenate  it with other dependencies!

For the project I was working on I couldn't depend on Node.js for dependency management since I was creating a front-end browser game. At first I decided to implement my own hacked up solution in Ruby to substitute and concatenate coffee script source files... it wasn't pretty :( After moving to sprockets I was able to refactor a few hundred lines into three :)

For example, suppose you have a file that contains sprockets `require` syntax called

#= require "src/one"
#= require "src/two"
#= require "src/three"
MyApp ?= {}

Then all you need to do is create a Sprockets::Environment instance and add the source path

require 'sprockets'
env =
env.paths << "src/coffee"
open("example.js", "w").write( env[""] )

Tada! It just works :)

Go Back