cl-uglify-js – JavaScript compressor/beautifier for Common Lisp

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:

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.

Table of Contents

1 Dependencies

I only tested it on SBCL.


The following functions are exported:

2.1 ast-squeeze (ast &key (sequences T) (dead-code T))

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 (default T) — set this to NIL to disable grouping consecutive statements into a sequence using the “comma operator”.
  • dead-code (default T) — if you pass NIL 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.

2.2 ast-mangle (ast &key toplevel)

“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 (default NIL) — 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.

2.3 ast-gen-code (ast &key (beautify T) (indent-level 4) (indent-start 0) quote-keys)

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.

2.4 split-code (code &optional (maxlen (* 32 1024)))

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.

3 Compress one file

To compress one file, you would do something like this:

      (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.

4 License

Copyright 2010 (c) Mihai Bazon <>

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

Author: Mihai Bazon

Date: 2011-01-24 00:09:50 EET

HTML generated by org-mode 7.01trans in emacs 23