CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-terser

JavaScript parser, mangler/compressor and beautifier toolkit for ES6+

Pending
Overview
Eval results
Files

cli.mddocs/

Command Line Interface

Full-featured command-line interface providing comprehensive JavaScript minification capabilities with extensive configuration options, file processing, and build system integration.

Capabilities

CLI Runner Function

Programmatic interface for running the Terser CLI with custom configuration.

/**
 * Run the Terser command-line interface programmatically
 * @param config - CLI configuration object
 */
async function run_cli(config: {
  program: import('commander').Command;
  packageJson: { name: string; version: string; [key: string]: any };
  fs: typeof import('fs');
  path: typeof import('path');
}): Promise<void>;

Command Usage

Basic command syntax for processing JavaScript files:

terser [input files] [options]

Usage Examples:

# Single file minification
terser input.js -o output.min.js

# Multiple files
terser file1.js file2.js -o bundle.min.js

# Compression with console removal
terser app.js --compress drop_console=true -o app.min.js

# Mangling with reserved names
terser lib.js --mangle reserved=['$','jQuery'] -o lib.min.js

# Source map generation
terser src.js --source-map filename=src.js.map -o src.min.js

Core Options

Essential command-line options for controlling minification behavior:

# Parsing Options
-p, --parse <options>       # Parser configuration
                           # bare_returns, html5_comments, shebang

# Compression Options  
-c, --compress [options]    # Enable compression with optional config
                           # pure_funcs, drop_console, dead_code, etc.

# Mangling Options
-m, --mangle [options]      # Enable name mangling with optional config
                           # reserved, keep_fnames, toplevel

# Property Mangling
--mangle-props [options]    # Mangle property names
                           # builtins, debug, keep_quoted, regex

# Output Options
-f, --format [options]      # Output formatting configuration
-b, --beautify [options]    # Alias for --format
-o, --output <file>         # Output file (default: STDOUT)

Advanced Options

Comprehensive configuration options for specialized use cases:

# ECMAScript Version
--ecma <version>            # Target ES version: 5, 2015, 2016, 2017, 2018, 2019, 2020

# Compatibility Options
--ie8                       # Internet Explorer 8 support
--safari10                  # Safari 10 compatibility

# Name Preservation
--keep-classnames           # Preserve class names
--keep-fnames              # Preserve function names

# Module Options
--module                    # Treat input as ES6 module
--toplevel                  # Process top-level scope

# Code Wrapping
-e, --enclose [args]        # Wrap in function with configurable args
--wrap <name>              # Wrap as exports object

# Optimization Control
--rename                    # Force symbol expansion
--no-rename                # Disable symbol expansion

# Debug and Performance
--timings                   # Display operation timing on STDERR

Source Map Options

Source map generation and configuration:

--source-map [options]      # Enable source map generation
                           # content, filename, includeSources, url

# Examples:
--source-map filename=output.js.map
--source-map url=inline
--source-map includeSources=true

Configuration File

Load options from JSON configuration file:

--config-file <file>        # Read minify() options from JSON file

Example config file:

{
  "compress": {
    "drop_console": true,
    "dead_code": true,
    "unused": true
  },
  "mangle": {
    "reserved": ["$", "jQuery"],
    "toplevel": true
  },
  "format": {
    "comments": false,
    "beautify": false
  }
}

Global Definitions

Define global constants for conditional compilation:

-d, --define <expr>[=value] # Define global constants

# Examples:
--define DEBUG=false
--define API_URL="https://api.example.com"
--define VERSION=1.2.3

Name Cache

Persistent name caching for consistent mangling across builds:

--name-cache <file>         # File to store mangled name mappings

# Example:
terser src.js --mangle --name-cache names.json -o output.js

Comment Preservation

Control comment output in minified code:

--comments [filter]         # Preserve comments with optional filtering

# Options:
--comments all             # Keep all comments
--comments false           # Remove all comments  
--comments /license/       # Keep comments matching regex
--comments                 # Keep JSDoc-style comments (default)

Development and Debugging

Options for debugging and development workflow:

--timings                   # Display operation timing on STDERR
-h, --help                 # Show usage information
-V, --version              # Print version number

# Special help topics:
terser --help options      # Show all available options with defaults
terser --help ast          # Show AST node types and structure

Input/Output Handling

Flexible input and output processing:

# Multiple input methods:
terser file.js                    # Single file
terser file1.js file2.js         # Multiple files
terser *.js                      # Glob patterns
cat file.js | terser             # STDIN input

# Output options:
terser input.js                  # Output to STDOUT
terser input.js -o output.js     # Output to file
terser input.js -o ast           # Output AST as JSON
terser input.js -o spidermonkey  # Output SpiderMonkey AST

Error Handling

The CLI provides detailed error reporting for various failure conditions:

  • Parse Errors: Syntax errors with line/column information
  • Option Validation: Invalid option values or combinations
  • File System: Missing input files or write permission errors
  • Source Map: Invalid source map configurations

Integration Examples

Common integration patterns for build systems:

# Webpack build script
terser dist/*.js --compress --mangle --source-map -o dist/bundle.min.js

# npm scripts
"scripts": {
  "minify": "terser src/**/*.js --compress --mangle -d",
  "build": "terser src/app.js --config-file terser.config.json -o dist/app.min.js"
}

# Make target
minify:
	terser $(wildcard src/*.js) --compress drop_console=true --mangle -o dist/bundle.min.js

Install with Tessl CLI

npx tessl i tessl/npm-terser

docs

advanced.md

cli.md

compression.md

configuration.md

formatting.md

index.md

mangling.md

minification.md

tile.json