Skip to content

Instantly share code, notes, and snippets.

@EtienneLem
Created October 18, 2012 15:27
Show Gist options
  • Save EtienneLem/3912564 to your computer and use it in GitHub Desktop.
Save EtienneLem/3912564 to your computer and use it in GitHub Desktop.
Sinatra (javascript|stylesheet)_include_tag helper w/ Rakefile tasks
# Assets tag helper
# Uses sprockets in development and local precompiled files in production
def javascript_include_tag(file_name)
path_prefix = development? ? '/assets/' : '/js/'
suffix = development? ? '' : "-#{MyProject::VERSION}.min"
%(<script src="#{path_prefix}#{file_name}#{suffix}.js"></script>)
end
def stylesheet_include_tag(file_name)
path_prefix = development? ? '/assets/' : '/css/'
suffix = development? ? '' : "-#{MyProject::VERSION}.min"
%(<link rel="stylesheet" href="#{path_prefix}#{file_name}#{suffix}.css">)
end
namespace :assets do
# `bundle exec rake assets:compile`
# * Compile stylesheets and javascripts
desc 'compile assets'
task :compile => [:compile_css, :compile_js] do
end
# `bundle exec rake assets:compile_css`
# IN => /app/assets/stylesheets/styles.styl
# OUT => /public/css/styles-<version>.min.css
desc 'compile css assets'
task :compile_css do
puts "Compiling stylesheets"
version = @new_version || MyProject::VERSION
sprockets = Sprockets::Environment.new
sprockets.append_path 'app/assets/stylesheets'
# Add Stylus support
# Stylus.setup sprockets
# Stylus.compress = true
# Stylus.use :nib
asset = sprockets['styles.styl']
outpath = File.join('public', 'css')
outfile = Pathname.new(outpath).join("styles-#{version}.min.css")
FileUtils.mkdir_p outfile.dirname
asset.write_to(outfile)
puts "=> Successfully compiled css assets"
end
# `bundle exec rake assets:compile_js`
# IN => /app/assets/javascripts/scripts.coffee
# OUT => /public/js/scripts-<version>.min.js
desc 'compile javascript assets'
task :compile_js do
puts "Compiling javascripts"
version = @new_version || MyProject::VERSION
sprockets = Sprockets::Environment.new
sprockets.js_compressor = YUI::JavaScriptCompressor.new :munge => true, :optimize => true
sprockets.append_path 'app/assets/javascripts'
asset = sprockets['scripts.coffee']
outpath = File.join('public', 'js')
outfile = Pathname.new(outpath).join("scripts-#{version}.min.js")
FileUtils.mkdir_p outfile.dirname
asset.write_to(outfile)
puts "=> Successfully compiled js assets"
end
end
module MyProject
VERSION = '0.0.1'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment