0
# Command Line Interface
1
2
Full-featured command-line interface providing comprehensive JavaScript minification capabilities with extensive configuration options, file processing, and build system integration.
3
4
## Capabilities
5
6
### CLI Runner Function
7
8
Programmatic interface for running the Terser CLI with custom configuration.
9
10
```typescript { .api }
11
/**
12
* Run the Terser command-line interface programmatically
13
* @param config - CLI configuration object
14
*/
15
async function run_cli(config: {
16
program: import('commander').Command;
17
packageJson: { name: string; version: string; [key: string]: any };
18
fs: typeof import('fs');
19
path: typeof import('path');
20
}): Promise<void>;
21
```
22
23
### Command Usage
24
25
Basic command syntax for processing JavaScript files:
26
27
```bash
28
terser [input files] [options]
29
```
30
31
**Usage Examples:**
32
33
```bash
34
# Single file minification
35
terser input.js -o output.min.js
36
37
# Multiple files
38
terser file1.js file2.js -o bundle.min.js
39
40
# Compression with console removal
41
terser app.js --compress drop_console=true -o app.min.js
42
43
# Mangling with reserved names
44
terser lib.js --mangle reserved=['$','jQuery'] -o lib.min.js
45
46
# Source map generation
47
terser src.js --source-map filename=src.js.map -o src.min.js
48
```
49
50
### Core Options
51
52
Essential command-line options for controlling minification behavior:
53
54
```bash
55
# Parsing Options
56
-p, --parse <options> # Parser configuration
57
# bare_returns, html5_comments, shebang
58
59
# Compression Options
60
-c, --compress [options] # Enable compression with optional config
61
# pure_funcs, drop_console, dead_code, etc.
62
63
# Mangling Options
64
-m, --mangle [options] # Enable name mangling with optional config
65
# reserved, keep_fnames, toplevel
66
67
# Property Mangling
68
--mangle-props [options] # Mangle property names
69
# builtins, debug, keep_quoted, regex
70
71
# Output Options
72
-f, --format [options] # Output formatting configuration
73
-b, --beautify [options] # Alias for --format
74
-o, --output <file> # Output file (default: STDOUT)
75
```
76
77
### Advanced Options
78
79
Comprehensive configuration options for specialized use cases:
80
81
```bash
82
# ECMAScript Version
83
--ecma <version> # Target ES version: 5, 2015, 2016, 2017, 2018, 2019, 2020
84
85
# Compatibility Options
86
--ie8 # Internet Explorer 8 support
87
--safari10 # Safari 10 compatibility
88
89
# Name Preservation
90
--keep-classnames # Preserve class names
91
--keep-fnames # Preserve function names
92
93
# Module Options
94
--module # Treat input as ES6 module
95
--toplevel # Process top-level scope
96
97
# Code Wrapping
98
-e, --enclose [args] # Wrap in function with configurable args
99
--wrap <name> # Wrap as exports object
100
101
# Optimization Control
102
--rename # Force symbol expansion
103
--no-rename # Disable symbol expansion
104
105
# Debug and Performance
106
--timings # Display operation timing on STDERR
107
```
108
109
### Source Map Options
110
111
Source map generation and configuration:
112
113
```bash
114
--source-map [options] # Enable source map generation
115
# content, filename, includeSources, url
116
117
# Examples:
118
--source-map filename=output.js.map
119
--source-map url=inline
120
--source-map includeSources=true
121
```
122
123
### Configuration File
124
125
Load options from JSON configuration file:
126
127
```bash
128
--config-file <file> # Read minify() options from JSON file
129
```
130
131
**Example config file:**
132
133
```json
134
{
135
"compress": {
136
"drop_console": true,
137
"dead_code": true,
138
"unused": true
139
},
140
"mangle": {
141
"reserved": ["$", "jQuery"],
142
"toplevel": true
143
},
144
"format": {
145
"comments": false,
146
"beautify": false
147
}
148
}
149
```
150
151
### Global Definitions
152
153
Define global constants for conditional compilation:
154
155
```bash
156
-d, --define <expr>[=value] # Define global constants
157
158
# Examples:
159
--define DEBUG=false
160
--define API_URL="https://api.example.com"
161
--define VERSION=1.2.3
162
```
163
164
### Name Cache
165
166
Persistent name caching for consistent mangling across builds:
167
168
```bash
169
--name-cache <file> # File to store mangled name mappings
170
171
# Example:
172
terser src.js --mangle --name-cache names.json -o output.js
173
```
174
175
### Comment Preservation
176
177
Control comment output in minified code:
178
179
```bash
180
--comments [filter] # Preserve comments with optional filtering
181
182
# Options:
183
--comments all # Keep all comments
184
--comments false # Remove all comments
185
--comments /license/ # Keep comments matching regex
186
--comments # Keep JSDoc-style comments (default)
187
```
188
189
### Development and Debugging
190
191
Options for debugging and development workflow:
192
193
```bash
194
--timings # Display operation timing on STDERR
195
-h, --help # Show usage information
196
-V, --version # Print version number
197
198
# Special help topics:
199
terser --help options # Show all available options with defaults
200
terser --help ast # Show AST node types and structure
201
```
202
203
### Input/Output Handling
204
205
Flexible input and output processing:
206
207
```bash
208
# Multiple input methods:
209
terser file.js # Single file
210
terser file1.js file2.js # Multiple files
211
terser *.js # Glob patterns
212
cat file.js | terser # STDIN input
213
214
# Output options:
215
terser input.js # Output to STDOUT
216
terser input.js -o output.js # Output to file
217
terser input.js -o ast # Output AST as JSON
218
terser input.js -o spidermonkey # Output SpiderMonkey AST
219
```
220
221
### Error Handling
222
223
The CLI provides detailed error reporting for various failure conditions:
224
225
- **Parse Errors**: Syntax errors with line/column information
226
- **Option Validation**: Invalid option values or combinations
227
- **File System**: Missing input files or write permission errors
228
- **Source Map**: Invalid source map configurations
229
230
### Integration Examples
231
232
Common integration patterns for build systems:
233
234
```bash
235
# Webpack build script
236
terser dist/*.js --compress --mangle --source-map -o dist/bundle.min.js
237
238
# npm scripts
239
"scripts": {
240
"minify": "terser src/**/*.js --compress --mangle -d",
241
"build": "terser src/app.js --config-file terser.config.json -o dist/app.min.js"
242
}
243
244
# Make target
245
minify:
246
terser $(wildcard src/*.js) --compress drop_console=true --mangle -o dist/bundle.min.js
247
```