如何使用Google的Closure编译JavaScript [英] How to use Google's Closure to compile JavaScript

查看:93
本文介绍了如何使用Google的Closure编译JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google刚刚发布了 Closure ,这是一个缩小JavaScript的编译器。

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

在产品网站上,它说Closure Compiler也已与Page Speed集成。

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

我如何使用使用Closure编译我的网页JavaScript的页面速度?

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

(或者,是否有一个网站,我可以简单地粘贴到我的JavaScript中以缩小它?

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

推荐答案

对于单个文件,它很简单

For a single file it's simple

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

对于多文件项目,您可以使用 calcdeps.py 编译器.jar

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

这样编译器可以提供有关类型错误等的有意义的信息。类型错误可以在编译时因为 compiler.jar 使用某些 JSDoc 注释类型信息。

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.

可以传递额外的编译器标志到 calcdeps.py 以及 -f - compiler_flags options

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

注意双引号和等号 - 必须在bash中使用该格式

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

这篇关于如何使用Google的Closure编译JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆