2011-04-17 05:50:19 -04:00
|
|
|
module Sprockets
|
2011-05-23 20:26:13 -04:00
|
|
|
autoload :Helpers, "sprockets/helpers"
|
2011-07-06 20:38:54 -04:00
|
|
|
autoload :LazyCompressor, "sprockets/compressors"
|
|
|
|
autoload :NullCompressor, "sprockets/compressors"
|
2011-05-23 20:26:13 -04:00
|
|
|
|
2011-06-29 08:39:41 -04:00
|
|
|
# TODO: Get rid of config.assets.enabled
|
2011-05-23 20:26:13 -04:00
|
|
|
class Railtie < ::Rails::Railtie
|
2011-07-07 18:13:10 -04:00
|
|
|
config.default_asset_host_protocol = :relative
|
|
|
|
|
2011-06-29 08:39:41 -04:00
|
|
|
rake_tasks do
|
|
|
|
load "sprockets/assets.rake"
|
|
|
|
end
|
|
|
|
|
2011-04-17 05:50:19 -04:00
|
|
|
# Configure ActionController to use sprockets.
|
|
|
|
initializer "sprockets.set_configs", :after => "action_controller.set_configs" do |app|
|
|
|
|
ActiveSupport.on_load(:action_controller) do
|
|
|
|
self.use_sprockets = app.config.assets.enabled
|
|
|
|
end
|
2011-04-17 04:51:07 -04:00
|
|
|
end
|
|
|
|
|
2011-04-17 05:50:19 -04:00
|
|
|
# We need to configure this after initialization to ensure we collect
|
|
|
|
# paths from all engines. This hook is invoked exactly before routes
|
2011-05-24 02:32:34 -04:00
|
|
|
# are compiled, and so that other Railties have an opportunity to
|
|
|
|
# register compressors.
|
2011-04-17 05:50:19 -04:00
|
|
|
config.after_initialize do |app|
|
|
|
|
assets = app.config.assets
|
|
|
|
next unless assets.enabled
|
|
|
|
|
|
|
|
app.assets = asset_environment(app)
|
2011-04-19 15:49:28 -04:00
|
|
|
|
|
|
|
ActiveSupport.on_load(:action_view) do
|
2011-05-24 02:30:06 -04:00
|
|
|
include ::Sprockets::Helpers::RailsHelper
|
|
|
|
|
2011-05-17 17:19:33 -04:00
|
|
|
app.assets.context_class.instance_eval do
|
2011-05-23 19:07:16 -04:00
|
|
|
include ::Sprockets::Helpers::RailsHelper
|
2011-04-21 12:03:56 -04:00
|
|
|
end
|
2011-04-19 15:49:28 -04:00
|
|
|
end
|
|
|
|
|
2011-05-08 06:57:07 -04:00
|
|
|
app.routes.prepend do
|
2011-04-17 05:50:19 -04:00
|
|
|
mount app.assets => assets.prefix
|
|
|
|
end
|
|
|
|
|
|
|
|
if config.action_controller.perform_caching
|
|
|
|
app.assets = app.assets.index
|
|
|
|
end
|
2011-04-17 04:51:07 -04:00
|
|
|
end
|
|
|
|
|
2011-04-17 05:50:19 -04:00
|
|
|
protected
|
2011-06-08 19:29:25 -04:00
|
|
|
def asset_environment(app)
|
|
|
|
require "sprockets"
|
2011-04-17 04:51:07 -04:00
|
|
|
|
2011-06-08 19:29:25 -04:00
|
|
|
assets = app.config.assets
|
|
|
|
|
|
|
|
env = Sprockets::Environment.new(app.root.to_s)
|
|
|
|
|
|
|
|
env.static_root = File.join(app.root.join("public"), assets.prefix)
|
2011-06-14 15:39:34 -04:00
|
|
|
|
|
|
|
if env.respond_to?(:append_path)
|
|
|
|
assets.paths.each { |path| env.append_path(path) }
|
|
|
|
else
|
|
|
|
env.paths.concat assets.paths
|
|
|
|
end
|
2011-06-08 19:29:25 -04:00
|
|
|
|
|
|
|
env.logger = Rails.logger
|
|
|
|
|
2011-07-12 21:30:11 -04:00
|
|
|
if env.respond_to?(:cache) && assets.cache_store != false
|
|
|
|
env.cache = ActiveSupport::Cache.lookup_store(assets.cache_store) || Rails.cache
|
2011-06-21 10:38:25 -04:00
|
|
|
end
|
|
|
|
|
2011-06-16 15:08:26 -04:00
|
|
|
if assets.compress
|
|
|
|
# temporarily hardcode default JS compressor to uglify. Soon, it will work
|
|
|
|
# the same as SCSS, where a default plugin sets the default.
|
2011-07-11 10:19:03 -04:00
|
|
|
unless assets.js_compressor == false
|
|
|
|
env.js_compressor = LazyCompressor.new { expand_js_compressor(assets.js_compressor || :uglifier) }
|
|
|
|
end
|
|
|
|
|
|
|
|
unless assets.css_compressor == false
|
|
|
|
env.css_compressor = LazyCompressor.new { expand_css_compressor(assets.css_compressor) }
|
|
|
|
end
|
2011-06-16 15:08:26 -04:00
|
|
|
end
|
2011-04-22 11:49:55 -04:00
|
|
|
|
2011-06-08 19:29:25 -04:00
|
|
|
env
|
2011-04-22 11:49:55 -04:00
|
|
|
end
|
|
|
|
|
2011-06-08 19:29:25 -04:00
|
|
|
def expand_js_compressor(sym)
|
|
|
|
case sym
|
|
|
|
when :closure
|
|
|
|
require 'closure-compiler'
|
|
|
|
Closure::Compiler.new
|
|
|
|
when :uglifier
|
|
|
|
require 'uglifier'
|
|
|
|
Uglifier.new
|
|
|
|
when :yui
|
|
|
|
require 'yui/compressor'
|
|
|
|
YUI::JavaScriptCompressor.new
|
|
|
|
else
|
|
|
|
sym
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expand_css_compressor(sym)
|
|
|
|
case sym
|
|
|
|
when :yui
|
|
|
|
require 'yui/compressor'
|
|
|
|
YUI::CssCompressor.new
|
|
|
|
else
|
|
|
|
sym
|
|
|
|
end
|
2011-04-22 11:49:55 -04:00
|
|
|
end
|
2011-04-17 04:51:07 -04:00
|
|
|
end
|
2011-04-21 12:03:56 -04:00
|
|
|
end
|