Sha256: e0e40a061bc2feccb8511c4a5399049767babe3939e6857d8dbff31bb352518c

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module WebResourceBundler::Filters::CompressFilter
  class Filter < WebResourceBundler::Filters::BaseFilter

    FILE_PREFIX = 'min_'

    def initialize(settings, file_manager)
      super(settings, file_manager)
      @js_compressor  = YUI::JavaScriptCompressor.new(:munge => settings[:obfuscate_js])
      @css_compressor = YUI::CssCompressor.new
    end

    def set_settings(settings)
      if @settings[:obfuscate_js] != settings[:obfuscate_js] 
        @js_compressor = YUI::JavaScriptCompressor.new(:munge => settings[:obfuscate_js]) 
      end
      @settings = settings
    end

    def apply!(block_data)
      compress_styles!(block_data.styles)
      compress_scripts!(block_data.scripts)
      block_data
    end

    protected

    def compress_scripts!(scripts)
      scripts.each do |file|
        file.content = @js_compressor.compress(file.content) unless file.content.empty?
        file.path    = new_js_path(file.path)
      end
    end

    def compress_styles!(styles)
      styles.each do |file|
        file.content = @css_compressor.compress(file.content) unless file.content.empty?
        file.path    = new_css_path(file.path)
      end
    end

    def new_css_path(path)
      @file_manager.cache_path_with_prefix(FILE_PREFIX, path)
    end

    def new_js_path(path)
      @file_manager.cache_path_with_prefix(FILE_PREFIX, path)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
web_resource_bundler-0.0.23 lib/web_resource_bundler/filters/compress_filter.rb
web_resource_bundler-0.0.22 lib/web_resource_bundler/filters/compress_filter.rb