Reputation: 8499
I'm looking for a tool to compress my JS files.
What I want is not just simply remove unneccessary characters; I prefer to tool to be able to reduce the lengths of local variables in functions also.
Even better I the tool can reduce the lengths of all other stuff like global variables, function names, etc. This is to protect the code from being used by web viewers without our consent.
For example, the tool should rename a variable or function name from "some_variable", "some_function" to "sv", "sf", etc. And then refactoring all related references.
I've tried to dig the net but couldn't find something similar.
Upvotes: 1
Views: 202
Reputation: 1075457
The Google Closure Compiler does this. It has various settings, but even the "simple optimizations" setting will shorten variable names and (in cases where it knows the function will never be called outside the script) function names. It will even inline functions when there's a savings to be had.
For example, given this script:
jQuery(function($) {
var allDivsOnThePage = $("div"),
someParagraphsAsWell = $("p");
setColor(allDivsOnThePage, "blue");
setColor(someParagraphsAsWell, "green");
function setColor(elms, color) {
return elms.css("color", color);
}
});
Using the closure compiler with simple optimizations (and telling it we're using jQuery) yields:
jQuery(function(a){var b=a("div"),a=a("p");b.css("color","blue");a.css("color","green")});
Note how it's not only shortened the identifiers, but reused one where it detected it could (in this case that didn't save us anything, but in some other cases it could), and inlined the setColor
function since that resulted in a savings.
Upvotes: 1
Reputation: 145458
Try YUI Compressor:
http://developer.yahoo.com/yui/compressor/
For me it seems to be one of the most powerful at the moment.
Upvotes: 1
Reputation: 3967
I think that this one can help you : http://www.minifyjavascript.com/
I've used it in the past and it does a good job!
Upvotes: 2