常见问题解答
使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。
- 什么是 Closure Compiler?我为什么要使用它?
- Closure 编译器是一种可加快 JavaScript 下载和运行速度的工具。您可以使用 Closure Compiler 来缩减 JavaScript 文件的大小,并提高其效率。
- Closure Compiler 与现有的其他 JavaScript 压缩器有何不同?
-
一般来说,Closure Compiler 的压缩效果应达到或超过其他代码精简工具,并缩短 Web 应用的下载时间。此外,Closure 编译器还可以在开发期间(而不是测试期间)帮助您查找语法错误,并识别可能存在 bug 的代码模式。
在简单模式下,Closure 编译器应该能够比其他工具做得更好,因为它使用类似编译器的分析来寻找更多方法来最大限度地减小代码大小。例如,Closure Compiler 可以内嵌仅在少数情况下使用的函数、重用变量名称,以及预先计算常量表达式。
在高级模式下,Closure 编译器还可以使用您添加的类型注释来查找难以发现的 bug。
- Closure 编译器能否编译嵌入在 HTML 中的 JavaScript?
- 不能。Closure 编译器只能处理仅包含 JavaScript 的文件。
- 我可以将 Closure Compiler 与其他 JavaScript 缩小器一起使用吗?
-
可以。Closure Compiler 会读取任何有效的 JavaScript 并生成有效的 JavaScript,因此您可以在通过其他压缩器运行 JavaScript 文件之前或之后,将 Closure Compiler 应用于该文件。
请注意,Closure 编译器和其他缩小器可能对传入的代码有一定要求。例如,剥离注释的缩小器可能会移除其他工具所需的许可或注释信息。
- 如何调试 Closure 编译器生成的 JavaScript?
- 如果您的已编译代码抛出错误或表现出意外行为,您可以使用源映射来调试问题。源映射可在已编译的代码与原始源代码之间建立映射,以便浏览器开发者工具向您显示原始源代码,而不是已编译的代码。 如需让 Closure 编译器生成源映射,请在命令行中传递
--create_source_map
标志。例如: $ java -jar compiler.jar --js example.js --create_source_map ./example-map --js_output_file example-compiled.js
然后,如果您使用的是支持 Source Map 的浏览器(例如 Chrome 或 Firefox),则可以像对未编译的代码一样设置断点,并且浏览器的开发者工具会在原始源代码中显示相应的代码行。如需详细了解 Chrome 的开发者工具(包括有关源映射的信息),请参阅 JavaScript 调试。 - 编译器是否会在应用执行速度和下载代码大小之间做出权衡?
- 可以。任何优化编译器都会做出权衡。某些大小优化确实会带来较小的速度开销。不过,Closure Compiler 的开发者一直小心谨慎,避免引入显著的额外运行时。编译器的某些优化甚至可以缩短运行时(请参阅下一个问题)。
- 编译器是否会针对速度进行优化?
- 在大多数情况下,代码越小,速度越快,因为下载时间通常是 Web 应用中最重要的速度因素。减少冗余的优化还可以加快代码的运行时间。
- 可以编译的文件大小是否有限制?
- 编译 Web 服务的最大文件大小有限制,但独立编译器应用没有。
- Closure 编译器是否适用于所有平台?
- 编译器是用 Java 编写的,因此可以在任何运行 Java 的地方运行。
- 编译器是否可以处理任何合法的 JavaScript?
- 大多数情况下是这样。某些 JavaScript 构造(包括
eval()
和 with()
)可能会使编译器转换所依据的假设失效。 - 我需要了解多少 Web 开发知识才能使用 Closure 编译器?
- Closure 编译器是一款 JavaScript 开发工具,因此您需要了解如何使用 JavaScript 编程才能使用该编译器。不过,任何使用 JavaScript 的人都可以从使用 Closure 编译器中受益。
- Closure 编译器如何与 Closure 库搭配使用?
- Closure 编译器为使用 Closure 库的代码提供特殊检查和优化。此外,Closure 编译器服务还可以自动包含 Closure 库文件。在 Closure 中找到自己的方式介绍了用于声明所需 Closure 部分的语法。如需了解如何将 Closure 库与 API 搭配使用,请参阅 API 参考。如需将 Closure 库与 Closure Compiler 应用搭配使用,您必须先下载 Closure 库。默认情况下,编译器应用中已启用对 Closure 库的支持。
- 当使用
ADVANCED_OPTIMIZATIONS
进行编译时,我的代码停止工作或编译器产生错误。为什么? - 使用高级模式通常需要进行一些准备工作并更改代码。高级编译和外部声明介绍了如何确保代码与
ADVANCED_OPTIMIZATIONS
搭配使用。 - 为什么编译后的脚本中存在随机换行符?
- Closure 编译器有意每隔 500 个字符左右添加换行符。防火墙和代理有时会损坏或忽略包含很长行的 JavaScript 文件。每 500 个字符添加一个换行符即可避免此问题。移除换行符对脚本的语义没有影响。对代码大小的影响很小,并且编译器会优化换行符放置位置,以便在对文件进行 gzip 压缩时,代码大小惩罚更小。
- 我有一些版权声明或开源许可文本必须显示在我的源代码中。如何防止 Closure 编译器剥离此文本?
- Closure Compiler 支持 JSDoc
@license
标记。向任何 JSDoc 注释添加 @license
标记,以在编译器输出中保留该注释。如需了解详情,请参阅为 Closure 编译器添加 JavaScript 注释。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):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."]]