public
Created

Rails 3.0.x runtime compilation and asset packaging with Barista and Jammit on Heroku

  • Download Gist
assets.yml
YAML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# config/assets.yml
 
package_assets: on
 
compress_assets: on
# compress_assets: off
javascript_compressor: uglifier
 
javascripts:
all:
- public/javascripts/jquery.min.js
- public/javascripts/jquery_ujs.js
- public/javascripts/jquery-ui.min.js
- public/javascripts/**/*.js
 
# pull in all the compiled javascripts (from barista)
- tmp/javascripts/**/*.js
 
stylesheets:
all:
# pull in all the compiled stylesheets (from compass/sass)
# - tmp/stylesheets/compiled/**/*.css
ie:
# - tmp/stylesheets/ie.css
barista_config.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# config/initializers/barista_config.rb
 
# Configure barista.
Barista.configure do |c|
 
# Change the root to use app/scripts
# c.root = Rails.root.join("app", "scripts")
c.root = Rails.root.join("app", "coffeescripts")
 
# Change the output root, causing Barista to compile into public/coffeescripts
# c.output_root = Rails.root.join("public", "coffeescripts")
c.output_root = Rails.root.join("tmp", "javascripts") # Heroku patch
 
# Disable auto compile, use generated file directly:
# c.auto_compile = false
 
# Add a new framework:
 
# c.register :tests, :root => Rails.root.join('test', 'coffeescript'), :output_prefix => 'test'
 
# Disable wrapping in a closure:
c.bare = true
# ... or ...
# c.bare!
 
# c.before_compilation { |path| puts "Barista: Compiling #{path}" }
# c.on_compilation { |path| puts "Barista: Successfully compiled #{path}" }
# c.on_compilation_error { |path, output| puts "Barista: Compilation of #{path} failed with:\n#{output}" }
# c.on_compilation_with_warning { |path, output| puts "Barista: Compilation of #{path} had a warning:\n#{output}" }
 
# Make helpers and the HAML filter output coffee-script instead of the compiled JS.
# Used in combination with the coffeescript_interpreter_js helper in Rails.
# c.embedded_interpreter = true
end
heroku_specifics.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# config/initializers/heroku_specifics.rb
 
require 'fileutils'
 
Rails.configuration.middleware.delete('Barista::Filter')
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Barista::Filter')
 
if Rails.env.development?
class Jammit::Packager
alias_method :old_individual_urls, :individual_urls
def individual_urls(package, extension)
Rails.logger.info "[HEROKU] Rewriting /tmp/javascript paths to /javascript"
old_individual_urls(package, extension).collect { |c| c.gsub(/^\/tmp\//, '/') }
end
end
end
 
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Rack::Static',
:urls => ['/javascripts'],
:root => "#{Rails.root}/tmp")

There is one hiccup that needs to be patched in Jammit. Currently, Jammit requires YUI regardless of specifying the use of "uglifier" in the assets. This dependency check is there because Jammit currently only supports YUI compression for CSS. There is an open issue for fixing, or disabling, this: https://github.com/documentcloud/jammit/issues/162

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.