简体   繁体   English

我可以告诉Closure编译器,仅针对特定类型,停止重命名属性吗?

[英]Can I tell the Closure compiler to, for specific types only, stop renaming properties?

This question follows: Why does Closure compiler rename properties of an extern type? 这个问题如下: 为什么Closure编译器重命名extern类型的属性? John's answer to that question brings up this second question. 约翰对这个问题的回答提出了第二个问题。

If I declare the extern type as suggested: 如果我按照建议声明extern类型:

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

then Javascript like: 那么Javascript就像:

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

will compile, as desired, to: 将根据需要编译为:

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

The properties are not renamed as usual. 这些属性不会像往常一样重命名。 Without the extern declaration, the compiled code would look like: 如果没有extern声明,编译后的代码将如下所示:

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

That's all good. 这一切都很好。 The problem is that the compiler has stopped renaming 'english' and 'spanish' in all places. 问题是编译器已经停止在所有地方重命名'english'和'spanish'。 Even if they are not on the extern type. 即使他们不是外部类型。

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

compiles to... 编译成......

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

Is there a way to stop this? 有办法阻止这个吗? If not, is there a reason for this behavior? 如果没有,是否有这种行为的原因?

I wanted to use extern types to handle things like JSON objects that originate from the server and do not have renamed properties. 我想使用extern类型来处理源自服务器但没有重命名属性的JSON对象。 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. 但是,如果每次我声明一个extern,我正在扼杀编译器重命名和缩小代码的能力,我会找到另一种方式。 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. 也许我将采用编译器生成的属性重命名映射( --property_map_output_file ),并在生成JSON响应时在服务器上使用它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM