Closure 编译器编译级别
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Closure 编译器提供三种级别的编译,从简单地移除空格和注释到积极地转换代码,应有尽有。
WHITESPACE_ONLY
WHITESPACE_ONLY
编译级别会从代码中移除注释,还会移除换行符、不必要的空格、多余的标点符号(例如圆括号和分号)以及其他空白字符。输出 JavaScript 在功能上与源 JavaScript 完全相同。
如果请求的输出语言模式与输入语言模式不同,仍会发生语言功能转译。
请参阅标志和选项中的 --language_in
和 --language_out
。
此编译级别提供的压缩程度是三个级别中最低的。
SIMPLE_OPTIMIZATIONS
SIMPLE_OPTIMIZATIONS
编译级别执行与 WHITESPACE_ONLY
相同的空白和注释移除操作,但它还会在表达式和函数内执行优化,包括将局部变量和函数参数重命名为更短的名称。将变量重命名为较短的名称可显著减小代码大小。由于 SIMPLE_OPTIMIZATIONS
级重命名仅适用于函数本地的符号,因此不会干扰已编译的 JavaScript 与其他 JavaScript 之间的互动。
使用 SIMPLE_OPTIMIZATIONS
进行编译始终会保留语法有效的 JavaScript 的功能,前提是代码不使用字符串名称访问局部变量(例如,通过使用 eval()
语句或通过对函数调用 toString)。
SIMPLE_OPTIMIZATIONS
是默认的编译级别。
ADVANCED_OPTIMIZATIONS
ADVANCED_OPTIMIZATIONS
编译级别执行的转换与 SIMPLE_OPTIMIZATIONS
相同,但添加了各种更激进的全局转换,以实现所有三个级别中最高的压缩率。ADVANCED_OPTIMIZATIONS
级压缩可将 JavaScript 压缩到其他工具无法达到的程度。
为了实现这种极高的压缩率,ADVANCED_OPTIMIZATIONS
对已编译的代码做出了强假设。如果您的代码不符合这些假设,ADVANCED_OPTIMIZATIONS
将生成无法运行的代码。
例如,使用 ADVANCED_OPTIMIZATIONS
编译的代码可能无法与未编译的代码搭配使用,除非您采取特殊步骤来确保互操作性。如果您未标记代码中引用的外部函数和属性,Closure 编译器会不恰当地重命名代码中的引用,导致代码中的名称与外部代码中的名称不匹配。
如需详细了解如何为 ADVANCED_OPTIMIZATIONS
准备代码,请参阅高级编译和外部声明。
ADVANCED_OPTIMIZATIONS
转换包括:
- 更激进的重命名:
使用 SIMPLE_OPTIMIZATIONS
进行编译只会重命名函数内的参数和变量。ADVANCED_OPTIMIZATIONS
还会重命名全局变量、函数名称和属性。
- 移除无用代码:
使用 ADVANCED_OPTIMIZATIONS
进行编译会移除可证明无法访问的代码。与大型库结合使用时,此功能特别有用。如果您仅使用大型库文件中的少数几个函数,编译器可以从其输出中移除除这些函数之外的所有内容。
- 全局内联:
使用 ADVANCED_OPTIMIZATIONS
进行编译会将某些函数调用替换为函数的正文。这种转换称为“内嵌”。编译器仅在确定内联安全且可节省空间时才会内联函数。使用 ADVANCED_OPTIMIZATIONS
进行编译时,如果编译器确定可以安全地内联常量和某些变量,也会这样做。
如何设置编译级别
如需在 Closure 编译器应用中设置编译级别,请添加命令行标志 --compilation_level
,并将其值设置为 WHITESPACE_ONLY
、SIMPLE
或 ADVANCED
,如以下命令所示:
java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js hello.js
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe Closure Compiler offers three compilation levels: \u003ccode\u003eWHITESPACE_ONLY\u003c/code\u003e, \u003ccode\u003eSIMPLE_OPTIMIZATIONS\u003c/code\u003e, and \u003ccode\u003eADVANCED_OPTIMIZATIONS\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eWHITESPACE_ONLY\u003c/code\u003e removes comments and unnecessary whitespace, while \u003ccode\u003eSIMPLE_OPTIMIZATIONS\u003c/code\u003e also renames local variables and function parameters for better compression.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eADVANCED_OPTIMIZATIONS\u003c/code\u003e provides the most aggressive code transformations and highest compression but requires careful code preparation to avoid runtime errors.\u003c/p\u003e\n"],["\u003cp\u003eUsers can choose the compilation level through the Closure Compiler service UI, service API, or application command line flags.\u003c/p\u003e\n"]]],[],null,["# Closure Compiler Compilation Levels\n\nThe Closure Compiler lets you choose from three levels of\ncompilation, ranging from simple removal of whitespace and comments\nto aggressive code transformations.\n\nWHITESPACE_ONLY\n---------------\n\n\nThe `WHITESPACE_ONLY` compilation level removes comments\nfrom your code and also removes line breaks, unnecessary spaces,\nextraneous punctuation (such as parentheses and semicolons), and other\nwhitespace. The output JavaScript is functionally identical to the source\nJavaScript.\n\n\nTranspilation of language features will still occur if the requested output\nlanguage mode is different from the input language mode.\nSee `--language_in` and `--language_out` in\n[Flags and Options](http://github.com/google/closure-compiler/wiki/Flags-and-Options).\n\n\nThis compilation level provides the least compression of the three\nlevels.\n\nSIMPLE_OPTIMIZATIONS\n--------------------\n\n\nThe `SIMPLE_OPTIMIZATIONS` compilation level performs\nthe same whitespace and comment removal\nas `WHITESPACE_ONLY`, but it also performs optimizations\nwithin expressions and functions, including renaming local\nvariables and function parameters to shorter names. Renaming\nvariables to shorter names makes code significantly\nsmaller. Because the `SIMPLE_OPTIMIZATIONS` level\nrenames only symbols that are local to functions, it does not\ninterfere with the interaction between the compiled JavaScript and\nother JavaScript.\n\n\nCompilation with `SIMPLE_OPTIMIZATIONS` always preserves\nthe functionality of syntactically valid JavaScript, provided that\nthe code does not access local variables using string names (by\nusing `eval()` statements, for example, or by calling\ntoString on functions).\n\n\n`SIMPLE_OPTIMIZATIONS` is the default compilation level.\n\nADVANCED_OPTIMIZATIONS\n----------------------\n\n\nThe `ADVANCED_OPTIMIZATIONS` compilation level performs\nthe same transformations as `SIMPLE_OPTIMIZATIONS`, but\nadds a variety of more aggressive global transformations to achieve\nthe highest compression of all three\nlevels. The `ADVANCED_OPTIMIZATIONS` level compresses\nJavaScript well beyond what is possible with other tools.\n\n\nTo enable this extreme\ncompression, `ADVANCED_OPTIMIZATIONS` makes strong\nassumptions about the compiled code. If your code does not conform\nto those assumptions, `ADVANCED_OPTIMIZATIONS` will\nproduce code that does not run.\n\n\nFor example, code compiled\nwith `ADVANCED_OPTIMIZATIONS` may not work with\nuncompiled code unless you take special steps to ensure\ninteroperability. If you do not flag external functions and\nproperties referenced in your code, Closure Compiler will\ninappropriately rename references in your code, causing mismatches\nbetween the names in your code and in the external code.\n\n\nTo learn more about how to prepare your code\nfor `ADVANCED_OPTIMIZATIONS`,\nread [Advanced Compilation and\nExterns](/closure/compiler/docs/api-tutorial3).\n\n\nThe `ADVANCED_OPTIMIZATIONS` transformations include:\n\n- *more aggressive renaming:*\n\n Compilation with `SIMPLE_OPTIMIZATIONS` only renames\n parameters and variables within\n functions. `ADVANCED_OPTIMIZATIONS` also renames\n global variables, function names, and properties.\n- *dead code removal:*\n\n\n Compilation with `ADVANCED_OPTIMIZATIONS` removes\n code that is provably unreachable. This is especially useful in\n combination with large libraries. If you use only a few\n functions from a large library file, the compiler can remove\n everything except those functions from its output.\n- *global inlining:*\n\n Compilation with `ADVANCED_OPTIMIZATIONS` replaces\n some function calls with the body of the function. This\n transformation is known\n as \"inlining\". The compiler only inlines functions when\n it determines that inlining is safe and saves space. Compilation\n with `ADVANCED_OPTIMIZATIONS` also inlines constants\n and some variables when the compiler determines that it can do\n so safely.\n\nHow to Set the Compilation Level\n--------------------------------\n\n\nTo set the compilation level in the Closure Compiler application,\ninclude the command line flag `--compilation_level` with\na value\nof `WHITESPACE_ONLY`, `SIMPLE`,\nor `ADVANCED`, as in the following command: \n\n```\njava -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js hello.js\n```"]]