GithubHelp home page GithubHelp logo

benjamn / cl-uglify-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mishoo/cl-uglify-js

1.0 2.0 0.0 189 KB

JavaScript compressor/beautifier library for Common Lisp

Shell 0.66% Common Lisp 99.34%

cl-uglify-js's Introduction

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:

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

Dependencies

I only tested it on SBCL.

API

The following functions are exported:

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.

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.

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.

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.

Compress one file

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.

License

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.