Google的js压缩与优化工具: Getting Started with the Closure Compiler Application
2012-03-14 02:02
676 查看
https://developers.google.com/closure/compiler/docs/gettingstarted_app
To work through this exercise you need the Java Runtime Environment version 6.
Download the Closure Compiler package
Create a working directory called
Download the Closure Compiler compiler.jar file and save it in
Create a JavaScript file
Create a file named
Save this file in the
Compile the JavaScript file
Run the following command from the
This command creates a new file called
Note that the compiler has stripped comments, whitespace and an unnecessary semi-colon. The compiler has also replaced the parameter name
To confirm that the compiled JavaScript code still works correctly, include
Load the HTML file in a browser, and you should see a friendly greeting!
To learn more about other flags and options for the Closure Compiler, execute the jar with the
Getting Started with the Closure Compiler Application
The Hello World of the Closure Compiler Application
The Closure Compiler application is a Java command-line utility that compresses, optimizes, and looks for mistakes in your JavaScript. To try out the Closure Compiler application with a simple JavaScript program, follow the steps below.To work through this exercise you need the Java Runtime Environment version 6.
Download the Closure Compiler package
Create a working directory called
closure-compiler.
Download the Closure Compiler compiler.jar file and save it in
closure-compiler.
Create a JavaScript file
Create a file named
hello.jscontaining the following JavaScript:
// A simple function. function hello(longName){ alert('Hello, '+ longName); } hello('New User');
Save this file in the
closure-compilerdirectory.
Compile the JavaScript file
Run the following command from the
closure-compilerdirectory:
java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js
This command creates a new file called
hello-compiled.js, which contains the following JavaScript:
function hello(a){alert("Hello, "+a)}hello("New User");
Note that the compiler has stripped comments, whitespace and an unnecessary semi-colon. The compiler has also replaced the parameter name
longNamewith the shorter name
a. The result is a much smaller JavaScript file.
To confirm that the compiled JavaScript code still works correctly, include
hello-compiled.jsin an HTML file like this one:
<html> <head><title>Hello World</title></head> <body> <scriptsrc="hello-compiled.js"></script> </body> </html>
Load the HTML file in a browser, and you should see a friendly greeting!
Next Steps
This example illustrates only the most simple optimizations performed by the Closure Compiler. To learn more about the compiler's capabilities, read Advanced Compilation and Externs.To learn more about other flags and options for the Closure Compiler, execute the jar with the
--helpflag:
java -jar compiler.jar --help
相关文章推荐
- js 压缩工具 google closure compiler
- 转:在项目中使用Google Closure Compiler(js压缩)
- Google Closure Compiler开源的javascript压缩加密工具
- Chapter 1. Getting Started with the Sample Application
- Java 开发者如何入门 Node.js 英文原文:Getting Started with Node.js for the Java Developer
- Google Closure Compiler开源的javascript压缩加密工具
- Getting Started with the LLVM System
- 《ZooKeeper》 Chapter 3 Getting Started with the ZooKeeper API
- 2 Getting Comfortable with Xcode 4 and the LLVM Compiler
- 使用Google的Closure Compiler,在本机上压缩javascript
- 在线的图片、js、css压缩优化工具介绍(配合小强视频 前端性能分析精要)
- Getting Started with Eclipse and the SWT
- Getting Started with the Helix Sources(II)
- Getting Started with the Lookup Service ----开始使用Lookup服务
- Get Started with the Google Fonts API
- Getting Started with the SWT
- 使用Google Closure Compiler高级压缩Javascript代码注意的几个地方
- Getting Started with AngularJS 1.5 and ES6: part1
- 码农干货系列【18】--getting started with Promise.js(总)
- Getting started with the MeeGo SDK for Linux