Skip to content

Instantly share code, notes, and snippets.

@hilios
Forked from jeffreyiacono/Rakefile
Created August 2, 2012 18:26
Show Gist options
  • Save hilios/3239413 to your computer and use it in GitHub Desktop.
Save hilios/3239413 to your computer and use it in GitHub Desktop.
rake task for precompiling assets using sprockets within a sinatra app + view helpers
require 'sinatra/base'
require 'sinatra/flash'
require 'rack/csrf'
require 'haml'
require 'sass'
require 'compass'
class Application < Sinatra::Application
register Sinatra::Flash
set :root, File.dirname(__FILE__)
set :sprockets, (Sprockets::Environment.new(root) { |env| env.logger = Logger.new(STDOUT) })
set :assets_prefix, 'compiled'
set :assets_path, File.join(root, 'public', assets_prefix)
set :compass_gem_root, Gem.loaded_specs['compass'].full_gem_path
configure do
sprockets.append_path File.join(root, 'assets', 'stylesheets')
sprockets.append_path File.join(compass_gem_root, 'frameworks', 'compass', 'stylesheets')
sprockets.append_path File.join(compass_gem_root, 'frameworks', 'blueprint', 'stylesheets')
sprockets.append_path File.join('assets', 'javascripts')
# ... more config
end
# we are deploying to heroku, which does not have a JVM, which YUI needs, so let's
# only require and config the compressors / minifiers for dev env
configure :development do
require 'yui/compressor'
require 'uglifier'
sprockets.css_compressor = YUI::CssCompressor.new
sprockets.js_compressor = Uglifier.new(mangle: true)
end
# ... more setup
end
require 'sinatra/base'
module Sinatra
module AssetHelpers
# +options+ can pass "force_production" to trump env setting
# if digest is used in the future, will need to incorporate this into these two helpers
def stylesheets_tag(options = {})
if production? || options[:force_production]
'<link rel="stylesheet" href="/compiled/css/application.min.css" type="text/css" media="all" />'
else
'<link rel="stylesheet" href="/assets/application.css" type="text/css" media="all" />'
end
end
def javascripts_tag(options = {})
if production? || options[:force_production]
'<script src="/compiled/js/application.min.js" type="text/javascript"></script>'
else
'<script src="/assets/application.js" type="text/javascript"></script>'
end
end
helpers AssetHelpers
end
end
require 'rubygems'
require 'bundler'
Bundler.require
require './application'
namespace :assets do
desc 'compile assets'
task :compile => [:compile_js, :compile_css] do
end
desc 'compile javascript assets'
task :compile_js do
sprockets = Application.settings.sprockets
asset = sprockets['application.js']
outpath = File.join(Application.settings.assets_path, 'js')
outfile = Pathname.new(outpath).join('application.min.js') # may want to use the digest in the future?
FileUtils.mkdir_p outfile.dirname
asset.write_to(outfile)
asset.write_to("#{outfile}.gz")
puts "successfully compiled js assets"
end
desc 'compile css assets'
task :compile_css do
sprockets = Application.settings.sprockets
asset = sprockets['application.css']
outpath = File.join(Application.settings.assets_path, 'css')
outfile = Pathname.new(outpath).join('application.min.css') # may want to use the digest in the future?
FileUtils.mkdir_p outfile.dirname
asset.write_to(outfile)
asset.write_to("#{outfile}.gz")
puts "successfully compiled css assets"
end
# todo: add :clean_all, :clean_css, :clean_js tasks, invoke before writing new file(s)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment