Add project vendored css and js into sprockets paths
This commit is contained in:
parent
740d4a913f
commit
c9b4edcc0a
1 changed files with 34 additions and 60 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue