Merge pull request #271 from Vasfed/master

Add project vendored css and js into sprockets path
This commit is contained in:
Thomas Reynolds 2012-02-19 16:24:44 -08:00
commit 3c78d9f171
4 changed files with 43 additions and 62 deletions

View file

@ -8,3 +8,8 @@ Feature: Sprockets Gems
# Given the Server is running at "sprockets-app"
# When I go to "/library/css/bootstrap_include.css"
# Then I should see "Bootstrap"
Scenario: Sprockets can pull js from vendored assets
Given the Server is running at "sprockets-app"
When I go to "/library/js/vendored_include.js"
Then I should see "var vendored_js_included = true;"

View file

@ -0,0 +1 @@
//= require "vendored_js"

View file

@ -0,0 +1 @@
var vendored_js_included = true;

View file

@ -15,56 +15,30 @@ module Middleman::CoreExtensions::Sprockets
# 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)
# 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')}
{ 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
# 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
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