or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast.mdcli.mdindex.mdtransform.md
tile.json

cli.mddocs/

Command Line Interface

UglifyJS provides a comprehensive command-line interface for processing JavaScript files with full configuration support for minification, compression, and output formatting.

Capabilities

Basic Usage

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

Core Options

Help and Version

-h, --help                    # Print usage information
--help options               # Print details on available options
-V, --version                # Print version number

Input/Output

-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

Parser Options

-p, --parse <options>        # Specify parser options

Parser option values:

  • acorn - Use Acorn for parsing
  • bare_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

Compression Options

-c, --compress [options]     # Enable compressor/specify compressor options

Compression option values:

  • pure_funcs - List of functions that can be safely removed when their return values are not used

Usage 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

Mangling Options

-m, --mangle [options]       # Mangle names/specify mangler options

Mangling option values:

  • reserved - List of names that should not be mangled

Usage 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

Property Mangling

--mangle-props [options]     # Mangle properties/specify mangler options

Property mangling options:

  • builtins - Mangle property names that overlap with standard JavaScript globals
  • debug - Add debug prefix and suffix
  • domprops - Mangle property names that overlap with DOM properties
  • keep_quoted - Only mangle unquoted properties
  • regex - Only mangle matched property names
  • reserved - List of names that should not be mangled

Usage 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

Output Options

-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 default
  • preamble - 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 parentheses

Usage 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

Comments and Annotations

--annotations               # Process and preserve comment annotations
--no-annotations           # Ignore and discard comment annotations
--comments [filter]         # Preserve copyright comments in the output

Comment filter values:

  • all - Preserve all comments
  • some - Preserve JSDoc-style comments with "@license" or "@preserve" (default)
  • Custom regex pattern

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 Maps

--source-map [options]      # Generate source maps

Source map options:

  • base - Base path for source map URLs
  • content - Input source map content
  • filename - Name of the input file in source map
  • includeSources - Include source content in source map
  • names - Include original names in source map
  • root - Root path for sources in source map
  • url - Source map URL

Usage 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

Advanced Options

--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 expression

Usage 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

Complete Examples

Production Build

# 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

# Development build with readable output
uglifyjs src/*.js \
  --beautify \
  --output dist/app.js \
  --source-map includeSources \
  --comments all

Module Processing

# ES module with top-level optimization
uglifyjs src/module.js \
  --module \
  --toplevel \
  --compress \
  --mangle \
  --output dist/module.min.js

Library Build

# Library build with UMD wrapper
uglifyjs src/lib.js \
  --wrap MyLibrary \
  --compress \
  --mangle reserved=['MyLibrary'] \
  --beautify preamble='/*! MyLibrary v1.0.0 */' \
  --output dist/mylibrary.js

Error Handling

UglifyJS CLI returns appropriate exit codes:

  • 0 - Success
  • 1 - Generic error (syntax error, file not found, etc.)
  • 2 - Invalid command line arguments

Error messages include file name, line, and column information:

Parse error at file.js:10:15

Configuration Files

While 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"
  }
}

Integration with Build Tools

npm scripts

{
  "scripts": {
    "minify": "uglifyjs dist/bundle.js -c -m -o dist/bundle.min.js"
  }
}

Makefile

dist/app.min.js: src/*.js
	uglifyjs $^ -c -m -o $@ --source-map

Shell script

#!/bin/bash
for file in src/*.js; do
  output="dist/$(basename "$file" .js).min.js"
  uglifyjs "$file" -c -m -o "$output"
done