本文介绍了防止Google Closure Compiler重命名设置对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Google Closure Compiler在作为设置或数据传递给函数时不重命名对象。通过查看jQuery中的注释,我认为这样可行:

I'm trying to get the Google Closure Compiler to not rename objects when passed as settings or data to a function. By looking at the annotations present in jQuery, I thought this would work:

/** @param {Object.<string,*>} data */
window.hello = function(data) {
    alert(data.hello);
};
hello({ hello: "World" });

然而,最终结果如下:

window.a = function(b) {
  alert(b.a)
};
hello({a:"World"});

发现 ajax 函数有这个注释,它似乎工作。那么,为什么不呢?如果data是来自外部源或设置对象的返回值,我希望能够告诉编译器不要触摸它,使用 this [escape]

The ajax function found here has this annotation and it appears to work. So, why won't this? If data is the return value from an external source or a settings object I'd like to be able to tell the compiler to not touch it, using the this["escape"] trick is to intrusive for something like this in my opinion.

function ajax(success) {
      // do AJAX call
    $.ajax({ success: success });
}
ajax(function(data) {
    alert(data.Success);
});

输出:

$.b({c:function(a){alert(a.a)}});

成功已重命名为 c 成功(带有大写字母S)已重命名为 a

success has been renamed to c and Success (with a capital S) has been renamed to a.

我现在使用并获得以下输出:

I now compile the same code with the jQuery 1.6 externs file and get the following output:

$.ajax({success:function(a){alert(a.a)}});

它还会产生一个警告:物品成功没有像我期望的那样定义,但它不能将 Success 重命名为 a ,这仍然会破坏我的码。我查看 ajax 的注释,我发现此类型表达式 {Object。< string,*> =} ,我相应地注释了我的代码,然后重新编译。仍然没有工作...

It also produces a warning that the property Success is not defined, as I would expect, but it cannot rename Success to simply a, that will still break my code. I look at the annotation present for the ajax and I find this type expression {Object.<string,*>=}, I annotate my code accordingly, and recompile. Still not working...

推荐答案

既然你的焦点似乎是源而不是输出,那么你好像是什么重点关注干(不要重复自己)。这是另一种DRY解决方案。

Since your focus seems to be on the source rather than the output, it seems like what you're focused on is DRY (Don't Repeat Yourself). Here's an alternative DRY solution.

您可以使用 - create_name_map_files 运行Closure Compiler。这样做会发出名为 _props_map.out 的文件。你可以让你的JSON发射服务器端调用(ASP.Net MVC或它可能是什么)在发出它们的JSON时使用这些映射,因此它们实际上发出缩小的JSON,利用重命名Closure Compiler执行。这样,您可以在Controller和脚本上更改变量或属性的名称,添加更多等,并且缩小从脚本一直传回Controller控制器输出。您的所有来源,包括控制器,仍然是非缩小的,易于阅读。

You can run the Closure Compiler with --create_name_map_files. Doing so emits a file named _props_map.out. You can have your JSON-emitting server-side calls (ASP.Net MVC or whatever it might be) use these maps when emitting their JSON, so they're actually emitting minified JSON that leverages the renames the Closure Compiler performed. This way you can change the name of a variable or property on your Controller and your scripts, add more, etc, and the minification carries through from the scripts all the way back to the Controller output. All of your source, including the Controller, continues to be non-minified and easy to read.

这篇关于防止Google Closure Compiler重命名设置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 03:23
查看更多