常見問題
透過集合功能整理內容 你可以依據偏好儲存及分類內容。
- 什麼是 Closure 編譯器?為什麼要使用這項功能?
- Closure 編譯器可加快 JavaScript 的下載和執行速度。您可以使用 Closure Compiler 縮減 JavaScript 檔案大小,並提升效率。
- Closure 編譯器與其他 JavaScript 壓縮器有何不同?
-
一般來說,Closure Compiler 應可達到或超過其他程式碼縮減工具的壓縮率,並縮短網頁應用程式的下載時間。此外,Closure 編譯器還可協助您在開發期間 (而非測試期間) 找出語法錯誤,並找出可能含有錯誤的程式碼模式。
在「簡單」模式下,Closure Compiler 應能比其他工具做得更好,因為它會使用類似編譯器的分析方式,找出其他可縮減程式碼大小的方法。舉例來說,Closure Compiler 可以內嵌只在少數情況下使用的函式、重複使用變數名稱,以及預先計算常數運算式。
在進階模式中,Closure Compiler 也可以使用您新增的型別註解,找出難以發現的錯誤。
- Closure Compiler 可以編譯嵌入在 HTML 中的 JavaScript 嗎?
- 否。Closure 編譯器只能處理僅含 JavaScript 的檔案。
- 我可以將 Closure 編譯器與其他 JavaScript 縮小器搭配使用嗎?
-
可以。Closure Compiler 會讀取任何有效的 JavaScript,並產生有效的 JavaScript,因此您可以在透過其他縮小器執行 JavaScript 檔案前後,將 Closure Compiler 套用至 JavaScript 檔案。
請注意,Closure 編譯器和其他壓縮工具可能會對傳入的程式碼有預期行為。如果縮小工具會移除註解,可能會一併移除其他工具所需的授權或註解資訊。
- 如何對 Closure 編譯器產生的 JavaScript 進行偵錯?
- 如果編譯後的程式碼會擲回錯誤或出現非預期行為,您可以使用來源對應來偵錯問題。來源對應會在編譯後的程式碼和原始碼之間建立對應,讓瀏覽器的開發人員工具顯示原始碼,而非編譯後的程式碼。如要讓 Closure Compiler 產生來源對應,請在指令列中傳遞
--create_source_map
標記。例如:$ java -jar compiler.jar --js example.js --create_source_map ./example-map --js_output_file example-compiled.js
接著,如果您使用支援來源對應的瀏覽器 (例如 Chrome 或 Firefox),可以像設定未編譯程式碼的中斷點一樣設定中斷點,瀏覽器的開發人員工具會顯示原始來源中對應的程式碼行。如要進一步瞭解 Chrome 開發人員工具,包括來源對應資訊,請參閱「偵錯 JavaScript」。 - 編譯器是否會在應用程式的執行速度和下載程式碼大小之間做出取捨?
- 可以。任何最佳化編譯器都會做出取捨。部分大小最佳化作業會造成些微速度負擔。不過,Closure Compiler 的開發人員已謹慎避免導入大量額外執行階段。編譯器的一些最佳化功能甚至會縮短執行階段 (請參閱下一個問題)。
- 編譯器是否會進行速度最佳化?
- 在大多數情況下,程式碼越小,速度就越快,因為在網頁應用程式中,下載時間通常是最重要的速度因素。減少多餘內容的最佳化作業也能加快程式碼的執行時間。
- 編譯的檔案大小是否有限制?
- 編譯網路服務有檔案大小上限,但獨立編譯器應用程式沒有。
- Closure Compiler 是否適用於所有平台?
- 編譯器是以 Java 撰寫,因此可在任何執行 Java 的位置執行。
- 編譯器可以處理任何合法的 JavaScript 嗎?
- 多數情況都是如此。部分 JavaScript 建構 (包括
eval()
和 with()
) 可能會使編譯器轉換所依據的假設失效。 - 使用 Closure 編譯器需要多少網頁開發知識?
- Closure 編譯器是 JavaScript 開發工具,因此您必須瞭解如何使用 JavaScript 程式設計,才能使用這個編譯器。不過,只要使用 JavaScript,就能從 Closure 編譯器獲益。
- Closure Compiler 如何與 Closure Library 搭配運作?
- Closure 編譯器會對使用 Closure 程式庫的程式碼進行特殊檢查和最佳化。此外,Closure Compiler 服務可以自動納入 Closure Library 檔案。尋找 Closure 的使用方式:說明如何宣告您需要的 Closure 部分。如要瞭解如何搭配使用 Closure 程式庫與 API,請參閱 API 參考資料。如要搭配使用 Closure Compiler 應用程式和 Closure Library,請先下載 Closure Library。編譯器應用程式預設會啟用 Closure 程式庫支援功能。
- 使用
ADVANCED_OPTIMIZATIONS
編譯時,我的程式碼停止運作或編譯器產生錯誤。為什麼? - 使用進階模式通常需要進行一些準備工作,並變更程式碼。進階編譯和外部函式說明如何確保程式碼可與
ADVANCED_OPTIMIZATIONS
搭配運作。 - 為什麼編譯後的指令碼會隨機換行?
- Closure 編譯器會刻意每隔 500 個字元左右就加入換行符。防火牆和 Proxy 有時會損毀或忽略含有極長行的 JavaScript 大型檔案。每 500 個半形字元就加入換行符,即可避免這個問題。移除換行符不會影響指令碼的語意。對程式碼大小的影響不大,而且編譯器會最佳化換行符位置,因此檔案經過 gzip 壓縮後,程式碼大小的影響會更小。
- 我的原始碼中必須顯示著作權聲明或開放原始碼授權文字。如何避免 Closure Compiler 移除這段文字?
- Closure Compiler 支援 JSDoc
@license
標記。在任何 JSDoc 註解中新增 @license
標記,即可在編譯器輸出內容中保留註解。詳情請參閱「為 Closure 編譯器註解 JavaScript」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","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-26 (世界標準時間)。"],[[["\u003cp\u003eThe Closure Compiler is a tool for making JavaScript download and run faster by reducing file size and improving efficiency.\u003c/p\u003e\n"],["\u003cp\u003eIt offers greater compression than standard minifiers and helps identify syntax errors and potential bugs during development.\u003c/p\u003e\n"],["\u003cp\u003eThe Closure Compiler works with any valid JavaScript and can be used alongside other minifiers for enhanced optimization.\u003c/p\u003e\n"],["\u003cp\u003eSource maps enable debugging of compiled code by providing a mapping back to the original source.\u003c/p\u003e\n"],["\u003cp\u003eWhile prioritizing download speed, the compiler also performs optimizations to enhance execution speed without significant runtime overhead.\u003c/p\u003e\n"]]],[],null,["- [What is the Closure Compiler? Why should I use it?](#what)\n- [How is the Closure Compiler different\n from the other JavaScript compressors that are out there?](#how-different)\n- [Can the Closure Compiler compile JavaScript that's embedded in HTML?](#tags)\n- [Can I use the Closure Compiler together with other JavaScript\n minifiers?](#other-minifiers)\n- [How can I debug the JavaScript that the Closure Compiler produces?](#sourcemaps)\n- [Does the compiler make any trade-off between my application's\n execution speed and download code size?](#tradeoffs)\n- [Does the compiler optimize for speed?](#speed)\n- [Are there any restrictions on the size of the files that can be compiled?](#size-restrictions)\n- [Is the Closure Compiler available for all platforms?](#platforms)\n- [Can the Compiler process any legal JavaScript?](#restrictions)\n- [How does the Closure Compiler work with\n the Closure Library?](#how-closure)\n- [My code stops working or the Compiler produces\n errors when I compile\n with `ADVANCED_OPTIMIZATIONS`. Why?](#advanced)\n- [Why are there random line feeds in\n compiled scripts?](#linefeeds)\n- [I have copyright notices or open source license\n text that must appear in my source code. How do I keep the Closure\n Compiler from stripping this text out?](#license)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\nWhat is the Closure Compiler? Why should I use it?\n:\n The Closure Compiler is a tool for making JavaScript download and\n run faster. You can use the Closure Compiler to reduce the size of\n your JavaScript files and to make them more efficient.\n\n\nHow is the Closure Compiler different from the other JavaScript\ncompressors that are out there?\n\n: Generally, the Closure Compiler should meet or exceed the compression\n of other code minification tools, and improve download times for your\n web application. In addition, the Closure Compiler can help you\n find syntax errors during development (rather than during testing)\n and identify potentially buggy code patterns.\n\n In Simple mode, Closure Compiler should be able to do better than\n other tools because it uses compiler-like analysis to find additional\n ways to minimize code size. For example, the Closure Compiler can\n inline functions that are used in only a few occurrences, reuse\n variable names, and pre-compute constant expressions.\n\n In Advanced mode, Closure Compiler can also use your added type\n annotations to find hard-to-spot bugs.\n\n\nCan the Closure Compiler compile JavaScript that's embedded in HTML?\n:\n No. The Closure Compiler works only on files that contain only JavaScript.\n\n\nCan I use the Closure Compiler together with other JavaScript\nminifiers?\n\n: Yes. Closure Compiler reads in any valid JavaScript and generates\n valid JavaScript, so you can apply the Closure Compiler to a\n JavaScript file either before or after you run the file through a\n different minifier.\n\n Remember that Closure Compiler and other minifiers might have\n expectations about the incoming code. A minifier that strips\n comments may remove licenses or annotation information needed by\n another tool, for example.\n\n\nHow can I debug the JavaScript that the Closure Compiler produces?\n:\n If your compiled code is throwing errors or exhibiting unexpected behavior,\n you can use Source Maps to debug the issue. A source map provides a mapping\n between the compiled code and the original source code so that the browser's\n developer tools can show you your original source code instead of the compiled code.\n To make the Closure Compiler produce a source map, pass the\n `--create_source_map` flag on the command line. For example: \n\n ```\n $ java -jar compiler.jar --js example.js --create_source_map ./example-map --js_output_file example-compiled.js\n ```\n\n Then, if you're using a browser that supports Source Maps (such as Chrome\n or Firefox), you can set breakpoints just as you would for uncompiled code,\n and the browser's developer tools will show the corresponding line of code\n in the original source. For more information about Chrome's developer tools, including\n information about source maps, see\n [Debugging JavaScript](https://developers.google.com/chrome-developer-tools/docs/javascript-debugging).\n\n\nDoes the compiler make any trade-off between my application's\nexecution speed and download code size?\n:\n Yes. Any optimizing compiler makes trade-offs. Some size\n optimizations do introduce small speed overheads. However, the\n Closure Compiler's developers have been careful not to introduce\n significant additional runtime. Some of the compiler's\n optimizations even decrease runtime (see next question).\n\n\nDoes the compiler optimize for speed?\n:\n In most cases smaller code is faster code, since download time is usually the most important speed factor in web applications. Optimizations that\n reduce redundancies speed up the run time of code as well.\n\n\nAre there any restrictions on the size of the files that can be compiled?\n:\n The compilation web service has a maximum file size, but the\n standalone compiler application does not.\n\n\nIs the Closure Compiler avalable for all platforms?\n:\n The compiler is written in Java, so it can run anywhere Java runs.\n\n\nCan the Compiler process any legal JavaScript?\n:\n Mostly. Some JavaScript constructs, including `eval()`\n and `with()`, can invalidate assumptions on which the\n compiler's transformations are based.\n\n\nHow much do I need to know about web development to use the\nClosure Compiler?\n:\n The Closure Compiler is a tool for JavaScript development, so you\n do need to know how to program in JavaScript to use the\n compiler. But anyone who uses JavaScript can benefit from using\n the Closure Compiler.\n\n\nHow does the Closure Compiler work with the Closure Library?\n:\n The Closure Compiler provides special checks and optimizations for\n code that uses the Closure Library. In addition, the Closure\n Compiler service can automatically include Closure Library\n files. [Finding\n Your Way around Closure](/closure/library/docs/introduction#deps) describes the syntax for declaring the\n parts of Closure that you need. See\n the [API\n reference](/closure/compiler/docs/api-ref#closure) for information on using the Closure Library with the\n API. To use the Closure Library with the Closure Compiler\n application you must\n first [download\n the Closure Library](https://github.com/google/closure-library). Support for the Closure Library is\n enabled in the compiler application by default.\n\n\nMy code stops working or the Compiler produces errors when I\ncompile with `ADVANCED_OPTIMIZATIONS`. Why?\n:\n Using Advanced mode usually requires some preparation and code\n changes. [Advanced Compilation\n and Externs](/closure/compiler/docs/api-tutorial3) explains how to make sure your code works\n with `ADVANCED_OPTIMIZATIONS`.\n\n\nWhy are there random line feeds in compiled scripts?\n:\n The Closure Compiler intentionally adds line breaks every 500\n characters or so. Firewalls and proxies sometimes corrupt or\n ignore large JavaScript files with very long lines. Adding line\n breaks every 500 characters prevents this problem. Removing the\n line breaks has no effect on a script's semantics. The impact on code size is small, and the Compiler\n optimizes line break placement so that the code size penalty is even smaller when files are gzipped.\n\n\nI have copyright notices or open source\nlicense text that must appear in my source code. How do I keep the\nClosure Compiler from stripping this text out?\n:\n Closure Compiler supports the JSDoc `@license` tag. Add the\n `@license` tag to any JSDoc comment to preserve the\n comment in the compiler\n output. See [Annotating\n JavaScript for the Closure Compiler](/closure/compiler/docs/js-for-compiler#tag-license) for more information."]]