These pages are old. They apply to UglifyJS v2. Version 3 has evolved a lot in the mean time to support most of ES6. Please check the documentation in the official repository for up-to-date information. Big thanks to all contributors, especially to Alex Lam S.L., who has maintained this project for years!

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.

SYNOPSIS

// 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
uglifyjs_ast.figure_out_scope();
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.

UglifyJS

JS compressor of world fame.

Open demo

Latest blog entries tagged "uglifyjs"

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

Fork me on Github