Created
November 8, 2011 05:35
-
-
Save jayzes/1347097 to your computer and use it in GitHub Desktop.
Wiring up Rack/Sprockets
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
require 'sprockets' | |
require 'sprockets-sass' | |
require 'compass' | |
require File.join(File.dirname(__FILE__), 'static_compiler') | |
Encoding.default_external = "UTF-8" | |
Sass::Engine::DEFAULT_OPTIONS[:load_paths].tap do |paths| | |
paths.push *Compass.sass_engine_options[:load_paths] | |
end | |
$sprockets_environment = Sprockets::Environment.new | |
$sprockets_environment.append_path 'javascripts' | |
$sprockets_environment.append_path 'stylesheets' | |
$sprockets_environment.append_path 'images' | |
$sprockets_environment.append_path 'spec/javascripts' | |
$sprockets_environment.append_path 'vendor/jasmine-1.1.0' | |
$sprockets_environment.append_path 'vendor/sinon-1.2.0' |
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
require 'rubygems' | |
require 'bundler/setup' | |
require File.join(File.dirname(__FILE__), 'lib', 'sprockets', 'rack') | |
run SprocketsApp |
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
source :rubygems | |
gem 'rake' | |
gem 'rack' | |
gem 'sprockets', '~> 2.0' | |
gem 'sprockets-sass', '0.3.0' | |
gem 'compass', '~> 0.12.alpha' | |
gem 'coffee-script' | |
gem 'tilt', :git => 'git://github.com/rtomayko/tilt.git' | |
gem 'ejs' | |
gem 'eco' |
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
require 'rack' | |
require File.join(File.dirname(__FILE__), 'config') | |
SprocketsApp = Rack::Builder.new do | |
# This is a workaround since I can't get Sprockets to serve subdirectories properly | |
map '/assets/admin' do | |
run Rack::Directory.new('images/admin') | |
end | |
map '/assets/fonts' do | |
run Rack::Directory.new('images/fonts') | |
end | |
map '/assets' do | |
run $sprockets_environment | |
end | |
map '/spec' do | |
run Rack::Directory.new('spec/javascripts') | |
end | |
map "/images" do | |
run Rack::Directory.new('images') | |
end | |
map "/" do | |
run Rack::Directory.new('www') | |
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
require 'sprockets' | |
require 'fileutils' | |
module Sprockets | |
class StaticCompiler | |
attr_accessor :env, :target, :digest | |
def initialize(env, target, options = {}) | |
@env = env | |
@target = target | |
@digest = options.key?(:digest) ? options.delete(:digest) : true | |
end | |
def precompile(paths) | |
manifest = {} | |
env.each_logical_path do |logical_path| | |
next unless precompile_path?(logical_path, paths) | |
if asset = env.find_asset(logical_path) | |
manifest[logical_path] = compile(asset) | |
end | |
end | |
manifest | |
end | |
def compile(asset) | |
asset_path = digest_asset(asset) | |
filename = File.join(target, asset_path) | |
FileUtils.mkdir_p File.dirname(filename) | |
asset.write_to(filename) | |
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/ | |
asset_path | |
end | |
def precompile_path?(logical_path, paths) | |
paths.each do |path| | |
if path.is_a?(Regexp) | |
return true if path.match(logical_path) | |
elsif path.is_a?(Proc) | |
return true if path.call(logical_path) | |
else | |
return true if File.fnmatch(path.to_s, logical_path) | |
end | |
end | |
false | |
end | |
def digest_asset(asset) | |
digest ? asset.digest_path : asset.logical_path | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment