tomb<blog>

musings from the dark side

Dec 11, 2009

Using the Google Closure Javascript Compiler with Coldfusion

Dec 11, 2009

It took me a couple of hours, but I was finally able to piece together enough of the Google closure snippets out there to create a functioning example of code so that you too can start testing the wonderful world of javascript compression.

<cfset strJSFilePath = ExpandPath("uncompiledscript.js") /> 
<cfset strJSFileOutPath = ExpandPath("compiled.js") /> 
 
<cffile action="read" file="#strJSFilePath#" variable = "js" /> 
 
<cfset compiler = createObject("java","com.google.javascript.jscomp.Compiler") /> 
<cfset instance = compiler.init() /> 
<cfset options = createObject("java","com.google.javascript.jscomp.CompilerOptions").init()  /> 
<cfset level = createObject("java","com.google.javascript.jscomp.CompilationLevel")  /> 
<!--- cfset level.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options)  / ---> 
<cfset level.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options)  /> 
<cfset jsfile = createObject("java","com.google.javascript.jscomp.JSSourceFile")  /> 
<cfset extern = jsfile.fromCode("externs.js", "function alert(x){};") /> 
<cfset jscript = jsfile.fromCode("input.js", "#js#") /> 
<cfset result = instance.compile(extern, jscript, options) /> 
 
<cfif result.success> 
<cffile action = "write" file = "#strJSFileOutPath#" output = "#instance.toSource()#" /> 
</cfif>

The advanced compilation mode is commented out if you want to try messing with that setting. I wasn't able to get my test script to work with that on but I'll keep trying. The documentation is sketchy out there right now, but I'm sure it'll improve once people get to play with these things a little more.

Remember to add the jar file to your Coldfusion library path if you want this example to work.

Code Coloring / Example / Documentation 

Comments

Write your comment:

(it will not be displayed)
Leave this field empty:

 
Powered by Mango Blog All Content and Media © Copyright MMX
Tomb Multimedia Productions Ltd
All Rights Reserved