Shawn Miller

Use Google's Closure Compiler In 5 Lines Of C#

November 10, 20095:10 PM

The Google Closure Compiler is a tool for making JavaScript download and run faster. It is a true compiler for JavaScript. Instead of compiling from a source language to machine code, it compiles from JavaScript to better JavaScript. It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left.

The best part of the Closure Compiler is its REST API. Here's 5 lines of C# that will optimize your JavaScript:

string js = "alert('Hello World');";

using (var webClient = new WebClient())
{
   webClient.Headers.Add(
"content-type", "application/x-www-form-urlencoded");
   
var postData = String.Format("js_code={0}&output_info=compiled_code", HttpUtility.UrlEncode(js));
   
js = webClient.UploadString("http://closure-compiler.appspot.com/compile", postData);
}