Method: YUICompressor.compress

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

.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