Module: Middleman::Features::SmushPNGs

Defined in:
lib/middleman/features/smush_pngs.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



3
4
5
6
7
8
9
10
11
12
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/middleman/features/smush_pngs.rb', line 3

def registered(app)
  require "middleman/builder"

  app.alias_method  :pre_smush_after_run, :after_run
  app.define_method :after_run do
    pre_smush_after_run
    smush_dir = File.join(Middleman::Server.build_dir, Middleman::Server.images_dir)
  
    # Read cache
    cache_file = File.join(Middleman::Server.root, ".smush-cache")
    cache_data = if File.exists?(cache_file)
      Marshal.restore(File.read(cache_file))
    else
      {}
    end
  
    require "smusher"
    require "json/pure"
    ::Smusher.class_eval do
      images_in_folder(smush_dir).each do |file|
        original_file_size = size(file)
        return if original_file_size.zero?
        return if cache_data[file] && cache_data[file] == original_file_size

        with_logging(file, true) do
          write_optimized_data(file)
          cache_data[file] = size(file) # Add or update cache
          File.open(cache_file, "w") { |f| f.write Marshal.dump(cache_data) } # Write cache
          say "<%= color('#{"[SMUSHED]".rjust(12)}', :yellow) %>  " + file.gsub(Middleman::Server.build_dir+"/", '')
        end
      end
    end
  end
end