Sha256: b6c846ff60d3befcdbc0b7224c638ec9bb7cc621e00c44f34debb01d62f7f696

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

require 'jsmin'
begin
  require 'md5'
rescue LoadError
  require 'digest/md5'
end

class Rack::PageSpeed::Filters::MinifyJavaScripts < Rack::PageSpeed::Filters::Base
  requires_store
  name      'minify_javascripts'
  priority  8
      
  def execute! document
    nodes = document.css('script')
    return false unless nodes.count > 0
    nodes.each do |node|
      if !node['src']
        node.content = JSMin.minify node.content
      else
        if match = %r(^/rack-pagespeed-(.*)).match(node['src'])
          store = @options[:store]
          store[match[1]] = JSMin.minify store[match[1]]
        else
          next unless local_script? node
          file = file_for node
          javascript = file.read
          hash = Digest::MD5.hexdigest file.mtime.to_i.to_s + javascript
          compressed = Nokogiri::XML::Node.new 'script', document
          compressed['src'] = "/rack-pagespeed-#{hash}.js"
          @options[:store]["#{hash}.js"] = JSMin.minify javascript
          node.before compressed
          node.remove
        end        
      end
    end
  end
  
  def local_script? node
    node['src'] and !(node['src'] =~ /^http/ or !(node['src'] =~ /.js$/))
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rack-pagespeed-1.0.4 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.0.3 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.0.2 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.0.1 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.0.0 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-0.2.0 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-0.1.0 lib/rack/pagespeed/filters/minify_javascripts.rb