简体   繁体   English

使用Google Closure清除未使用的功能

[英]Using google closure to strip out unused functions

This previous question asked how to eliminate dead code but didn't seem to answer the central question: find dead JavaScript code? 前面的问题问如何消除无效代码,但似乎没有回答核心问题: 找到无效的JavaScript代码?

I am trying to minify a set of 12 javascript files that include jquery and a couple of other libraries. 我试图缩小一组12个javascript文件,其中包括jquery和其他几个库。 I am confident that most of jquery is unused. 我相信大多数jquery都没有使用。

The current compile line is: 当前的编译行是:

java -jar compiler.jar --language_in ECMASCRIPT5 --compilation_level ADVANCED_OPTIMIZATIONS --js *.js --js_output_file liq-min.js

It reduces the size of the code from 240k to 176k, but since 90k is jquery, it should have been able to do a lot better. 它将代码的大小从240k减少到176k,但是由于90k是jquery,因此它应该能够做得更好。

Specifically for google closure, is there any way to define a set of initial function calls, and eliminate all functions that are not called? 专门针对Google闭包,有没有办法定义一组初始函数调用,并消除所有未调用的函数? Obviously eval and other string related issues mean we would have to manually set the list, but what is important is to strip out the bigger libraries being called. 显然,eval和其他与字符串有关的问题意味着我们必须手动设置列表,但重要的是要删除要调用的更大的库。

Also, closure shrinks local variable names but not global function names. 另外,闭包收缩局部变量名,但不收缩全局函数名。 Is there any way to do that as well? 有没有办法做到这一点? If necessary, I am prepared to add a string substitution pass at the end, but that is not ideal. 如果有必要,我准备在末尾添加一个字符串替换通道,但这并不理想。

In other words, is there any way to get closure to convert: 换句话说,有什么方法可以使闭包转换:

Calendar.foo()
function foobar() {...}

into 进入

C.f()
function f2() {...}

Given that I am entering all the code into the compiler, there is no need to worry about external references to these functions. 鉴于我将所有代码输入到编译器中,因此无需担心对这些函数的外部引用。 In other words, this is not a library, it is an application and the only function calls needed are those that are used. 换句话说,这不是一个库,它是一个应用程序,唯一需要的函数调用就是所使用的那些。

Oops, now I found this other answer: Use closure compiler to remove unused parts of jQuery 糟糕,现在我找到了另一个答案: 使用闭包编译器删除jQuery的未使用部分

It appears that jquery is not written to handle advanced optimization. 看来jquery并不是为处理高级优化而编写的。 I will take a quick peek at jquery but it's probably not worth working on. 我将快速浏览一下jquery,但可能不值得研究。

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

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