explaingit

mishoo/uglifyjs

13,393JavaScriptAudience · developerComplexity · 2/5Setup · easy

TLDR

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.

Mindmap

mindmap
  root((UglifyJS))
    What it does
      Minification
      Mangling
      Beautification
    Features
      Source maps
      Dead code removal
      CLI and API
    Tech
      JavaScript
      Node.js
      npm
    Use cases
      Web deploy
      Build pipelines
      Code obfuscation
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Shrink JavaScript files before deploying a website to speed up load times

USE CASE 2

Obfuscate source code by shortening variable and function names using the mangler

USE CASE 3

Generate source maps so compressed code can be debugged in the browser's devtools

USE CASE 4

Integrate minification into a build pipeline via the JavaScript API

Tech stack

JavaScriptNode.jsnpm

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

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.

Copy-paste prompts

Prompt 1
Show me how to use UglifyJS from the command line to compress and mangle app.js and output app.min.js with a source map
Prompt 2
Write a Node.js script that uses the UglifyJS JavaScript API to compress multiple input files and merge them into one minified bundle
Prompt 3
How do I configure UglifyJS to remove all console.log statements from my code during the compress step?
Prompt 4
What UglifyJS flags do I need to enable property name mangling while keeping my public API intact?
Prompt 5
How do I use UglifyJS to beautify a minified file back into readable, indented code?
Open on GitHub → Explain another repo

← mishoo on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.