better rack

This commit is contained in:
tdreyno 2010-03-02 17:38:08 -08:00
parent 3f2f6d7150
commit ccbc3cf46f
6 changed files with 38 additions and 55 deletions

View file

@ -23,7 +23,7 @@ module Middleman
set :build_dir, "build"
set :http_prefix, nil
use ::Rack::ConditionalGet if environment == :development
#use ::Rack::ConditionalGet if environment == :development
helpers Sinatra::ContentFor
set :features, []
@ -137,7 +137,6 @@ require "middleman/rack/static"
require "middleman/rack/sprockets"
require "middleman/rack/minify_javascript"
require "middleman/rack/minify_css"
require "middleman/rack/downstream"
class Middleman::Base
helpers Middleman::Helpers
@ -166,11 +165,14 @@ class Middleman::Base
set :app_file, File.expand_path(local_config)
end
use Middleman::Rack::Static
use Middleman::Rack::Sprockets
# use Rack::Static, :urls => [/.*/], :root => Middleman::Base.public
# use Rack::Static, :urls => [/.*/], :root => Middleman::Base.views
# use Middleman::Rack::Static
use Middleman::Rack::Sprockets, :root => Middleman::Base.root,
:load_path => [ File.join("public", Middleman::Base.js_dir),
File.join("views", Middleman::Base.js_dir) ]
use Middleman::Rack::MinifyJavascript
use Middleman::Rack::MinifyCSS
use Middleman::Rack::Downstream
def self.new(*args, &block)
# loop over enabled feature

View file

@ -1,9 +0,0 @@
class Middleman::Rack::Downstream
def initialize(app, options={})
@app = app
end
def call(env)
env["DOWNSTREAM"] || @app.call(env)
end
end

View file

@ -10,19 +10,16 @@ class Middleman::Rack::MinifyCSS
end
def call(env)
if Middleman::Base.enabled?(:minify_css) &&
env["DOWNSTREAM"] && env["PATH_INFO"].match(/\.css$/)
status, headers, response = @app.call(env)
if Middleman::Base.enabled?(:minify_css) && env["PATH_INFO"].match(/\.css$/)
compressor = ::YUI::CssCompressor.new
source = env["DOWNSTREAM"][2].is_a?(::Rack::File) ?
File.read(env["DOWNSTREAM"][2].path) :
env["DOWNSTREAM"][2]
env["DOWNSTREAM"][2] = compressor.compress(source)
env["DOWNSTREAM"][1]["Content-Length"] = ::Rack::Utils.bytesize(env["DOWNSTREAM"][2]).to_s
uncompressed_source = response.is_a?(::Rack::File) ? File.read(response.path) : response
response = compressor.compress(uncompressed_source)
headers["Content-Length"] = ::Rack::Utils.bytesize(response).to_s
end
@app.call(env)
[status, headers, response]
end
end

View file

@ -10,19 +10,16 @@ class Middleman::Rack::MinifyJavascript
end
def call(env)
if Middleman::Base.enabled?(:minify_javascript) &&
env["DOWNSTREAM"] && env["PATH_INFO"].match(/\.js$/)
status, headers, response = @app.call(env)
if Middleman::Base.enabled?(:minify_javascript) && env["PATH_INFO"].match(/\.js$/)
compressor = ::YUI::JavaScriptCompressor.new(:munge => true)
source = env["DOWNSTREAM"][2].is_a?(::Rack::File) ?
File.read(env["DOWNSTREAM"][2].path) :
env["DOWNSTREAM"][2]
env["DOWNSTREAM"][2] = compressor.compress(source)
env["DOWNSTREAM"][1]["Content-Length"] = ::Rack::Utils.bytesize(env["DOWNSTREAM"][2]).to_s
uncompressed_source = response.is_a?(::Rack::File) ? File.read(response.path) : response
response = compressor.compress(uncompressed_source)
headers["Content-Length"] = ::Rack::Utils.bytesize(response).to_s
end
@app.call(env)
[status, headers, response]
end
end

View file

@ -8,27 +8,23 @@ end
class Middleman::Rack::Sprockets
def initialize(app, options={})
@app = app
@options = options
end
def call(env)
path = env["PATH_INFO"]
if env["PATH_INFO"].match(/\.js$/)
public_file_path = File.join(Middleman::Base.public, env["PATH_INFO"])
view_file_path = File.join(Middleman::Base.views, env["PATH_INFO"])
if env["DOWNSTREAM"] && path.match(/\.js$/)
source = "public" if File.exists?(File.join(Middleman::Base.views, path))
source = "views" if File.exists?(File.join(Middleman::Base.views, path))
source_file = Rack::File.new(Middleman::Base.public) if File.exists?(public_file_path)
source_file = Rack::File.new(Middleman::Base.views) if File.exists?(view_file_path)
if source
source_file = env["DOWNSTREAM"][2].is_a?(::Rack::File) ?
env["DOWNSTREAM"][2].path :
env["DOWNSTREAM"][2]
secretary = ::Sprockets::Secretary.new( :root => Middleman::Base.root,
:source_files => [ source_file ],
:load_path => [ File.join("public", Middleman::Base.js_dir),
File.join("views", Middleman::Base.js_dir) ])
env["DOWNSTREAM"][2] = secretary.concatenation.to_s
env["DOWNSTREAM"][1]["Content-Length"] = ::Rack::Utils.bytesize(env["DOWNSTREAM"][2]).to_s
if source_file
status, headers, response = source_file.call(env)
secretary = ::Sprockets::Secretary.new(@options.merge( :source_files => [ response.path ] ))
response = secretary.concatenation.to_s
headers["Content-Length"] = ::Rack::Utils.bytesize(response).to_s
return [status, headers, response]
end
end

View file

@ -8,11 +8,11 @@ class Middleman::Rack::Static
view_file_path = File.join(Middleman::Base.views, env["PATH_INFO"])
if File.exists?(public_file_path) && !File.directory?(public_file_path)
env["DOWNSTREAM"] = ::Rack::File.new(Middleman::Base.public).call(env)
::Rack::File.new(Middleman::Base.public).call(env)
elsif File.exists?(view_file_path) && !File.directory?(view_file_path)
env["DOWNSTREAM"] = ::Rack::File.new(Middleman::Base.views).call(env)
end
::Rack::File.new(Middleman::Base.views).call(env)
else
@app.call(env)
end
end
end