Class: Rack::PageSpeed::Filters::MinifyJavaScripts
- Defined in:
- lib/rack/pagespeed/filters/minify_javascripts.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
available_filters, inherited, #initialize, name, priority, requires_store
Constructor Details
This class inherits a constructor from Rack::PageSpeed::Filters::Base
Instance Method Details
#execute!(document) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rack/pagespeed/filters/minify_javascripts.rb', line 13 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 status, headers, body = content_for node next unless node.name == 'script' && status == 200 javascript = ""; body.each do |part| javascript << part end hash = Digest::MD5.hexdigest headers['Last-Modified'] + 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 |