Bắt đầu sử dụng ứng dụng Closure Compiler
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Ứng dụng Hello World của Trình biên dịch Closure
Ứng dụng Closure Compiler là một tiện ích dòng lệnh Java giúp nén, tối ưu hoá và tìm lỗi trong JavaScript của bạn. Để dùng thử ứng dụng Closure Compiler với một chương trình JavaScript đơn giản, hãy làm theo các bước bên dưới.
Để thực hiện bài tập này, bạn cần có Môi trường chạy Java phiên bản 7.
-
Tải gói Trình biên dịch Closure xuống
Tạo một thư mục đang hoạt động có tên là closure-compiler
.
Tải tệp JAR được phát hành gần đây nhất xuống từ kho lưu trữ Maven rồi lưu tệp đó vào closure-compiler
.
-
Tạo tệp JavaScript
Tạo một tệp có tên là hello.js
chứa JavaScript sau:
// A simple function.
function hello(longName) {
alert('Hello, ' + longName);
}
hello('New User');
Lưu tệp này trong thư mục closure-compiler
.
-
Biên dịch tệp JavaScript
Chạy lệnh sau từ thư mục closure-compiler
:
java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js
Lệnh này sẽ tạo một tệp mới có tên là hello-compiled.js
, chứa JavaScript sau đây:
function hello(a){alert("Hello, "+a)}hello("New User");
Xin lưu ý rằng trình biên dịch đã loại bỏ các nhận xét, khoảng trắng và dấu chấm phẩy không cần thiết. Trình biên dịch cũng đã thay thế tên tham số longName
bằng tên ngắn hơn là a
. Kết quả là một tệp JavaScript nhỏ hơn nhiều.
Để xác nhận rằng mã JavaScript đã biên dịch vẫn hoạt động chính xác, hãy thêm hello-compiled.js
vào một tệp HTML như tệp này:
<html>
<head><title>Hello World</title></head>
<body>
<script src="hello-compiled.js"></script>
</body>
</html>
Tải tệp HTML trong trình duyệt và bạn sẽ thấy một lời chào thân thiện!
Các bước tiếp theo
Ví dụ này chỉ minh hoạ những hoạt động tối ưu hoá đơn giản nhất do Trình biên dịch Closure thực hiện. Để tìm hiểu thêm về các chức năng của trình biên dịch, hãy đọc bài viết Biên dịch nâng cao và các phần bên ngoài.
Để tìm hiểu thêm về các cờ và lựa chọn khác cho Trình biên dịch Closure, hãy thực thi jar bằng cờ --help
:
java -jar compiler.jar --help
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[[["\u003cp\u003eThe Closure Compiler is a Java command-line tool used to compress, optimize, and debug JavaScript code.\u003c/p\u003e\n"],["\u003cp\u003eThis guide provides a basic example of using the Closure Compiler to minify a simple "Hello World" JavaScript function.\u003c/p\u003e\n"],["\u003cp\u003eThe compiler removes unnecessary elements like comments and whitespace, shortens variable names, and produces a smaller, more efficient JavaScript file.\u003c/p\u003e\n"],["\u003cp\u003eYou can confirm the functionality of the compiled code by including it in an HTML file and loading it in a browser.\u003c/p\u003e\n"]]],[],null,["# Getting Started with the Closure Compiler Application\n\nThe Hello World of the Closure Compiler Application\n---------------------------------------------------\n\n\nThe Closure Compiler application is a Java command-line utility that\ncompresses, optimizes, and looks for mistakes in your JavaScript. To\ntry out the Closure Compiler application with a simple JavaScript\nprogram, follow the steps below.\n\n\nTo work through this exercise you need the Java Runtime Environment\nversion 7.\n\n1. **Download the Closure Compiler package**\n\n\n Create a working directory called `closure-compiler`.\n\n\n Download the most recently released JAR file from the\n [Maven repository](https://mvnrepository.com/artifact/com.google.javascript/closure-compiler), and\n save it in `closure-compiler`.\n2. **Create a JavaScript file**\n\n\n Create a file named `hello.js` containing the following\n JavaScript: \n\n ```carbon\n // A simple function.\n function hello(longName) {\n alert('Hello, ' + longName);\n }\n hello('New User');\n ```\n\n\n Save this file in the `closure-compiler` directory.\n3. **Compile the JavaScript file**\n\n\n Run the following command from\n the `closure-compiler` directory: \n\n ```\n java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js\n ```\n\n\n This command creates a new file\n called `hello-compiled.js`, which contains the following\n JavaScript: \n\n ```text\n function hello(a){alert(\"Hello, \"+a)}hello(\"New User\");\n ```\n\n\n Note that the compiler has stripped comments, whitespace and an\n unnecessary semi-colon. The compiler has also replaced the parameter\n name `longName` with the shorter name `a`. The\n result is a much smaller JavaScript file.\n\n\n To confirm that the compiled JavaScript code still works correctly,\n include `hello-compiled.js` in an HTML file like this\n one: \n\n ```text\n \u003chtml\u003e\n \u003chead\u003e\u003ctitle\u003eHello World\u003c/title\u003e\u003c/head\u003e\n \u003cbody\u003e\n \u003cscript src=\"hello-compiled.js\"\u003e\u003c/script\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n ```\n\n\n Load the HTML file in a browser, and you should see a friendly greeting!\n\n### Next Steps\n\n\nThis example illustrates only the most simple optimizations\nperformed by the Closure Compiler. To learn more about the\ncompiler's capabilities, read [Advanced\nCompilation and Externs](/closure/compiler/docs/api-tutorial3).\n\n\nTo learn more about other flags and options for the Closure\nCompiler, execute the jar with the `--help` flag: \n\n```\njava -jar compiler.jar --help\n```"]]