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

发布于 2024-12-17 03:05:33 字数 1282 浏览 3 评论 0原文

这个问题如下: 为什么闭包编译器重命名外部类型的属性? 约翰对该问题的回答提出了第二个问题。

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

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

那么 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 声明,编译后的代码将如下所示:

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

没关系。问题是编译器已停止在所有位置重命名“english”和“spanish”。即使它们不属于外部类型。

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

编译为...

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

有办法阻止这个吗?如果不是,这种行为有原因吗?

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

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

If I declare the extern type as suggested:

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

then Javascript like:

/**
 * @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:

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. 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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2024-12-24 03:05:33

闭包编译器可以根据类型重命名: https://github .com/google/closure-compiler/wiki/Type-Based-Property-Renaming
这也增强了其他优化,例如内联和死代码删除。这是 Google 内部使用的,但会带来一定的成本,因为如果您在类型声明中“撒谎”,它可能会引入一些困难的调试场景。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文