Class: Rack::PageSpeed::Filters::MinifyJavaScripts

Inherits:
Base
  • Object
show all
Defined in:
lib/rack/pagespeed/filters/minify_javascripts.rb

Instance Attribute Summary

Attributes inherited from Base

#document, #options

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
        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

#local_script?(node) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rack/pagespeed/filters/minify_javascripts.rb', line 38

def local_script? node
  node.name == 'script' and file_for(node)
end