javascript - 如何 minfy/混淆全局函数名称?
我有一些具有以下格式的代码:
function myfunc1 () { ... jquery.bind('click', myfunc2) ... }
function myfunc2 () { ... }
...
是的,这些函数是全局的,但没关系,因为我是在 google chrome 扩展内容脚本中编写的,所以它是沙箱的。
现在,我正在尝试缩小和混淆代码。我尝试过 YUI Compressor 和 Google Closure 编译器。问题是,我不知道如何缩小/混淆全局函数名称。对于 YUI,它不会缩小全局变量,以防它们被外部调用。在高级模式下使用 Closure 时,它似乎可以重命名全局变量,但是我在删除死代码时遇到了问题。大多数函数似乎已经死了,因为它们依赖于 DOM 交互和事件处理并且不直接调用。
那么关于如何缩小这些全局变量有什么想法吗?我是否需要编写一个脚本来进行一些正则表达式替换?如果这能更好地适应缩小模式,我也愿意重构我的代码(例如,添加到闭包或诸如此类的东西)
I have some code that has the following format:
function myfunc1 () { ... jquery.bind('click', myfunc2) ... }
function myfunc2 () { ... }
...
Yes, the functions are global, but it's ok since I'm writing within a google chrome extension content script, so it's sandboxed.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在代码中对常用的 jQuery 函数进行本地引用,然后将其缩小?
例如:
You could make local references to commonly used jQuery functions in your code, which would then be minified?
For example:
缩小器不会修改公共/全局名称,因为对于许多脚本来说,这会破坏公共 API 的可用性和可预测性。
由于您不需要维护公共 API,因此通过将它们包装在闭包函数中来使它们成为“私有”可能就足够了:
但是,即使这样,也不能保证,因为这在很大程度上取决于压缩器作者的判断。
Minifiers won't munge public/global names since, for many scripts, that will destroy the availability and predictability of the public API.
Since you don't need to maintain a public API, making them "private" by wrapping them in a closure function may be enough:
But, even then, no guarantees as it's very much at the discretion of the minifier's authors.
请参阅语义设计 JavaScript 混淆器。让您可以完全控制哪些符号被混淆,哪些符号未被混淆,这样您就可以管理此类情况。无需更改您的工作代码。
我为他们工作。
See the Semantic Designs JavaScript Obfuscator. Gives you complete control over what symbols are obfuscated, and which are not, precisely so you can manage situations like this. No need to change your working code.
I work for them.
请参阅闭包编译器的在线文档。
换句话说:
Please see the on-line docs for the Closure Compiler.
In other words: