- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题“Google Closure Compiler 如何处理引号(字符串文字)?”也可以改写为:
注意 1:这个问题不是关于为什么 Closure(或其他一些压缩器)选择更喜欢双引号(如 here 和 here 所问)。
注意 2:这个问题不是关于单引号和双引号的讨论,但是了解 GCC 对我们的代码做了什么(以及为什么)是相当有用的!
最佳答案
经常说(或问为什么)Google Closure Compiler (GCC) 用双引号替换单引号(即使 compilation_level
设置为 WHITESPACE_ONLY
!!!):
示例 xmp_1.js:
alert('Hello world!'); // output: alert("Hello world!");
然而……这只是“真相”的一半,因为:
示例 xmp_2.js:
alert('Hello "world"!'); // output: alert('Hello "world"!');
// *NOT*: alert("Hello \"world\"!");
GCC 本质上是一个“您的原始 javascript” 到“更小(更高效)的 javascript” 的翻译器:因此它不会“盲目地”用双引号替换单引号引号,但尝试选择“最佳引号字符”(毕竟.. 主要目标之一是“缩小”脚本)。
来自源代码 ( CompilerOptions.java ) 和 this issue-report人们可以了解到:
If the string contains more single quotes than double quotes then the compiler will wrap the string using double quotes and vice versa.
If the string contains no quotes or an equal number of single and double quotes, then the compiler will default to using double quotes.
像这个例子 xmp_3.js:
alert('Hello "w\'orld"!'); // output: alert('Hello "w\'orld"!');
alert('Hello "w\'o\'rld"!'); // alert("Hello \"w'o'rld\"!");
请注意上面的 xmp_3 如何导致使用 '
和 "
作为外引号的“混合”输出:最佳选择后跟默认值(当它没有没关系)。
事实证明,在一些严肃的合法现实世界案例中,默认使用单引号会更好。如 issue 836 中所述(自 2012 年 10 月 8 日起)以上引用:
The FT web app (app.ft.com) and the Economist app for Playbook deliver JavaScript updates to the client along with other resources by transmitting them as part of a JSON-encoded object. JSON uses double quotes natively, so all the double quotes in the compiled JavaScript need to be escaped. This inflates the size of the FT web app's JS by about 20kB when transmitting a large update.
该问题的记者带来了一份礼物:一个补丁,添加了选项 prefer_single_quotes
以将默认引号字符从双引号更改为单引号。
这个问题得到了足够的重视,以至于项目成员 Santos 考虑将默认的双引号更改为单引号(“看看是否有人提示”)。两次(也在记者/补丁贡献者表示他将其作为一个选项实现后,它不会产生任何向后兼容性后果,因为“有人可能出于某些奇怪的原因依赖用双引号输出的字符串”)。
然而,大约一周后补丁被接受 (r2258),又一周后重新设计 (r2257) 并且在 2012 年 10 月 30 日,Santos 报告说现在可以通过以下方式启用该选项:--formatting=SINGLE_QUOTES
(所以除了 PRETTY_PRINT
和 PRINT_INPUT_DELIMITER
之外的第三个选项用于 formatting
-key)。
(注意:在当前的源代码中,目前仍然可以找到对“prefer_single_quotes”的大量引用。)
用法:
如果您(下载并)使用(本地 java)应用程序:
java -jar compiler.jar --js xmp_1.js --formatting SINGLE_QUOTES
你会看到:alert('Hello world!');
现在编译为 alert('Hello world!');
但是,在撰写本文时,位于 http://closure-compiler.appspot.com 的编译器服务 API 和 UI(最有可能使用该 API) , 不要不接受这第三个(新的,虽然已经存在一年)格式化选项:SINGLE_QUOTES
并且会抛出一个错误:17:未知的格式选项 single_quotes。
(再次)挖掘源代码后,似乎(我不是 Java 专家)这是因为 jscomp/webservice/common/Protocol.java只接受旧的 PRETTY_PRINT
和 PRINT_INPUT_DELIMITER
* All the possible values for the FORMATTING key.
*/
public static enum FormattingKey implements ProtocolEnum {
PRETTY_PRINT("pretty_print"),
PRINT_INPUT_DELIMITER("print_input_delimiter"),
;
如果此选项在 API 和/或 UI 中可用,我将更新此答案。
希望这可以帮助并节省一些时间,因为谷歌可以找到关于 SINGLE_QUOTES
的唯一文档和引用目前在这一期 836 和源代码中的一些评论中。现在它对 SO 有一些解释(我期望它)。
关于javascript - Google Closure Compiler 如何处理引号(字符串文字)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19874862/
我在运行 compile test:compile it:compile经常并且...希望将击键次数减少到类似 *:compile 的数量。 .不过,它似乎不起作用。 $ sbt *:compile
有人可以给我这个问题的提示(或整个解决方案!): 在 Clojurescript 项目中,如何自动将编译日期/时间硬编码在符号中,以便在使用应用程序时显示? 谢谢。 最佳答案 有多种解决方案: 使用l
我是 ember.js 框架的新手,使用 ruby on rails 和 ember.debug.js -v 1.10.1(最新版本)。我一直在网上看到 ember 更改了这个最新的补丁,但我不知
我不是 Fortran 程序员(只是短暂的经验),但我需要编译一个部分用 F77 编写的程序。在我之前有人用 Absoft 编译器编译过它,但现在我需要在另一台机器上用 g77 重复这个过程。对于 A
我运行命令 mvn clean package 我得到了上面的错误我的 pom 是: http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
我有以下问题。 我想在测试编译阶段排除一些.java文件(** / jsfunit / *。java),另一方面,我想在编译阶段包括它们(id我使用tomcat启动tomcat:运行目标) ) 我的p
符合 wikipedia A compiler is a computer program (or set of programs) that transforms source code writt
我想构建项目,但出现如下错误: 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile
当我通过右键单击项目名称进行 Maven 安装时,出现以下错误: [INFO] Scanning for projects... [WARNING] [WARNING] Some proble
我是 Maven 的新手,我想将我的应用程序导入到 Maven。和以前一样,我想将我的 ejb 项目中的类引用到我的 war 项目中。我在类中没有错误,但是如果我在我的父项目上安装 maven,那么我
当我将 ASP.NET Web 应用程序部署到生产环境时,我使用配置转换来删除 debug="true"来自 .但是,就在今天,我注意到 web.config 中的另一个部分如下所示:
This question already has answers here: Maven Compilation Error: (use -source 7 or higher to enable
我正在使用 Maven 3.0.5 和 Spring Tool Source 3.2 并安装了 Maven 插件。当我尝试执行“运行方式---> Maven 安装”时,出现以下错误: [INFO] S
我试图用 AngularJS 创建我自己的递归指令,它调用自己以漂亮的 JSON 格式转换 View 中的对象。好吧,首先我使用 ng-include 调用带有模板的脚本,在其中使用 ng-if 验证
可以通过 @suppress annotation使用Google的Closure Compiler在每个文件的基础上禁止显示警告。但是,似乎无法同时抑制多个警告-例如globalThis和check
假设一个拥有 10 到 20 年经验的熟练开发人员从未构建过编译器或模拟器,哪一个会更具挑战性? 你能比较一下会成为障碍的问题吗? 谢谢。 最佳答案 仿真和编译是完全不同的,但由于两者都被认为是“低级
最近发现Vim中有一个命令叫compiler。您可以使用任何常见的编译器(例如,:compiler gcc、:compiler php 等)来调用它,但它似乎没有任何立竿见影的效果。 我在联机帮助页上
我试图从 spring.io 指南中部署最简单的应用程序 Guide 但是我有一些麻烦.. 我做了什么: 创建的项目。 (来自 spring.io 教程) 下载 heroku CLI 在 Intell
每当进行 Maven Build..>clean install 时,我都会遇到此错误。我尝试过使用不同版本的插件并添加 testFailureIgnore 属性,但问题仍然存在。请找到下面的 POM
我有一个 web 应用程序,我尝试使用 maven 进行编译,不幸的是,在执行 mvn clean package 时它不起作用。 stackoverflow 上有很多问题看起来都一样,但没有解决了我
我是一名优秀的程序员,十分优秀!