In my opinion, this is the absolute best javascript compressor/obfuscator ever.. Thanks Mozilla Open Source.
AskApache Online Dojo Compressor
So how does this all work? And why should anyone choose this tool over the other ones that are available. The answer to both of those questions is in the design of the the Dojo compressor. Instead of brittle regular expressions, the Dojo compressor is based on Rhino, a JavaScript engine from the Mozilla project.
Being based on a real parse stream, the Dojo compressor can get a better idea for the context of a token (variable name, etc.) than the regular-expression based tools. This allows us to achieve the over-riding goal of a compressor that would be acceptable to the Dojo project: it must never mangle a public API.
API Safety
There are many “obfuscators” available in addition to size reduction tools. Over the years, many people have attempted to “encrypt” or otherwise obfuscate JavaScript sent over the wire to browsers, and it never pans out. Why not? For starters, JavaScript (as implemented in browsers) is completely interpreted. This means that any further compilation beyond source transformations will not work everywhere, and the tool provides a “decryption” tool along with the “encrypted” or obfuscated source, the unencrypted version will be available at runtime for anyone with a debugger to see. For those tools that just transform source code by mangling variable names, it’s even easier to revert their changes. Therefore, obfuscation and encryption aren’t useful goals. Size reduction, on the other hand, is a useful goal.
But not if your size-reduction tool breaks things. There are, of course, many increments available for the “compression” process. Potential choices available to a tool author include:
- removing comments
- collapsing line-beginning whitespace
- removing line-ending whitespace
- collapsing multiple blank lines
- removing all new-line characters
- removing whitespace around operators
- removing whitespace near/around curly braces
- replacing symbols with shorter names (this is how most “obfuscation” is done)
And the list goes on and gets ever-more esoteric as one tries to squeeze every last K out of a JavaScript file. But at some point, you can go too far. The Dojo
compressor attempts to strike a balance between debuggability (not replacing all symbols and not removing all newlines) and size reduction.
Javascript-compressor -online-compression -javascript -compression -dojo -compress