views:

1221

answers:

8

Google just released Closure, which is a compiler to minify JavaScript.

On the product site, it says "The Closure Compiler has also been integrated with Page Speed".

How do I use Page Speed to compile my web pages JavaScript with Closure?

(Or, is there a web site that I can simply paste in my JavaScript to have closure minify it?

+6  A: 

It looks like it's described here:

http://code.google.com/closure/compiler/docs/gettingstarted%5Fui.html

John Boker
A: 

As Jhon said, you can use Closure in this app http://closure-compiler.appspot.com/home. It will parse your script to a "better JavaScript". "It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left"

Zied
A: 

Google is great because they typically provide walkthroughs and tutorials of their "developer" software. Closure is no exception. Start here and follow the links to the tool that you want to learn about. Then, just read and follow along. They have a ton of information about each tool. Enjoy!

JasCav
A: 

"Page Speed 1.4 Beta integrates the Closure Compiler to minify JavaScript files automatically. However, you will need to download and install the Page Speed Beta and Closure Compiler separately."

http://code.google.com/speed/page-speed/download.html

I haven't installed this version yet, but I'm fairly certain that Page Speed will present you with compiled code in its optimization recommendations.

Scott Johnson
+2  A: 

For a single file it's simple

java -jar $path_to_jar/compiler.jar --js input_file.js \
          --js_output_file output_file.js

For a multi-file project you can use calcdeps.py in combination with the compiler.jar

#!/bin/sh$
$CALCDEPS_PATH=/path/to_calcdeps  #directory containing calcdeps.py
$JAR_PATH=/path/to_jar            #directory containing compiler.jar
$CLOSURE_PATH=/path/to_closure    #contains directory "closure"
$CALCDEPS_PATH/calcdeps.py --path $CLOSURE_PATH \
                           --path . \
                           --compiler_jar $JAR_PATH/compiler.jar \
                           --input main_project_file.js \
                           --output_mode compiled \
                           > compiled_project_file.js

That way compiler gives meaningful information about type errors, etc. Type errors can be caught at compile time because compiler.jar uses certain JSDoc comments for type information.

Extra compiler flags can be passed to calcdeps.py along with -f or --compiler_flags options

If you want to use advanced optimizations set

--compiler_flags "--compilation_level=ADVANCED_OPTIMIZATIONS"

notice the double quotes and the equal sign - had to use that format in bash

Evgeny
A: 

It seems that Closure Compiler is integrated with Page Speed only for Windows.

Daniel Gonzalez Gasull
A: 

Use the closure compiler with PHP (hosted via CURL or local via command line tool)

http://bohuco.net/blog/2009/11/google-closure-compiler-with-php/

DerFichtl
A: 

If anyone wants to use closure library with their own code or use soy templates I recommend this article which comes with a template project that can be easily modified to get you going really quickly.

gatapia