From ba7754e7a78b679680e9d7f2bc0d0334e307da70 Mon Sep 17 00:00:00 2001 From: tdreyno Date: Sun, 6 Feb 2011 10:57:28 -0800 Subject: [PATCH] fix js compression. Speed up static asset loading --- bin/mm-server | 7 ++-- lib/middleman/builder.rb | 46 ++++++++++----------- lib/middleman/features/minify_javascript.rb | 3 ++ lib/middleman/server.rb | 3 +- lib/middleman/version.rb | 2 +- 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/bin/mm-server b/bin/mm-server index 0a82130b..26f0e1c2 100755 --- a/bin/mm-server +++ b/bin/mm-server @@ -48,9 +48,10 @@ class Middleman::Server set :root, Dir.pwd end -require 'shotgun' -config = File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'config.ru') -app = Shotgun.new(config, &lambda { |inner_app| Middleman::Server }) +# require 'shotgun' +# config = File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'config.ru') +# app = Shotgun.new(config, &lambda { |inner_app| Middleman::Server }) +app = Middleman::Server.new require 'rubygems' require 'thin' diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index 9bd40060..6a5cc942 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -29,7 +29,6 @@ module Middleman def initialize(*args) Middleman::Server.new - ::Tilt.mappings.keys << "js" super end @@ -41,11 +40,11 @@ module Middleman end def build_static_files - action Directory.new(self, Middleman::Server.public, Middleman::Server.build_dir) + action Directory.new(self, Middleman::Server.public, Middleman::Server.build_dir, { :force => true }) end def build_dynamic_files - action Directory.new(self, Middleman::Server.views, Middleman::Server.build_dir) + action Directory.new(self, Middleman::Server.views, Middleman::Server.build_dir, { :force => true }) end @@hooks = {} @@ -78,30 +77,29 @@ module Middleman execute! end - protected + protected - def execute! - lookup = config[:recursive] ? File.join(source, '**') : source - lookup = File.join(lookup, '{*,.[a-z]*}') + def execute! + lookup = config[:recursive] ? File.join(source, '**') : source + lookup = File.join(lookup, '{*,.[a-z]*}') + + Dir[lookup].sort.each do |file_source| + next if File.directory?(file_source) + next if file_source.include?('layout') + next unless file_source.split('/').select { |p| p[0,1] == '_' }.empty? - Dir[lookup].sort.each do |file_source| - next if File.directory?(file_source) - next if file_source.include?('layout') - next unless file_source.split('/').select { |p| p[0,1] == '_' }.empty? - - file_extension = File.extname(file_source) - file_destination = File.join(given_destination, file_source.gsub(source, '.')) - file_destination.gsub!('/./', '/') - - handled_by_tilt = ::Tilt.mappings.keys.include?(file_extension.gsub(/^\./, "")) - if handled_by_tilt - file_destination.gsub!(file_extension, "") - destination = base.tilt_template(file_source, file_destination, config, &@block) - else - destination = base.copy_file(file_source, file_destination, config, &@block) - end + file_extension = File.extname(file_source) + file_destination = File.join(given_destination, file_source.gsub(source, '.')) + file_destination.gsub!('/./', '/') + + handled_by_tilt = ::Tilt.mappings.keys.include?(file_extension.gsub(/^\./, "")) + if handled_by_tilt || (file_extension == ".js") + file_destination.gsub!(file_extension, "") unless file_extension == ".js" + destination = base.tilt_template(file_source, file_destination, config, &@block) + else + destination = base.copy_file(file_source, file_destination, config, &@block) end end - + end end end \ No newline at end of file diff --git a/lib/middleman/features/minify_javascript.rb b/lib/middleman/features/minify_javascript.rb index 07a16056..bdbffb3e 100755 --- a/lib/middleman/features/minify_javascript.rb +++ b/lib/middleman/features/minify_javascript.rb @@ -1,6 +1,9 @@ module Middleman::Features::MinifyJavascript class << self def registered(app) + # Only do minification on build or prod mode + return unless [:build, :production].include? app.environment + Middleman::Features::MinifyJavascript::Haml::Javascript.send :include, ::Haml::Filters::Base require "middleman/features/minify_javascript/rack" diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index 3b24b264..69df1cff 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -161,7 +161,8 @@ class Middleman::Server set :app_file, File.expand_path(local_config) end - use ::Rack::ConditionalGet if environment == :development + use ::Rack::ConditionalGet + use ::Rack::Static, :urls => ["/#{self.images_dir}"], :root => "public" @@run_after_features.each { |block| class_eval(&block) } diff --git a/lib/middleman/version.rb b/lib/middleman/version.rb index 949f5d71..1fbc65e1 100644 --- a/lib/middleman/version.rb +++ b/lib/middleman/version.rb @@ -1,3 +1,3 @@ module Middleman - VERSION = "1.1.0.beta.3" + VERSION = "1.1.0.beta.4" end