Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
response = Net::HTTP.post_form(
URI.parse("http://closure-compiler.appspot.com/compile"),
{
:js_code => STDIN.read,
:compilation_level => ENV['TM_CLOSURE_COMPILATION_LEVEL'] || 'SIMPLE_OPTIMIZATIONS',
:output_info => ["compiled_code","statistics"],
:output_format => "json"
}
)
# parse json
d = JSON::parse(response.body)
# create new file
new_file = "#{File.basename(ENV['TM_FILEPATH'], '.js')}.compiled.js"
File.open(new_file, "w") do |f|
f.write d["compiledCode"]
end
# open the file
`mate #{new_file}`
# statistics
s = d["statistics"]
# tooltip values
t = {
:in => s['originalSize'],
:out => s['compressedSize'],
:ratio => "%0.2f%%" % (s['compressedSize']*100.0/s['originalSize'])
}
# tooltip display
%x{ "$DIALOG" tooltip --text "Input: #{t[:in]} bytes\nOutput: #{t[:out]} bytes\nCompression: #{t[:ratio]}" }
{ shellVariables = (
{ name = 'TM_CLOSURE_COMPILATION_LEVEL';
value = 'SIMPLE_OPTIMIZATIONS';
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.