fredrik.eriksson

Coffee and a keyboard

Google Closure compiler for .Net (with IKVM)

google-closureI overheard someone asking if the Google closure compiler existed for .Net. So I will take this opportunity showing off one of my favorite project IKVM.Net, to convert the closure compiler so it runs on the .Net Framework or Mono.

Duke-GuitarIKVM.NET is a Java Virtual Machine for byte-code translation, verification and classloading for the .Net and Mono runtime. It is dependent upon the OpenJDK project for implementation of the JDK libraries. IKVM.NET can be used in two different modes: 

  • Dynamically: Java bytecode is translated on the fly into CIL
  • Statically: Java bytecode is compiled down to CIL and stored in an executable/DLL form

The main components of IKVM.NET are:

  • ikvm.exe: Starter executable comparable to java.exe (dynamic mode)
  • ikvmc.exe: Static compiler, used to compile java classes and jars into a .Net assembly (static mode)
  • ikvmstub.exe: A tool to generate stub class files from a .Net assembly
  • IKVM.Runtime.dll: The VM runtime and all supporting code
  • IKVM.OpenJDK.*.dll: The compiled version of the Java class libraries

To find more information about IKVM.NET go there homepage:

I will not go into much detail about what the Google closure compiler is; in short it is a JavaScript compiler that makes your JavaScript run and download faster. For more details visit the Closure compiler Labs site

Let’s get start by downloading and unpacking the closure compiler and ikvm.net:

For the rest of the steps I’m using PowerShell (any other shell will work equally well)

  1. Navigate to the folder where you unpacked the closure compiler
  2. Add ikvm.net’s bin directory to the environment path $env:PATH += ";" + "<ikvm_path> \bin"
  3. Statically compile the compiler.jar file: ikvmc -target:exe -fileversion:2010.06.16 .\compiler.jar
  4. Copy dependencies from the ikvm bin directory to the closure directory: IKVM.OpenJDK.Core.dll, .Jdbc.dll, .Misc.dll, .SwingAWT.dll, .Text.dll, .Util.dll, XML.API.dll and IKVM.Runtime.dll

We can now take our closure compiler for a test drive lets download jQuery and try to minify it. In fact the jQuery min version on there page has been minified by the closure compiler. This is how the jQuery Makefile call the closure compiler.

Converted to PowerShell it would look like this:

.\compiler.exe --js .\jquery-1.4.2.js --warning_level QUIET
| out-file -encoding ascii .\jquery.min.js

You could use the > command in power shell as well but the default encoding of the output file would be Unicode and therefore the file would be a lot bigger.

Now when we have a managed assembly it’s much easier to integrate the closure compiler into existing infrastructure or even use it in our own .Net application. Let’s take a look how to get started with integrating the closure compiler into a small .Net application:

Instead of targeting an exe with ikvmc this time we use the target library

ikvmc -target:library .\compiler.jar

The output will be a compiler.dll file that we can reference in our .Net application.

Let’s create small application that does the same thing as the PowerShell example above:

namespace Google.Closure.Compiler
{
    using System.IO;
    using System.Text;
    using com.google.javascript.jscomp;

    class Program
    {
        static void Main(string[] args)
        {
            var compiler = new Compiler();

            var options = new CompilerOptions();

            var dummy = JSSourceFile.fromCode("externs.js", "");
            var source = JSSourceFile.fromFile(@"jquery-1.4.2.js");

            var result = compiler.compile(dummy, source, options);

            File.WriteAllText("jquery-min.js",
                              compiler.toSource(),
                              Encoding.ASCII);
        }
    }
}

There are a couple of ways to use the compiler this is the simples I found. The compile method on the com.google.javascript.jscomp.Compiler class takes two source files the first is an extern js file (not used by me), the second is the input jquery js file and, the third parameter is the compiler options (I recommend taking a look at the compiler options class there are a lot of cool and useful options to play around with). After the compile method have executed we can do as I did get the resulting source output with the toSource() method or we can retrieve errors, warnings, or the graphviz dot file for the AST.

There are a lot more things that can be done with both the Google Closure Compiler and IKVM.NET but this should get you started.