Class: Rake::Pipeline::Web::Filters::UglifyFilter

Inherits:
Filter
  • Object
show all
Includes:
FilterWithDependencies
Defined in:
lib/rake-pipeline-web-filters/uglify_filter.rb

Overview

A filter that uses the Uglify JS compressor to compress JavaScript input files.

Requires uglifier.

Examples:

Rake::Pipeline.build do
  input "app/assets", "**/*.js"
  output "public"

  # Compile each JS file under the app/assets
  # directory.
  filter Rake::Pipeline::Web::Filters::UglifyFilter
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ UglifyFilter

Returns a new instance of UglifyFilter

Parameters:

  • options (Hash) (defaults to: {})

    options to pass to Uglify

  • block (Proc)

    a block to use as the Filter's #output_name_generator.


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rake-pipeline-web-filters/uglify_filter.rb', line 29

def initialize(options={}, &block)
  block ||= proc { |input| 
    if input =~ %r{.min.js$}
      input
    else
      input.sub(/\.js$/, '.min.js')
    end
  }

  @preserve_input = options.delete :preserve_input

  super(&block)
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns a hash of options to pass to Uglify when compiling.

Returns:

  • (Hash)

    a hash of options to pass to Uglify when compiling.


24
25
26
# File 'lib/rake-pipeline-web-filters/uglify_filter.rb', line 24

def options
  @options
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object

Implement the #generate_output method required by the Filter API. Compiles each input file with Uglify.

Parameters:

  • inputs (Array<FileWrapper>)

    an Array of FileWrapper objects representing the inputs to this filter.

  • output (FileWrapper)

    a single FileWrapper object representing the output.


57
58
59
60
61
62
63
64
65
# File 'lib/rake-pipeline-web-filters/uglify_filter.rb', line 57

def generate_output(inputs, output)
  inputs.each do |input|
    if should_skip_minify?(input, output)
      output.write input.read
    else
      output.write Uglifier.compile(input.read, options)
    end
  end
end

#should_skip_minify?(input, output) ⇒ Boolean

Returns:

  • (Boolean)

44
45
46
47
# File 'lib/rake-pipeline-web-filters/uglify_filter.rb', line 44

def should_skip_minify?(input, output)
  (@preserve_input && input.path == output.path) ||
  input.path =~ %r{.min.js$}
end