【问题标题】:About YUI compressor and javascripts关于 YUI 压缩器和 javascripts
【发布时间】:2014-07-31 20:19:13
【问题描述】:

我在我的网站中使用车把。所以我需要将“handlebars.runtime.js”发送到客户端。但是我发现官方网站的'handlebars.runtime.js'没有被压缩,所以我使用YUI压缩器来压缩它。

当我检查压缩的“handlebars.runtime.js”时发生了一些奇怪的事情:与前一个相比,压缩后的代码中有些代码消失了。而压缩的一开始是这样的:

/*!

 handlebars v1.3.0

 Copyright (C) 2011 by Yehuda Katz

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.

 @license
 */
;var Handlebars=(function(){v (rest of the codes...)

如您所见,YUI 压缩器并未删除 cmets,并且代码的开头有一个奇怪的分号。谁能解释一下?谢谢!

PS:开头未压缩的'handlebars.runtime.js'是这样的:

/*!

handlebars v1.3.0

Copyright (C) 2011 by Yehuda Katz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

@license
*/
/* exported Handlebars */
var Handlebars = (function() {
// handlebars/safe-string.js
var __module3__ = (function() {
"use strict";
var __exports__;
// Build out our basic SafeString type
function SafeString(string) {
this.string = string;
}

 SafeString.prototype.toString = function() {
  return "" + this.string;
 };

  __exports__ = SafeString;
  return __exports__;
})();

【问题讨论】:

    标签: javascript yui-compressor


    【解决方案1】:

    由于开头评论/*!中的感叹号,它仍然存在

    这是有意的,通常在使用开源库时,您希望保留版权说明

    【讨论】:

    • 但是你能解释一下为什么一开始出现了一个奇怪的分号,并且与之前的相比,一些内容发生了变化吗? YUI 压缩器在压缩代码时会改变脚本的内容吗?
    • 是 YUI 压缩器优化内容以缩小文件大小。您可以使用命令行中的--disable-optimizations 选项禁用此功能。 (见yui.github.io/yuicompressor)。不完全确定额外的半列来自哪里,您可以编辑您的帖子并添加脚本的非压缩版本吗?
    • 请看已编辑的问题,我添加了未压缩问题的开头。
    • 我找不到任何相关文档,但我猜这是预期的行为,因此当有不可压缩的注释(由/*! 定义)时,添加半列以防止放置在该注释之前的任何代码都将与该注释之后的行合并。
    • 所以你的意思是分号对代码的功能没有影响?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多