Module: YUICompressor

Defined in:
lib/yuicompressor.rb,
lib/yuicompressor/jruby.rb,
lib/yuicompressor/shell.rb

Defined Under Namespace

Classes: ErrorReporter

Constant Summary collapse

JAR_FILE =

The path to the YUI Compressor jar file.

File.expand_path('../yuicompressor-2.4.8.jar', __FILE__)

Class Method Summary collapse

Class Method Details

.command_arguments(options = {}) ⇒ Object

Returns an array of flags that should be passed to the jar file on the command line for the given set of options.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/yuicompressor/jruby.rb', line 33

def command_arguments(options={})
  args = [ options[:line_break] ? options[:line_break].to_i : -1 ]

  if options[:type].to_s == 'js'
    args << !! options[:munge]
    args << false # verbose?
    args << !! options[:preserve_semicolons]
    args << ! options[:optimize] # disable optimizations?
  end

  args
end

.compress(stream_or_string, options = {}) ⇒ Object

Returns a compressed version of the given stream_or_string of code using the given options. When using this method directly, at least the :type option must be specified, and should be one of "css" or "js". See YUICompressor#compress_css and YUICompressor#compress_js for details about which options are acceptable for each type of compressor.

In addition to the standard options, this method also accepts a :java option that can be used to specify the location of the Java executable. This option will default to using "java" unless otherwise specified.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yuicompressor/jruby.rb', line 52

def compress(stream_or_string, options={})
  raise ArgumentError, 'Option :type required' unless options.key?(:type)

  stream = streamify(stream_or_string)
  output = StringIO.new

  reader = InputStreamReader.new(stream.to_inputstream)
  writer = OutputStreamWriter.new(output.to_outputstream)

  compressor = case options[:type].to_s
  when 'js'
    options = default_js_options.merge(options)
    JavaScriptCompressor.new(reader, ErrorReporter.new)
  when 'css'
    options = default_css_options.merge(options)
    CssCompressor.new(reader)
  else
    raise ArgumentError, 'Unknown resource type: %s' % options[:type]
  end

  compressor.compress(writer, *command_arguments(options))
  writer.flush

  output.rewind
  output.read
end

.compress_css(stream_or_string, options = {}, &block) ⇒ Object

Compress the given CSS stream_or_string using the given options. Options should be a Hash with any of the following keys:

:line_break

The maximum number of characters that may appear in a single line of compressed code. Defaults to no maximum length. If set to 0 each line will be the minimum length possible.

:charset

Charset. Default is utf8.



22
23
24
# File 'lib/yuicompressor.rb', line 22

def compress_css(stream_or_string, options={}, &block)
  compress(stream_or_string, options.merge(:type => 'css'), &block)
end

.compress_js(stream_or_string, options = {}, &block) ⇒ Object

Compress the given JavaScript stream_or_string using the given options. Options should be a Hash with any of the following keys:

:line_break

The maximum number of characters that may appear in a single line of compressed code. Defaults to no maximum length. If set to 0 each line will be the minimum length possible.

:munge

Should be true if the compressor should shorten local variable names when possible. Defaults to false.

:preserve_semicolons

Should be true if the compressor should preserve all semicolons in the code. Defaults to false.

:optimize

Should be true if the compressor should enable all micro optimizations. Defaults to true.

:charset

Charset. Default is utf8.



40
41
42
# File 'lib/yuicompressor.rb', line 40

def compress_js(stream_or_string, options={}, &block)
  compress(stream_or_string, options.merge(:type => 'js'), &block)
end

.default_css_optionsObject

:nodoc:



44
45
46
# File 'lib/yuicompressor.rb', line 44

def default_css_options #:nodoc:
  { :line_break => nil, :charset => 'utf8' }
end

.default_js_optionsObject

:nodoc:



48
49
50
51
52
53
54
55
# File 'lib/yuicompressor.rb', line 48

def default_js_options #:nodoc:
  { :line_break => nil,
    :charset => 'utf8',
    :munge => false,
    :preserve_semicolons => false,
    :optimize => true
  }
end

.jruby?Boolean

Returns true if the Ruby platform is JRuby.

Returns:

  • (Boolean)


10
11
12
# File 'lib/yuicompressor.rb', line 10

def jruby?
  !! (RUBY_PLATFORM =~ /java/)
end

.streamify(stream_or_string) ⇒ Object

:nodoc:



57
58
59
60
61
62
63
64
65
# File 'lib/yuicompressor.rb', line 57

def streamify(stream_or_string) #:nodoc:
  if IO === stream_or_string || StringIO === stream_or_string
    stream_or_string
  elsif String === stream_or_string
    StringIO.new(stream_or_string.to_s)
  else
    raise ArgumentError, 'Stream or string required'
  end
end

.stringify(stream_or_string) ⇒ Object

:nodoc:



67
68
69
70
71
72
73
# File 'lib/yuicompressor.rb', line 67

def stringify(stream_or_string) #:nodoc:
  case stream_or_string
    when IO then stream_or_string.read
    when String then stream_or_string
    else raise ArgumentError, 'Stream or string required'
  end
end