HomeBlogUglifyJS — a JS parser/compressor/beautifier

UglifyJS — a JS parser/compressor/beautifier

... for NodeJS.

With minimal changes it should work on any JavaScript engine, but currently NodeJS is the main development platform.

It implements a JavaScript parser which produces a clean abstract syntax tree from JS code (this part is ported from parse-js, a great JS parser for Common Lisp).  Then it contains a few functions that manipulate the AST to compress variable names to single characters, and provide various other compression techniques such as:

  • join consecutive var declarations:

    var a = 10; var b = 20;  ==>  var a = 10, b = 20;
  • remove block brackets {} where possible

  • transform foo["bar"] into foo.bar

  • various optimizations for IF statements:

    • remove "else" where possible (when the last statement in an IF block is "return", "throw", "break" or "continue")

    • transform simple IF-s like:

      if (foo) bar(); else baz();  ==>  foo?bar():baz();
      if (!foo) bar(); else baz();  ==>  foo?baz():bar();
      if (foo) bar();  ==>  foo&&bar();
      if (!foo) bar();  ==>  foo||bar();

and some others.

It compresses better than the YUI compressor, and safer than Google Closure.  And it's a lot faster than any of these.

Get the code here: http://github.com/mishoo/UglifyJS and spread the word! :-)

          Comments

          • By: SveinungAug 20 (01:53) 2010Gr8 §

            Good job, seams to work mighty fine.

            Might be prettier to code with chaining tho, some sugar:
            UglifyJS(orig_code).ast_mangle().ast_squeeze().gen_code()

          • By: KevinSep 11 (10:33) 2010RE: UglifyJS — a JS parser/compressor/beautifier §

            Awesome work!

            Would there be much of a performance hit when running the compressed code if statements like `someVal===false&&run()` were reduced to `!someVal&&run()`?

          • By: Xuheki userSep 15 (11:10) 2010Xuheki §

            Sorry if this is unrelated to this post, but this is the only way I found to contact you on your website.

            I would like to ask if you are still developing Xuheki, or if you have plans to continue developing it.

            Xuheki is the only web-based email client I could find that can support multiple IMAP accounts, so it fits my needs perfectly. Its a shame your client is less known or publicized and it will be a waste if the development is not continued.

            You can contact me on my email I provider above.

            • By: mishooSep 15 (20:54) 2010RE: Xuheki §

              Glad to hear that there is one Xuheki user other than me. :-)

              Unfortunately I don't have the time to do more development, or even maintain it (it's been long since I last touched that code).  But I'm still using it every day as my personal email client, and for the time being it works pretty fine.

              If only somebody would sponsor the development...  I do have some ideas that I'd like to work on. :-.  But without financing I simply can't afford the time.

          • By: JasonOct 19 (18:19) 2010Contact §

            Hey, I am also trying to contact re: YMacs, but have been unable to find an email for you :P if you could contact me at the included email I have some code for you :)

            • By: mishooOct 19 (19:15) 2010RE: Contact §

              Hey Jason, that's good to know! :-.  Yeah, sure I'd like to see your code.

              My email address is linked somewhere in the Ymacs page, but probably not very visible (though it should be in the source code).  It is mihai.bazon@gmail.com -- but probably best if you post it on the Ymacs Google group -- http://groups.google.com/group/ymacs

              I'm planning to move Ymacs source code to GitHub soon, and that would hopefully make it easier to collaborate.

          • By: ZiggyFeb 11 (13:18) 2011RE: UglifyJS — a JS parser/compressor/beautifier §

            Firstly many thanks for this amazing compressor! I have been using the JS build tools workflow to combine+compress my JS libs:
            http://code.google.com/p/js-build-tools/
            Not sure if you're familiar with it, basically an ANT script for the YUIcompressor, works great in Textmate, one keyboard shortcut to run it. I was wondering if there's a way to use UglifyJS in this manner? Could it be run as a jar? Sorry I don't have a lot of experience with node. Thanks again

          • By: thornJun 03 (17:27) 2011RE: UglifyJS — a JS parser/compressor/beautifier §

            Don't you agree that the beatifier should produce a code like this:

            if (cond)
                statement1;
            else
                statement2;

            instead of

            if (cond) statement1; else statement2;

            ?

            The one-line variant looks beautiful only if statement1 and statement2 are short enough. Also it would be nice to have a switch between tabs and spaces indentation.

          • By: SabeehJul 06 (01:11) 2011Running UglifyJS from MS Windows command prompt §

            Hi Mihai,

            I've been testing out some Javascript compression tools, namely YUI and Closure and wanted to give UglifyJS a try.  However, I am running my tests on the Windows 7 platform from the command prompt.  Could you please tell me what steps I should follow as far as installation, command line arguments, etc., to successfully run UglifyJS from the command prompt in Windows (if this is even possible?)

            Sabeeh

          Page info
          Created:
          2010/08/15 23:18
          Modified:
          2010/08/18 11:12
          Author:
          Mihai Bazon
          Comments:
          12
          Tags:
          javascript, lexical closures, lisp, node.js, programming, v8
          See also