Class: Rake::Pipeline::Web::Filters::YUIJavaScriptFilter

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

Overview

A filter that compresses JavaScript input files using the YUI JavaScript compressor.

Requires yui-compressor

Examples:

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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of YUIJavaScriptFilter.

Parameters:

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

    options to pass to the YUI JavaScript compressor.

  • block (Proc)

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



30
31
32
33
34
# File 'lib/rake-pipeline-web-filters/yui_javascript_filter.rb', line 30

def initialize(options={}, &block)
  block ||= proc { |input| input.sub(/\.js$/, '.min.js') }
  super(&block)
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns a hash of options to pass to the YUI compressor when compressing.

Returns:

  • (Hash)

    a hash of options to pass to the YUI compressor when compressing.



24
25
26
# File 'lib/rake-pipeline-web-filters/yui_javascript_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. Compresses each input file with the YUI JavaScript compressor.

Parameters:

  • inputs (Array<FileWrapper>)

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

  • output (FileWrapper)

    a single FileWrapper object representing the output.



45
46
47
48
49
50
# File 'lib/rake-pipeline-web-filters/yui_javascript_filter.rb', line 45

def generate_output(inputs, output)
  compressor = YUI::JavaScriptCompressor.new(options)
  inputs.each do |input|
    output.write compressor.compress(input.read)
  end
end