2012-01-23 16:58:20 +00:00
|
|
|
require 'sprockets'
|
2012-02-08 17:41:18 +00:00
|
|
|
require 'sprockets/engines'
|
2012-02-29 19:24:07 +00:00
|
|
|
require 'forwardable'
|
2012-01-23 16:58:20 +00:00
|
|
|
|
|
|
|
module Flowerbox::Delivery
|
|
|
|
class SprocketsHandler
|
2012-02-29 19:24:07 +00:00
|
|
|
extend Forwardable
|
|
|
|
|
2012-01-23 16:58:20 +00:00
|
|
|
attr_reader :files, :options
|
|
|
|
|
2012-02-29 19:24:07 +00:00
|
|
|
def_delegators :environment, :append_path
|
|
|
|
|
2012-01-23 16:58:20 +00:00
|
|
|
def initialize(options)
|
|
|
|
@options = options
|
|
|
|
|
2012-01-23 17:21:11 +00:00
|
|
|
@files = UniqueAssetList.new
|
2012-01-23 16:58:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add(asset)
|
2012-01-30 16:34:57 +00:00
|
|
|
paths_for(asset).each { |path| @files.add(path_for_compiled_asset(path)) }
|
2012-01-23 16:58:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def paths_for(asset)
|
|
|
|
environment.find_asset(asset).to_a.collect(&:pathname)
|
|
|
|
end
|
|
|
|
|
|
|
|
def environment
|
|
|
|
return @environment if @environment
|
|
|
|
|
|
|
|
@environment = Sprockets::Environment.new
|
2012-01-30 16:34:57 +00:00
|
|
|
@environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons)
|
2012-02-08 17:41:18 +00:00
|
|
|
@environment.unregister_bundle_processor('text/css', Sprockets::CharsetNormalizer)
|
2012-01-30 16:34:57 +00:00
|
|
|
@environment.register_engine('.js', Flowerbox::Delivery::Tilt::JSTemplate)
|
2012-02-08 17:41:18 +00:00
|
|
|
@environment.register_engine('.css', Flowerbox::Delivery::Tilt::CSSTemplate)
|
|
|
|
@environment.register_engine('.jst', Flowerbox::Delivery::Tilt::JSTTemplate)
|
|
|
|
|
2012-02-29 19:24:07 +00:00
|
|
|
options[:asset_paths].each { |path| append_path(path) }
|
2012-01-30 16:34:57 +00:00
|
|
|
|
2012-01-23 16:58:20 +00:00
|
|
|
@environment
|
|
|
|
end
|
2012-01-30 16:34:57 +00:00
|
|
|
|
|
|
|
def asset_for(*args)
|
|
|
|
environment.find_asset(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def path_for_compiled_asset(path)
|
|
|
|
Pathname(asset_for(path, :bundle => false).to_s)
|
|
|
|
end
|
2012-01-23 16:58:20 +00:00
|
|
|
end
|
|
|
|
end
|
2012-02-29 19:24:07 +00:00
|
|
|
|