gitmyhub

UglifyJS

JavaScript ★ 13k updated 1y ago

JavaScript parser / mangler / compressor / beautifier toolkit

UglifyJS is a JavaScript toolkit that compresses source code into smaller files for faster web page loading, with a command-line tool, JavaScript API, source map support, and a beautifier.

JavaScriptNode.jsnpmsetup: easycomplexity 2/5

UglifyJS is a toolkit for processing JavaScript files. Its main use is minification: taking readable JavaScript source code and compressing it into a much smaller file that works the same way but takes less time to download. Smaller files matter because websites typically load JavaScript from a server, and large files slow that down.

The toolkit does several things. The parser reads JavaScript and builds an internal representation of the code. The compressor rewrites that representation to be more efficient, removing dead code, collapsing expressions, and applying various logic transformations. The mangler shortens variable and function names from descriptive words to single characters, which saves space without changing behavior. A beautifier does the opposite: it takes compressed or unformatted code and makes it readable again with proper indentation.

Both a command-line tool and a JavaScript API are provided. The command line takes one or more input files, runs the selected steps, and writes output to a file or to standard output. You can control exactly which features to apply with flags: compression, mangling, property name mangling, and beautification all have their own switches and sub-options. Source maps are supported, which allow browsers to map the compressed code back to the original source for debugging.

The tool supports most JavaScript syntax, including modern ECMAScript features. For very new or exotic syntax it recommends running a tool like Babel first to convert the code to something UglifyJS understands. The v3 API is not backwards compatible with the older v2 series.

Installation is a single npm command, either globally to use it from the terminal or locally to use it inside a project. The full README is longer than what was shown.

Where it fits