UglifyJS provides a comprehensive command-line interface for processing JavaScript files with full configuration support for minification, compression, and output formatting.
uglifyjs [input files] [options]Process JavaScript files from command line with various transformation options.
Usage Examples:
# Basic minification
uglifyjs input.js -o output.js
# Multiple input files
uglifyjs file1.js file2.js -o combined.js
# Pipe from stdin
cat input.js | uglifyjs -o output.js
# Read from stdin, output to stdout
echo "function test() { return 42; }" | uglifyjs-h, --help # Print usage information
--help options # Print details on available options
-V, --version # Print version number-o, --output <file> # Output file path (default: STDOUT)Special output formats:
uglifyjs input.js -o ast # Output UglifyJS AST as JSON
uglifyjs input.js -o spidermonkey # Output SpiderMonkey AST as JSON-p, --parse <options> # Specify parser optionsParser option values:
acorn - Use Acorn for parsingbare_returns - Allow return outside of functions (useful for CommonJS modules)spidermonkey - Assume input files are SpiderMonkey AST format (JSON)Usage Examples:
# Allow bare returns (CommonJS modules)
uglifyjs module.js -p bare_returns -o output.js
# Parse SpiderMonkey AST input
uglifyjs ast.json -p spidermonkey -o output.js-c, --compress [options] # Enable compressor/specify compressor optionsCompression option values:
pure_funcs - List of functions that can be safely removed when their return values are not usedUsage Examples:
# Basic compression
uglifyjs input.js -c -o output.js
# Compression with pure function list
uglifyjs input.js -c pure_funcs=['Math.floor','console.log'] -o output.js
# Disable compression
uglifyjs input.js -c false -o output.js-m, --mangle [options] # Mangle names/specify mangler optionsMangling option values:
reserved - List of names that should not be mangledUsage Examples:
# Basic name mangling
uglifyjs input.js -m -o output.js
# Preserve specific names
uglifyjs input.js -m reserved=['$','jQuery'] -o output.js
# Disable mangling
uglifyjs input.js -m false -o output.js--mangle-props [options] # Mangle properties/specify mangler optionsProperty mangling options:
builtins - Mangle property names that overlap with standard JavaScript globalsdebug - Add debug prefix and suffixdomprops - Mangle property names that overlap with DOM propertieskeep_quoted - Only mangle unquoted propertiesregex - Only mangle matched property namesreserved - List of names that should not be mangledUsage Examples:
# Basic property mangling
uglifyjs input.js --mangle-props -o output.js
# Only mangle unquoted properties
uglifyjs input.js --mangle-props keep_quoted -o output.js
# Mangle with regex pattern
uglifyjs input.js --mangle-props regex='/^_/' -o output.js-b, --beautify [options] # Beautify output/specify output options
-O, --output-opts [options] # Specify output options (beautify disabled by default)Output option values:
beautify - Enabled with --beautify by defaultpreamble - Preamble to prepend to the output (e.g., licensing information)quote_style - Quote style: 0 (auto), 1 (single), 2 (double), 3 (original)wrap_iife - Wrap IIFEs in parenthesesUsage Examples:
# Beautified output
uglifyjs input.js -b -o output.js
# Custom quote style
uglifyjs input.js -b quote_style=1 -o output.js
# Add preamble
uglifyjs input.js -b preamble='/* Copyright 2023 */' -o output.js
# Wrap IIFEs
uglifyjs input.js -b wrap_iife -o output.js--annotations # Process and preserve comment annotations
--no-annotations # Ignore and discard comment annotations
--comments [filter] # Preserve copyright comments in the outputComment filter values:
all - Preserve all commentssome - Preserve JSDoc-style comments with "@license" or "@preserve" (default)Usage Examples:
# Preserve license comments
uglifyjs input.js --comments -o output.js
# Preserve all comments
uglifyjs input.js --comments all -o output.js
# Process pure annotations
uglifyjs input.js --annotations -o output.js--source-map [options] # Generate source mapsSource map options:
base - Base path for source map URLscontent - Input source map contentfilename - Name of the input file in source mapincludeSources - Include source content in source mapnames - Include original names in source maproot - Root path for sources in source mapurl - Source map URLUsage Examples:
# Basic source map generation
uglifyjs input.js --source-map -o output.js
# Source map with custom filename
uglifyjs input.js --source-map filename='src/input.js' -o output.js
# Include source content
uglifyjs input.js --source-map includeSources -o output.js
# Custom source map URL
uglifyjs input.js --source-map url='output.js.map' -o output.js--timings # Display timing information
--toplevel # Enable top-level variable and function name mangling
--module # Treat input as ES module
--define <key=value> # Define global variables
--keep-fnames # Preserve function names
--keep-fargs # Preserve function arguments
--ie8 # Support Internet Explorer 8
--webkit # Enable workarounds for Safari/WebKit bugs
--wrap <name> # Wrap output in a function
--enclose [args] # Enclose output in immediately invoked function expressionUsage Examples:
# Top-level mangling for modules
uglifyjs module.js --toplevel -o output.js
# Define global constants
uglifyjs input.js --define DEBUG=false -o output.js
# Preserve function names
uglifyjs input.js --keep-fnames -o output.js
# IE8 compatibility
uglifyjs input.js --ie8 -o output.js
# Wrap in IIFE
uglifyjs input.js --wrap MyLib -o output.js
# Enclose with arguments
uglifyjs input.js --enclose window,document -o output.js# Full production optimization
uglifyjs src/*.js \
--compress pure_funcs=['console.log','console.debug'] \
--mangle reserved=['$','jQuery'] \
--output dist/app.min.js \
--source-map url='app.min.js.map' \
--comments '/license|copyright/i'# Development build with readable output
uglifyjs src/*.js \
--beautify \
--output dist/app.js \
--source-map includeSources \
--comments all# ES module with top-level optimization
uglifyjs src/module.js \
--module \
--toplevel \
--compress \
--mangle \
--output dist/module.min.js# Library build with UMD wrapper
uglifyjs src/lib.js \
--wrap MyLibrary \
--compress \
--mangle reserved=['MyLibrary'] \
--beautify preamble='/*! MyLibrary v1.0.0 */' \
--output dist/mylibrary.jsUglifyJS CLI returns appropriate exit codes:
0 - Success1 - Generic error (syntax error, file not found, etc.)2 - Invalid command line argumentsError messages include file name, line, and column information:
Parse error at file.js:10:15While UglifyJS doesn't use configuration files directly, you can use shell scripts or package.json scripts:
{
"scripts": {
"build": "uglifyjs src/*.js -c -m -o dist/app.min.js --source-map",
"build:dev": "uglifyjs src/*.js -b -o dist/app.js --source-map includeSources"
}
}{
"scripts": {
"minify": "uglifyjs dist/bundle.js -c -m -o dist/bundle.min.js"
}
}dist/app.min.js: src/*.js
uglifyjs $^ -c -m -o $@ --source-map#!/bin/bash
for file in src/*.js; do
output="dist/$(basename "$file" .js).min.js"
uglifyjs "$file" -c -m -o "$output"
done