Created
April 9, 2011 01:12
-
-
Save eric1234/911003 to your computer and use it in GitHub Desktop.
Using Sprockets 2 in Rails 3.0.x with CoffeeScript & SASS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UPDATE: Please see some of the forks for an updated version of this guide. I | |
myself have moved onto the Rails 3.1 betas to get the asset pipeline. But if | |
you want to stay on stable there are other folks who are keeping this guide | |
relevant despite the changes constantly occurring on Sprockets 2. The comments | |
on this gist will lead you to the right forks. :) | |
Some brief instructions on how to use Sprocket 2 in Rails to get CoffeeScript | |
powered JS and SASS powered CSS with YUI compression all via the magic of rack. | |
This stuff will be native in Rails 3.1 and the layout of the files on the | |
filesystem will be different but this guide will get you working with it | |
while we wait for all that to finalize. | |
Ignore the number prefixes on each file. This is just to ensure proper order in the Gist. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'coffee-script' | |
gem 'yui-compressor', :require => 'yui/compressor' | |
gem 'sass' | |
gem 'json' # sprocket dependency for Ruby 1.8 only | |
gem 'sprockets', :git => 'git://github.com/sstephenson/sprockets.git' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Config a Sprockets::Environment to mount as a Rack end-point. I like to use a subclass | |
# as it allows the config to be easily reusable. Since I use the same instance for | |
# all mount points I make it a singleton class. I just add this as an initializer to my | |
# project since it is really just configuration. | |
class AssetServer < Sprockets::Environment | |
include Singleton | |
def initialize | |
super Rails.public_path | |
paths << 'javascripts' << 'stylesheets' | |
if Rails.env.production? | |
self.js_compressor = YUI::JavaScriptCompressor.new :munge => true, :optimize => true | |
self.css_compressor = YUI::CssCompressor.new | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mount the rack end-point for JavaScript and CSS. | |
MyApp::Application.routes.draw do | |
mount AssetServer.instance => 'javascripts' | |
mount AssetServer.instance => 'stylesheets' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Put this in your public/javascripts directory and call /javascripts/application.js in your browser | |
alert 'hello world' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Put this in your public/stylesheets directory and call /stylesheets/application.css in your browser | |
body {margin: 2px + 5px} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sprockets 2 has a lot more under the hood but this gets you started. | |
A few things not covered: | |
1. Anything supported by Tilt can be used as a template engine | |
(not just Sass and CoffeeScript). | |
2. Although Sass has native abilities to include other files, Sprockets 2 | |
gives the ability to all formats through special comments like: | |
// =require "foo" | |
It's special commands can be fairly powerful (like requiring an entire | |
directory or tree). NOTE: Use the comment character relevant for the | |
language. So coffescript should be: | |
# =require 'foo.js' | |
Then you can create 'foo.js.coffee' and when served it will be as one | |
file. | |
3. Sprockets 2 has the ability to pre-compile the assets for maximum speed. | |
Also useful when the deployment environment doesn't support a template | |
language (like CoffeeScript). |
@MohitSharma - Things have changed a lot in Sprockets 2 since I wrote this guide. You will likely need to change some things. Also some forks of this guide were made to accomidate changes in Sprockets 2. Unsure if those are still maintained or if their editor has moved onto Rails 3.1+. It should be possible to use Sprockets 2 in a Rails 3.0.11 app (since it is just rack based). You may just have to dig a bit.
Thanks @eric1234
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this is compatible with rails 3.0.11