Sha256: bf1ce0cf041673b821ec2a6e4312a79932d60c4b47a91e572648a4ae5905a4a0

Contents?: true

Size: 1.14 KB

Versions: 9

Compression:

Stored size: 1.14 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  2
      
  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.name == 'script' and file_for(node)
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
rack-pagespeed-fork-0.1.0 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.1.2 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.1.1 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.1.0 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.0.9 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.0.8 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.0.7 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.0.6 lib/rack/pagespeed/filters/minify_javascripts.rb
rack-pagespeed-1.0.5 lib/rack/pagespeed/filters/minify_javascripts.rb