Building a custom GUI to configure a template

Google Closure

Google have made a very convenient Javascript compiler called Closure. Javascript compilers can reduce the size of Javascript to make it faster to send across the web and for browsers to load and parse the code. They are helpful for making web applications faster.

What’s kind of cool is that Google provide a RESTful API to Closure so it’s possible to actually invoke it right from within the Translator. This is the source to a simple module I wrote to invoke it:

closure ={}

local Url = 'http://closure-compiler.appspot.com/compile'

function closure.compile(Code)
   local P={
      js_code=Code,
      compilation_level='SIMPLE_OPTIMIZATIONS',
      output_format='text',
      output_info='compiled_code'
   }
   local Out = net.http.post{url=Url, 
         parameters=P, live=true}
   if Out:sub(1,5) == 'Error' then
      iguana.logWarning('Unable to compress Javacript code:\n'..Out)
      return Code   
   end
   return Out
end

Generally you don’t want to invoke the compiler in this manner too often online since Google will shut you out. So I set up the template module to only do the compilation if and when there is the production flag set and if the compilation fails then the code gracefully degrades to using the uncompressed javascript code. Still very neat that one can do this so easily using RESTful APIs.

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.