我可以告诉Closure编译器,仅针对特定类型,停止重命名属性吗? [英] Can I tell the Closure compiler to, for specific types only, stop renaming properties?

查看:67
本文介绍了我可以告诉Closure编译器,仅针对特定类型,停止重命名属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题如下:为什么Closure编译器是否重命名extern类型的属性? John对该问题的回答提出了第二个问题。

This question follows: Why does Closure compiler rename properties of an extern type? John's answer to that question brings up this second question.

如果我按照建议声明extern类型:

If I declare the extern type as suggested:

/** @interface */
function SpanishNoun() {}
/** @type {string} */
SpanishNoun.prototype.english;
/** @type {string} */
SpanishNoun.prototype.spanish;

然后Javascript如:

then Javascript like:

/**
 * @param {SpanishNoun} n 
 */
exp.foo = function (n) {
    console.log(n.english, n.spanish, n['english'], n['spanish']);  
}

将根据需要编译为:

function(a){console.log(a.english,a.spanish,a.english,a.spanish)};

这些属性不会像往常一样重命名。如果没有extern声明,编译后的代码将如下所示:

The properties are not renamed as usual. Without the extern declaration, the compiled code would look like:

function(a){console.log(a.a,a.c,a.english,a.spanish)

这一切都很好。问题是编译器已停止在所有位置重命名english和spanish。即使它们不是extern类型。

That's all good. The problem is that the compiler has stopped renaming 'english' and 'spanish' in all places. Even if they are not on the extern type.

/**
 * @param {AnotherType}  
 */
exp.bar = function (c) {
    c.other = c.english;
}

汇编为......

function(a){a.b=a.english};

有没有办法阻止这个?如果没有,是否有这种行为的原因?

Is there a way to stop this? If not, is there a reason for this behavior?

我想使用extern类型来处理源自服务器但没有重命名属性的JSON对象。但是,如果每次我声明一个extern,我正在扼杀编译器重命名和缩小代码的能力,我会找到另一种方式。也许我将采用编译器生成的属性重命名映射( - property_map_output_file ),并在生成JSON响应时在服务器上使用它。

I wanted to use extern types to handle things like JSON objects that originate from the server and do not have renamed properties. But if every time I declare an extern I'm eating away at the compiler's ability to rename and shrink the code, I will find another way. Perhaps I will take the property renaming map generated by the compiler (--property_map_output_file) and use it on the server when generating JSON responses.

推荐答案

Closure编译器可以根据类型重命名: https://github.com/google/closure-compiler/wiki/Type-Based-Property-Renaming
这增强了其他优化,例如内联和死代码删除也是如此。这在内部用于谷歌,但需要付出代价,因为如果你在你的类型声明中撒谎,它会引入一些硬调试方案。

The Closure Compiler can rename based on types: https://github.com/google/closure-compiler/wiki/Type-Based-Property-Renaming This enhances other optimizations such as inlining and dead code removal as well. This is used internally to Google but comes with a cost as it can introduce some hard debug scenarios if you "lie" in your type declarations.

这篇关于我可以告诉Closure编译器,仅针对特定类型,停止重命名属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆