JS compressor of world fame.

Open demo

Latest blog entries tagged "uglifyjs"

/** May the source-map be with you! **/

UglifyJS — using the SpiderMonkey AST

The SpiderMonkey AST is a well documented and established AST structure for representing JavaScript programs. It grows increasingly popular and there are two solid JavaScript parsers written in JavaScript that produce it: Acorn and Esprima. I didn't actually know about it when I was designing UglifyJS v2, and even now I can see some technical advantages of my own AST, so I won't switch to the SpiderMonkey AST internally in the foreseeable future. However, UglifyJS can import a SpiderMonkey AST into its own format, and from there you can compress/mangle it or generate code as usual.


// for example, use Acorn to parse code:
acorn = require("acorn");
spidermonkey_ast = acorn.parse(source_code);
uglifyjs_ast = UglifyJS.AST_Node.from_mozilla_ast(spidermonkey_ast);

// further use uglifyjs_ast as if it were produced by UglifyJS.parse
ast = uglifyjs_ast.transform(compressor);
code = ast.print_to_string();

The AST_Node.from_mozilla_ast function takes a SpiderMonkey abstract syntax tree (usually a Program node, but you can pass incomplete trees as well) and produces the equivalent UglifyJS AST.

Note: importing does not currently support Mozilla-specific JavaScript features, such as the let keyword, multiple catch clauses etc. That's because UglifyJS itself doesn't support those.
Fork me on Github