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

  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.



50
51
52
53
54
55
56
57
58
# File 'lib/rake-pipeline-web-filters/uglify_filter.rb', line 50

def generate_output(inputs, output)
  inputs.each do |input|
    if input.path =~ %r{min.js$}
      output.write input.read
    else
      output.write Uglifier.compile(input.read, options)
    end
  end
end