views:

73

answers:

2

Does anyone know of a JavaScript minifer that can detect unused code and strip it out.

Google Closure seems alright but it appears to only compact.

I'm looking for something much more advanced, where it can look through my JavaScript and not only compact but also remove unused code.

Anyone know of such a tool?

The use base being, I use very little functionality of JQuery - what I would like to do is remove from the base/core all the bloat that is unneeded from my use.

+1  A: 

Probably not.

The problem is that there's no guaranteed way to figure out what's used and what isn't. Javascript can be used/referenced from HTML, the script(s) could be used with other unknown scripts that use otherwise unused code and eval() blocks may use things you don't realize.

Minify and gzip it and that's enough. If not, cull it by hand (although getting rid of code is a lot harder than adding it in the first place).

cletus
+1  A: 

Closure Compiler should do the job, from the Closure FAQ

Closure Compiler helps reduce the size of your JavaScript by removing comments and unused code and shrinking the remaining code.

cxfx
I just did a simple test at http://closure-compiler.appspot.com/home and it does NOT appear that Closure is removing unused code :(
Henryk
Just having a dig through the closure-compiler code, is there some configuration setting that need activating called removeDeadCode?
cxfx
Certainly claims to remove dead code, "It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left", from http://code.google.com/closure/compiler/
cxfx
"Dead code" just means code that cannot be reached, such as the variable assignment in `return; var a = 1;` and you're unlikely to find any of that in jQuery.
NickFitz
You need to run the compiler in advanced mode to enable dead code removal. See http://code.google.com/closure/compiler/docs/api-tutorial3.html
Annie