javascript-如何minfy/混淆全局函数名称? [英] javascript - how to minfy/obfuscate global function names?

查看:59
本文介绍了javascript-如何minfy/混淆全局函数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些具有以下格式的代码:

I have some code that has the following format:

function myfunc1 () { ... jquery.bind('click', myfunc2) ... }
function myfunc2 () { ... }
...

是的,这些函数是全局的,但是没关系,因为我是在google chrome扩展程序内容脚本中编写的,因此它是沙盒的.

Yes, the functions are global, but it's ok since I'm writing within a google chrome extension content script, so it's sandboxed.

现在,我正在尝试最小化和混淆代码.我已经尝试过YUI Compressor和Google Closure编译器.问题是,我不知道如何缩小/混淆全局函数名称.使用YUI不会最小化全局变量,以防它们在外部被调用.在高级模式下,使用Closure似乎可以重命名全局变量,但是我在删除无效代码方面遇到了问题.大多数函数似乎已死机,因为它们依赖于DOM交互和事件处理,因此无法直接调用.

Now, I'm trying to minify and obfuscate the code. I've tried YUI Compressor and the Google Closure compiler. The problem is, I can't figure out how to minify/obfuscate the global function names. With YUI it doesn't minify the global variables in case they're called externally. With Closure in advanced mode, it seems like it can rename the global variables, however I'm having problem with the dead code removal. Most functions appear to be dead since they depend on DOM interaction and event handling and aren't called directly.

那么关于如何最小化这些全局变量的任何想法?我是否只需要编写脚本来进行一些正则表达式替换?如果更适合缩小模式,我也愿意重构我的代码(例如,添加到闭包之类的东西)

So any idea on how to minify these global variables? Do I need to just write a script to do some regex replacement? I'm also open to refactoring my code if that would fit the minification pattern better (for example, adding to a closure or whatnot)

推荐答案

缩小器不会改变公共/全局名称,因为对于许多脚本而言,它们会破坏公共API的可用性和可预测性.

Minifiers won't munge public/global names since, for many scripts, that will destroy the availability and predictability of the public API.

由于您不需要维护公共API,因此通过将它们包装在闭包函数中使其成为私有"就足够了:

Since you don't need to maintain a public API, making them "private" by wrapping them in a closure function may be enough:

(function () {

    function myfunc1 () { ... jquery.bind('click', myfunc2) ... }
    function myfunc2 () { ... 
    ...

})();

但是,即使那样,也不能保证,因为这完全取决于采矿者的作者.

But, even then, no guarantees as it's very much at the discretion of the minifier's authors.

这篇关于javascript-如何minfy/混淆全局函数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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