開始使用 Closure Compiler 應用程式
透過集合功能整理內容 你可以依據偏好儲存及分類內容。
Closure 編譯器應用程式的 Hello World
Closure Compiler 應用程式是 Java 指令列公用程式,可壓縮、最佳化及尋找 JavaScript 中的錯誤。如要使用簡單的 JavaScript 程式試用 Closure Compiler 應用程式,請按照下列步驟操作。
如要完成這項練習,您需要 Java Runtime Environment 第 7 版。
-
下載 Closure 編譯器套件
建立名為 closure-compiler
的工作目錄。
從 Maven 存放區下載最新發布的 JAR 檔案,然後儲存至 closure-compiler
。
-
建立 JavaScript 檔案
建立名為 hello.js
的檔案,並在當中加入下列 JavaScript:
// A simple function. function hello(longName) { alert('Hello, ' + longName); } hello('New User');
將這個檔案儲存在 closure-compiler
目錄中。
-
編譯 JavaScript 檔案
從 closure-compiler
目錄執行下列指令:
java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js
這個指令會建立名為 hello-compiled.js
的新檔案,其中包含下列 JavaScript:
function hello(a){alert("Hello, "+a)}hello("New User");
請注意,編譯器已移除註解、空白字元和不必要的分號。編譯器也將參數名稱 longName
替換為較短的名稱 a
。這樣一來,產生的 JavaScript 檔案就會小得多。
如要確認編譯後的 JavaScript 程式碼仍可正常運作,請在 HTML 檔案中加入 hello-compiled.js
,如下所示:
<html> <head><title>Hello World</title></head> <body> <script src="hello-compiled.js"></script> </body> </html>
在瀏覽器中載入 HTML 檔案,您應該會看到友善的問候訊息!
後續步驟
這個範例只說明 Closure 編譯器執行的最簡單最佳化作業。如要進一步瞭解編譯器的功能,請參閱「進階編譯和外部函式」。
如要進一步瞭解 Closure 編譯器的其他標記和選項,請執行含有 --help
標記的 JAR:
java -jar compiler.jar --help
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[[["\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```"]]