Merge pull request #271 from Vasfed/master
Add project vendored css and js into sprockets path
This commit is contained in:
commit
3c78d9f171
4 changed files with 43 additions and 62 deletions
|
@ -3,8 +3,13 @@ Feature: Sprockets Gems
|
|||
Given the Server is running at "sprockets-app"
|
||||
When I go to "/library/js/jquery_include.js"
|
||||
Then I should see "var jQuery ="
|
||||
|
||||
|
||||
# Scenario: Sprockets can pull CSS from gem
|
||||
# Given the Server is running at "sprockets-app"
|
||||
# When I go to "/library/css/bootstrap_include.css"
|
||||
# Then I should see "Bootstrap"
|
||||
# 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;"
|
|
@ -0,0 +1 @@
|
|||
//= require "vendored_js"
|
1
middleman-more/fixtures/sprockets-app/vendor/assets/javascripts/vendored_js.js
vendored
Normal file
1
middleman-more/fixtures/sprockets-app/vendor/assets/javascripts/vendored_js.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
var vendored_js_included = true;
|
|
@ -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!
|
||||
|
|
Loading…
Reference in a new issue