Add project vendored css and js into sprockets paths

This commit is contained in:
Vasily Fedoseyev 2012-02-14 00:17:32 +04:00
parent 740d4a913f
commit c9b4edcc0a

View file

@ -3,68 +3,42 @@ require "sprockets"
# Sprockets extension
module Middleman::CoreExtensions::Sprockets
# Setup extension
class << self
# Once registered
def registered(app)
# Default compression to off
app.set :js_compressor, false
app.set :css_compressor, false
# Once Middleman is setup
app.ready do
# Create sprockets env for JS
# Create sprockets env for JS and CSS
js_env = Middleman::CoreExtensions::Sprockets::JavascriptEnvironment.new(self)
css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(self)
# Add any gems with vendor/assets/javascripts to paths
vendor_dir = File.join("vendor", "assets", "javascripts")
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
::Middleman.spec_has_file?(spec, vendor_dir)
end.each do |spec|
js_env.append_path File.join(spec.full_gem_path, vendor_dir)
end
# Add any gems with (vendor|app|.)/assets/javascripts to paths
# also add similar directories from project root (like in rails)
root_paths = [%w{ app }, %w{ assets }, %w{ vendor }, %w{ app assets }, %w{ vendor assets }]
try_js_paths = root_paths.map{|rp| File.join(rp, 'javascripts')}
try_css_paths = root_paths.map{|rp| File.join(rp, 'stylesheets')}
# Add any gems with app/assets/javascripts to paths
app_dir = File.join("app", "assets", "javascripts")
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
::Middleman.spec_has_file?(spec, app_dir)
end.each do |spec|
js_env.append_path File.join(spec.full_gem_path, app_dir)
end
# Intercept requests to /javascripts and pass to sprockets
map "/#{js_dir}" do
run js_env
{ try_js_paths => js_env, try_css_paths => css_env }.each do |paths, env|
([root] + ::Middleman.rubygems_latest_specs.map(&:full_gem_path)).each do |root_path|
paths.map{|p| File.join(root_path, p)}.
select{|p| File.directory?(p)}.
each{|path| env.append_path(path)}
end
end
# Setup Sprockets Sass options
sass.each { |k, v| ::Sprockets::Sass.options[k] = v }
# Create sprockets env for CSS
css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(self)
# Add any gems with vendor/assets/stylesheets to paths
vendor_dir = File.join("vendor", "assets", "stylesheets")
gems_with_css = ::Middleman.rubygems_latest_specs.select do |spec|
::Middleman.spec_has_file?(spec, vendor_dir)
end.each do |spec|
css_env.append_path File.join(spec.full_gem_path, vendor_dir)
end
# Add any gems with app/assets/stylesheets to paths
app_dir = File.join("app", "assets", "stylesheets")
gems_with_css = ::Middleman.rubygems_latest_specs.select do |spec|
::Middleman.spec_has_file?(spec, app_dir)
end.each do |spec|
css_env.append_path File.join(spec.full_gem_path, app_dir)
end
# Intercept requests to /stylesheets and pass to sprockets
map("/#{css_dir}") do
run css_env
end
# Intercept requests to /javascripts and /stylesheets and pass to sprockets
map("/#{js_dir}") { run js_env }
map("/#{css_dir}"){ run css_env }
end
end
alias :included :registered
@ -76,7 +50,7 @@ module Middleman::CoreExtensions::Sprockets
def initialize(app)
@app = app
super app.source_dir
# Make the app context available to Sprockets
context_class.send(:define_method, :app) { app }
context_class.class_eval do
@ -89,26 +63,26 @@ module Middleman::CoreExtensions::Sprockets
end
end
end
# During development, don't use the asset cache
def find_asset(path, options = {})
expire_index! if @app.development?
super
end
end
# Javascript specific environment
class JavascriptEnvironment < MiddlemanEnvironment
# Init
def initialize(app)
super
expire_index!
# Remove old compressor
unregister_bundle_processor 'application/javascript', :js_compressor
# Register compressor from config
register_bundle_processor 'application/javascript', :js_compressor do |context, data|
if context.pathname.to_s =~ /\.min\./
@ -117,30 +91,30 @@ module Middleman::CoreExtensions::Sprockets
app.js_compressor.compress(data)
end
end if app.js_compressor
# configure search paths
append_path app.js_dir
end
# Clear cache on error
def javascript_exception_response(exception)
expire_index!
super(exception)
end
end
# CSS specific environment
class StylesheetEnvironment < MiddlemanEnvironment
# Init
def initialize(app)
super
expire_index!
# Remove old compressor
unregister_bundle_processor 'text/css', :css_compressor
# Register compressor from config
register_bundle_processor 'text/css', :css_compressor do |context, data|
if context.pathname.to_s =~ /\.min\./
@ -149,11 +123,11 @@ module Middleman::CoreExtensions::Sprockets
app.css_compressor.compress(data)
end
end if app.css_compressor
# configure search paths
append_path app.css_dir
end
# Clear cache on error
def css_exception_response(exception)
expire_index!