This is a Common Lisp version of UglifyJS. It works on data produced by parse-js to generate a “minified” version of the code. Currently it can:
- reduce variable names (usually to single letters)
- join consecutive var statements
- resolve simple binary expressions
- group most consecutive statements using the “sequence” operator (comma)
- remove unnecessary blocks
- convert IF expressions in various ways that result in smaller code
- remove some unreachable code
It’s faster than YUI Compressor, Google Closure and UglifyJS, and almost as good as (and a bit safer than) Google Closure in terms of compressed code size.
See the UglifyJS page for more information.
I only tested it on SBCL.
The following functions are exported:
Applies various compression techniques. It expects an AST (as returned by parse-js) and returns a new, compatible AST (possibly sharing structure with the original one!).
Optional keyword arguments:
sequences
(defaultT
) — set this toNIL
to disable grouping consecutive statements into a sequence using the “comma operator”.dead-code
(defaultT
) — if you passNIL
it will not attempt to remove unreachable code.
When it encounters unreachable code and dead-code
is true, this function
will warn
about it. The warnings go to *error-output*
, so rebind that
if you want to catch the messages.
“Mangles” variable names (renames all variables to shorter version). This function is careful not to affect the semantics of the code. It will avoid renaming undeclared variables (which could possibly be defined in some other script), and avoid renaming names that are under the influence of a with block, or within the context of an eval call.
Optional keyword arguments:
toplevel
(defaultNIL
) — pass true here if you want to mangle the toplevel scope. By default we don’t.
Note that this function returns a somewhat incompatible AST. Literal names
(which are normally strings) are replaced with lambda-s that would return
the mangled version. ast-gen-code
knows how to deal with this.
Given an abstract syntax tree, this function returns the corresponding JavaScript code (as a string).
Optional keyword arguments:
beautify
(default true). By default ast-gen-code returns nicely indented code. If you want to compress, pass:beautify NIL
.
The other arguments only make sense if beautify
is true:
indent-level
: number of spaces to use for indentation. Note that case/default statements are indented to half of this number, so better pass an even one.indent-start
: the whole code will be indented by this number of spaces (default 0).quote-keys
: by default, we only quote keys that cannot be used otherwise (i.e. reserved words such as “while”). Pass this true to quote all keys regardless.
Given `code’ (a string) it will split it by adding a newline every `maxlen’ (or so) characters. I found both Firefox and Chrome croak with weird errors when the whole code was on a single 680K line, so even if it adds a few more bytes this step should be done for safety. The default `maxlen’ is 32K.
To compress one file, you would do something like this:
(ast-gen-code
(ast-mangle
(ast-squeeze
(with-open-file (in "/path/to/file.js")
(parse-js:parse-js in)))) :beautify nil)
Some simplified API will be available at some point, though I’m not sure it would be really useful.
Copyright 2010 (c) Mihai Bazon <[email protected]> This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.