2011-08-29 15:06:50 -04:00
|
|
|
require 'pathname'
|
|
|
|
require 'rbconfig'
|
2011-07-10 18:10:49 -04:00
|
|
|
require "sprockets"
|
2011-08-29 15:06:50 -04:00
|
|
|
|
2011-07-10 18:10:49 -04:00
|
|
|
module Middleman::CoreExtensions::Sprockets
|
|
|
|
class << self
|
2011-08-09 14:08:46 -04:00
|
|
|
def registered(app)
|
2011-07-10 19:02:52 -04:00
|
|
|
app.set :js_compressor, false
|
2011-10-19 19:59:30 -04:00
|
|
|
app.set :css_compressor, false
|
2011-08-31 16:27:29 -04:00
|
|
|
|
|
|
|
# Cut off every extension after .js (which sprockets eats up)
|
|
|
|
app.build_reroute do |destination, request_path|
|
|
|
|
if !request_path.match(/\.js\./i)
|
|
|
|
false
|
|
|
|
else
|
|
|
|
[
|
|
|
|
destination.gsub(/\.js(\..*)$/, ".js"),
|
|
|
|
request_path.gsub(/\.js(\..*)$/, ".js")
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-09 14:08:46 -04:00
|
|
|
app.after_configuration do
|
2011-08-29 15:06:50 -04:00
|
|
|
js_env = Middleman::CoreExtensions::Sprockets::JavascriptEnvironment.new(app)
|
|
|
|
|
2011-10-28 02:32:45 -04:00
|
|
|
vendor_dir = File.join("vendor", "assets", "javascripts")
|
2011-08-29 15:06:50 -04:00
|
|
|
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
|
2011-10-28 02:32:45 -04:00
|
|
|
::Middleman.spec_has_file?(spec, vendor_dir)
|
2011-08-29 15:06:50 -04:00
|
|
|
end.each do |spec|
|
2011-10-28 02:32:45 -04:00
|
|
|
js_env.append_path File.join(spec.full_gem_path, vendor_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
2011-08-29 15:06:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# add paths to js_env (vendor/assets/javascripts)
|
2011-08-09 14:08:46 -04:00
|
|
|
app.map "/#{app.js_dir}" do
|
2011-08-29 15:06:50 -04:00
|
|
|
run js_env
|
2011-08-09 14:08:46 -04:00
|
|
|
end
|
2011-10-19 19:21:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
app.after_compass_config do
|
|
|
|
css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(app)
|
2011-10-28 02:32:45 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
app_dir = File.join("app", "assets", "stylesheets")
|
2011-10-19 19:21:46 -04:00
|
|
|
gems_with_css = ::Middleman.rubygems_latest_specs.select do |spec|
|
2011-10-28 02:32:45 -04:00
|
|
|
::Middleman.spec_has_file?(spec, app_dir)
|
2011-10-19 19:21:46 -04:00
|
|
|
end.each do |spec|
|
2011-10-28 02:32:45 -04:00
|
|
|
css_env.append_path File.join(spec.full_gem_path, app_dir)
|
2011-10-19 19:21:46 -04:00
|
|
|
end
|
2011-08-09 18:54:07 -04:00
|
|
|
|
2011-10-19 19:21:46 -04:00
|
|
|
app.map "/#{app.css_dir}" do
|
|
|
|
run css_env
|
|
|
|
end
|
2011-07-10 19:02:52 -04:00
|
|
|
end
|
2011-07-10 18:10:49 -04:00
|
|
|
end
|
|
|
|
alias :included :registered
|
|
|
|
end
|
|
|
|
|
2011-08-03 00:58:20 -04:00
|
|
|
class MiddlemanEnvironment < ::Sprockets::Environment
|
2011-07-10 19:02:52 -04:00
|
|
|
def initialize(app)
|
2011-07-13 20:34:41 -04:00
|
|
|
full_path = app.views
|
|
|
|
full_path = File.join(app.root, app.views) unless app.views.include?(app.root)
|
|
|
|
|
|
|
|
super File.expand_path(full_path)
|
2011-08-31 16:15:59 -04:00
|
|
|
|
|
|
|
# Make the app context available to Sprockets
|
|
|
|
context_class.send(:define_method, :app) { app }
|
|
|
|
context_class.class_eval do
|
|
|
|
def method_missing(name)
|
|
|
|
if app.respond_to?(name)
|
|
|
|
app.send(name)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-10 19:02:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class JavascriptEnvironment < MiddlemanEnvironment
|
|
|
|
def initialize(app)
|
|
|
|
super
|
|
|
|
|
|
|
|
# Disable css
|
2011-10-19 19:59:30 -04:00
|
|
|
# unregister_processor "text/css", ::Sprockets::DirectiveProcessor
|
2011-07-10 19:02:52 -04:00
|
|
|
|
|
|
|
self.js_compressor = app.settings.js_compressor
|
2011-07-10 18:10:49 -04:00
|
|
|
|
|
|
|
# configure search paths
|
2011-07-13 20:34:41 -04:00
|
|
|
append_path app.js_dir
|
2011-07-10 18:10:49 -04:00
|
|
|
end
|
2011-08-19 19:42:28 -04:00
|
|
|
|
|
|
|
def javascript_exception_response(exception)
|
|
|
|
expire_index!
|
|
|
|
super(exception)
|
|
|
|
end
|
2011-07-10 18:10:49 -04:00
|
|
|
end
|
2011-07-10 19:02:52 -04:00
|
|
|
|
2011-10-19 19:21:46 -04:00
|
|
|
class StylesheetEnvironment < MiddlemanEnvironment
|
|
|
|
def initialize(app)
|
|
|
|
super
|
|
|
|
|
|
|
|
# Disable js
|
2011-10-19 19:59:30 -04:00
|
|
|
# unregister_processor "application/javascript", ::Sprockets::DirectiveProcessor
|
|
|
|
|
|
|
|
self.css_compressor = app.settings.css_compressor
|
2011-10-19 19:21:46 -04:00
|
|
|
|
|
|
|
# configure search paths
|
|
|
|
append_path app.css_dir
|
|
|
|
end
|
|
|
|
|
|
|
|
def css_exception_response(exception)
|
|
|
|
expire_index!
|
|
|
|
super(exception)
|
|
|
|
end
|
|
|
|
end
|
2011-07-10 18:10:49 -04:00
|
|
|
end
|