How to use Google's Closure to compile JavaScript

后端 未结 6 1613
说谎
说谎 2021-02-05 14:56

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\".

6条回答
  •  情深已故
    2021-02-05 15:13

    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

提交回复
热议问题