ADVANCED_OPTIMIZATION 无需重命名

发布于 2024-10-06 17:57:58 字数 224 浏览 6 评论 0原文

我正在 Google 闭包编译器的帮助下编写 Google Chrome 扩展。 我大量使用消息 API 在不同进程上运行的代码之间进行通信。这就是为什么我的文件需要单独编译。如果我使用高级优化,我还必须访问使用此消息 API 发送的数据中带引号的字符串的属性。这没有问题,但它让我的代码看起来很丑。但我喜欢通过高级优化来删除死代码。

我希望能够删除死代码,而无需进行高级优化带来的重命名。这可以通过闭包编译器实现吗?

I am writing a Google Chrome extension with the help of Google closure compiler.
I make heavy use of the message API to communicate between code that runs on different processes. Thats why my files need to be compiled separately. If I use advanced optimizations I also have to access properties with quoted strings in data I send with this message API. That works without a problem but it makes my code look ugly. But I like the dead code removal that comes with advanced optimizations.

I would like to be able to do dead code removal without the renaming that comes with advanced optimizations. Is this possible with the closure compiler?

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

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

发布评论

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

评论(3

|煩躁 2024-10-13 17:57:58

它是 不可能开箱即用,但是您可以下载源代码并自己用 Java 进行自定义。

It is not possible out of the box, You can however download the source and make the customization yourself in Java.

那小子欠揍 2024-10-13 17:57:58

是的,我同意。在处理传递数据的所有代码中必须使用 obj["prop"] 而不是 obj.prop ,这很丑陋,只是为了在中使用闭包编译器高级模式。

我开发的一个技巧是构建一个映射对象:

var mapping = {
  field1: "field1",
  field2: "field2"
    :
};

该对象在由闭包编译器编译后,会将字段名称损坏(重命名)映射到原始的未损坏的名称,例如:

var a = {
   b: "field1",
   c: "field2"
      :
};

然后在发送数据之前,我将其传递通过克隆整个数据结构的函数,在将 new 对象传递出去之前,将每个损坏的字段名称转换为新对象中未损坏的版本:

function cloneData(obj) {
    var newobj = {};
    foreach (var name in obj) {
        if (!obj.hasOwnProperty(name)) continue;
        var fullname = mapping[name] || name;
        newobj[fullname] = obj[name];
    }
    return newobj;
}

对于接收到的数据,执行相反的操作。

Yes, I agree. It is ugly to have to use obj["prop"] instead of obj.prop in all your code that deals with data being passed around just to use the Closure Compiler in Advanced mode.

One trick I have developed is to build a mapping object:

var mapping = {
  field1: "field1",
  field2: "field2"
    :
};

This object, after compiling by the Closure Compiler, will have field names mangled (renamed) mapping to the original, unmangled names, e.g:

var a = {
   b: "field1",
   c: "field2"
      :
};

Then before I send the data, I pass it through a function that clones the entire data structure, converting each mangled field name into the unmangled version in the new object before passing the new object out:

function cloneData(obj) {
    var newobj = {};
    foreach (var name in obj) {
        if (!obj.hasOwnProperty(name)) continue;
        var fullname = mapping[name] || name;
        newobj[fullname] = obj[name];
    }
    return newobj;
}

For data received, do the reverse.

你的呼吸 2024-10-13 17:57:58

在不知道您想要保留多少个的情况下,您可以查看文档的“导出您想要保留的符号”部分。

Without knowing how many you want to hold onto, you might look into the "Export Symbols You Want To Keep" section of the docs.

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