From 202064690978059bcc40946de50cf15ea03787d0 Mon Sep 17 00:00:00 2001 From: tdreyno Date: Tue, 17 Nov 2009 12:24:10 -0800 Subject: [PATCH] move yui into rack --- lib/middleman/builder.rb | 6 +++++- lib/middleman/features/cache_buster.rb | 13 ++++++++----- lib/middleman/features/minify_javascript.rb | 18 ------------------ lib/middleman/features/sprockets.rb | 5 ----- lib/middleman/rack/sprockets.rb | 16 +++++++++++++++- spec/cache_buster_on_spec.rb | 4 ++-- 6 files changed, 30 insertions(+), 32 deletions(-) delete mode 100755 lib/middleman/features/sprockets.rb diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index b2019d9b..deba3d20 100755 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -27,6 +27,9 @@ module Middleman end def self.file(name, *args, &block) + file_ext = File.extname(args[0]) + return if Middleman::Base.supported_formats.include? file_ext[1..file_ext.length] + if (args[0] === args[1]) args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "").gsub("#{File.basename(Middleman::Base.public)}/", "") end @@ -35,7 +38,8 @@ module Middleman def self.init! glob! File.basename(Middleman::Base.public), [] - glob! File.basename(Middleman::Base.views), Middleman::Base.supported_formats + glob! File.basename(Middleman::Base.views), %w(sass js) + glob! File.basename(Middleman::Base.views), Middleman::Base.supported_formats - %w(sass js) end def after_run diff --git a/lib/middleman/features/cache_buster.rb b/lib/middleman/features/cache_buster.rb index 101bde6a..fcffe0ef 100755 --- a/lib/middleman/features/cache_buster.rb +++ b/lib/middleman/features/cache_buster.rb @@ -30,11 +30,14 @@ class << Middleman::Base end real_path_static = File.join(self.public, prefix, path) - real_path_dynamic = File.join(self.views, prefix, path) - real_path_dynamic << ".sass" if File.extname(real_path_dynamic) == ".css" - - http_path << "?" + File.mtime(real_path_static).strftime("%s") if File.readable?(real_path_static) - http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic) + + if File.readable?(real_path_static) + http_path << "?" + File.mtime(real_path_static).strftime("%s") + elsif Middleman::Base.environment == "build" + real_path_dynamic = File.join(self.root, self.build_dir, prefix, path) + http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic) + end + http_path end end diff --git a/lib/middleman/features/minify_javascript.rb b/lib/middleman/features/minify_javascript.rb index f953ad36..58093708 100755 --- a/lib/middleman/features/minify_javascript.rb +++ b/lib/middleman/features/minify_javascript.rb @@ -1,4 +1,3 @@ -require "yui/compressor" require "middleman/builder" module Middleman @@ -14,23 +13,6 @@ END end end end - - class Builder - alias_method :pre_yui_after_run, :after_run - def after_run - pre_yui_after_run - - compressor = ::YUI::JavaScriptCompressor.new(:munge => true) - Dir[File.join(Middleman::Base.build_dir, Middleman::Base.js_dir, "**", "*.js")].each do |path| - lines = IO.readlines(path) - if lines.length > 1 - compressed_js = compressor.compress(lines.join($/)) - File.open(path, 'w') { |f| f.write(compressed_js) } - say "<%= color('#{"[COMPRESSED]".rjust(12)}', :yellow) %> " + path.gsub(Middleman::Base.build_dir+"/", '') - end - end - end - end end Middleman::Base.supported_formats << "js" \ No newline at end of file diff --git a/lib/middleman/features/sprockets.rb b/lib/middleman/features/sprockets.rb deleted file mode 100755 index 742d3983..00000000 --- a/lib/middleman/features/sprockets.rb +++ /dev/null @@ -1,5 +0,0 @@ -require "middleman/rack/sprockets" - -class Middleman::Base - use Middleman::Rack::Sprockets -end \ No newline at end of file diff --git a/lib/middleman/rack/sprockets.rb b/lib/middleman/rack/sprockets.rb index a9faccae..ce35fbf7 100755 --- a/lib/middleman/rack/sprockets.rb +++ b/lib/middleman/rack/sprockets.rb @@ -1,9 +1,16 @@ begin require 'sprockets' require 'middleman/rack/sprockets+ruby19' # Sprockets ruby 1.9 duckpunch + rescue LoadError puts "Sprockets not available. Install it with: gem install sprockets" end + +begin + require "yui/compressor" +rescue LoadError + puts "Sprockets not available. Install it with: gem install yui-compressor" +end module Middleman module Rack @@ -21,8 +28,15 @@ module Middleman :source_files => [ File.join("views", path) ], :load_path => [ File.join("public", Middleman::Base.js_dir), File.join("views", Middleman::Base.js_dir) ]) + + result = secretary.concatenation.to_s + + if @app.class.respond_to?(:minify_javascript?) && @app.class.minify_javascript? + compressor = ::YUI::JavaScriptCompressor.new(:munge => true) + result = compressor.compress(result) + end - [200, { "Content-Type" => "text/javascript" }, [secretary.concatenation.to_s]] + [200, { "Content-Type" => "text/javascript" }, [result]] else @app.call(env) end diff --git a/spec/cache_buster_on_spec.rb b/spec/cache_buster_on_spec.rb index b91e97d8..847bb679 100755 --- a/spec/cache_buster_on_spec.rb +++ b/spec/cache_buster_on_spec.rb @@ -13,9 +13,9 @@ describe "Cache Buster Feature in CSS" do browser.last_response.body.should include("?") end - it "should append query string in HTML if on" do + it "should not append query string in HTML if on IN DEVELOPMENT" do browser = Rack::Test::Session.new(Rack::MockSession.new(@base.new)) browser.get("/cache-buster.html") - browser.last_response.body.count("?").should == 2 + browser.last_response.body.count("?").should == 0 end end \ No newline at end of file