Cory’s Ruby YUI Compressor v 2.0 Simpler, shorter.
So, a while ago I write a Ruby YUI Compressor Wrapper which is still available and works great (although it could use a newer jar file) as far as I know from Mandula's Blog. Some other guy also wrote one that pretty much had the same functionality as my gem. He must have hated it or something, I dunno. Even another guy wrote a wrapper for Closure Compiler if thats your bag.
Anywho, on a new project I'm working on *I just need a inline YUI compressor* and I dont give a damn about anything else like bundling, globbing or whatever. So I ended up throwing together this little (~50 lines less comments) Ruby YUI Compressor to do my dirty work.
I won't make it a gem probably, because if I do someone will just make a ruby-yui-simpler-also gem and put it on digg and get a bigger following
(</sarcasm>)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 78 79 80 81 82 83 84 85 86 87 | require 'open3' require 'stringio' class YUI include Open3 JAR_PATH = File.join(File.dirname(__FILE__),'yuicompressor-2.4.2.jar') class << self # @param io [String|File] # @param options [Hash] - See YUI.cli # @returns String # def compress(io, options = {}) options[:type] ||= :js options[:charset] ||= "utf-8" stdin, stdout, stderr = Open3.popen3( YUI.cli(options) ) stdin.puts streamify(io).read stdin.close my_stderr = stderr.read my_stdout = stdout.read # if compression failed, just output original IO if my_stderr.empty? return my_stdout else raise Exception, my_stderr end end # If any exceptions are raised, the original string will be # returned incase you are compressing stuff during a deploy # and _just_dont_care_ def compress_safe(io, options={}) YUI.compress(io, options) rescue Exception => ex puts ex.message return streamify(io).read end # Sets up the command line options # @param options [Hash] # :jar_path - [String] Path to the jar file, default "./yuicompress-2.4.2.jar" # :charset - [String] default 'utf-8' # :type - [Symbol] Options: js/css, default :js # :line_break - [Fixnum] Column to add line break at # # if the type of compression is JS, then additional options: # :nomunge - [Boolean] do not obfuscate # :preserve_semi - [Boolean] preserve all semicolons # :disable_opt - [Boolean] Disable all micro optimizations # # @returns String # def cli(options) _cmd = ["java -jar #{options[:jar_path] || JAR_PATH}"] _cmd << "--type #{options[:type]}" _cmd << "--charset #{options[:charset]}" if options[:charset] _cmd << "--charset #{options[:line_break]}" if options[:line_break] if options[:type] == :js _cmd << "--nomunge" if options[:nomunge] _cmd << "--preserve-semi" if options[:preserve_semi] _cmd << "--disable-optimizations" if options[:disable_opt] end _cmd.join(' ') end # If a file or a string, treat it like its a stream # # @param string_or_stream [File|String] # @return StringIO # def streamify(string_or_stream) if string_or_stream.respond_to?(:read) string_or_stream.rewind if string_or_stream.eof? string_or_stream else StringIO.new(string_or_stream) end end end #end class << self end |
Wanna use it?
1. Copy the code above, throw it in your lib folder or where ever
2. Download the YUI compressor JAR file and put it in the same directory.
3. Do something like the following...
1 2 3 | YUI.compress("function weebleep(){var cool_variable= 3; return cool_variable;}") #=> "function weebleep(){var a=3;return a};" YUI.compress(File.open( "public/javascripts/application.js")) #=> A bunch of compressed javascript YUI.compress(File.open( "public/stylesheets/application.css"), {:type => :css}) #=> Compressed stylesheet |
Other options include:
:jar_path - [String] Path to the jar file, default "./yuicompress-2.4.2.jar"
:charset - [String] default 'utf-8'
:type - [Symbol] Options: js/css, default :js
:line_break - [Fixnum] Column to add line break at
if the type of compression is JS, then additional options:
:nomunge - [Boolean] do not obfuscate
:preserve_semi - [Boolean] preserve all semicolons
:disable_opt - [Boolean] Disable all micro optimizations
Yay have a blast. Its another compression wrapper.