FFI Javascript minifier
Description
This library was created in order to provide a reasonably fast Javascript minifier on JRuby platform. Both native Java and Ruby implementations were found to be in several orders of magnitude slower. The only change from the original C implementation, is that it was changed to C++ (in order to make global variables become instance variables), and it works with buffers rather than stdin/stdout now.
Synopsis
require 'jsmin_ffi'
input = IO.read('prototype.js')
begin
output = JsminFFI.minify!(input)
File.open('output.js', 'w') {|f| f.write(output)}
rescue Jsmin::ParseError => e
$stderr.puts "Cannot minify: #{e}"
end
or use a native extension:
require ‘Jsmin’
input = IO.read(‘jquery.js’)
begin
output = Jsmin.minify(input)
File.open(‘output.js’, ‘w’) {|f| f.write(output)}
rescue Jsmin::ParseError => e
$stderr.puts “Cannot minify: #{e}”
end
Bugs
The memory allocated in the C++ code might not be freed when using FFI. It depends on how FFI handles the returned char*.
See also
For the original implementation, please see: http://www.crockford.com/javascript/jsmin.html