JavaScript parser, mangler/compressor and beautifier toolkit for ES6+
npx @tessl/cli install tessl/npm-terser@5.44.00
# Terser
1
2
Terser is a comprehensive JavaScript minification and optimization library for ES6+ code. It provides advanced parsing, compression, mangling, and code generation capabilities with extensive configuration options and both programmatic and CLI interfaces.
3
4
## Package Information
5
6
- **Package Name**: terser
7
- **Package Type**: npm
8
- **Language**: JavaScript/TypeScript
9
- **Installation**: `npm install terser`
10
11
## Core Imports
12
13
```typescript
14
import { minify, minify_sync, to_ascii } from "terser";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const { minify, minify_sync, to_ascii } = require("terser");
21
```
22
23
## Basic Usage
24
25
```typescript
26
import { minify } from "terser";
27
28
// Minify JavaScript code
29
const code = "function hello() { console.log('Hello, World!'); }";
30
31
const result = await minify(code, {
32
compress: {
33
dead_code: true,
34
drop_console: true
35
},
36
mangle: {
37
keep_fnames: false
38
}
39
});
40
41
console.log(result.code);
42
// Output: function hello(){}
43
```
44
45
## Architecture
46
47
Terser is built around several key components:
48
49
- **Parser**: Converts JavaScript code into Abstract Syntax Trees (AST) using Acorn
50
- **Compressor**: Applies various optimization passes to reduce code size and improve performance
51
- **Mangler**: Renames variables, functions, and properties to shorter names for size reduction
52
- **Output Generator**: Converts optimized AST back to JavaScript code with configurable formatting
53
- **CLI Interface**: Command-line tool for batch processing and build system integration
54
- **Source Maps**: Maintains mapping between original and minified code for debugging
55
56
## Capabilities
57
58
### Core Minification
59
60
Primary minification functionality providing asynchronous and synchronous interfaces for JavaScript code optimization.
61
62
```typescript { .api }
63
function minify(
64
files: string | string[] | { [file: string]: string },
65
options?: MinifyOptions
66
): Promise<MinifyOutput>;
67
68
function minify_sync(
69
files: string | string[] | { [file: string]: string },
70
options?: MinifyOptions
71
): MinifyOutput;
72
73
function to_ascii(base64: string): string;
74
75
interface MinifyOutput {
76
code?: string;
77
map?: string;
78
decoded_map?: object | null;
79
timings?: {
80
parse: number;
81
rename: number;
82
compress: number;
83
scope: number;
84
mangle: number;
85
properties: number;
86
format: number;
87
total: number;
88
};
89
}
90
```
91
92
[Minification](./minification.md)
93
94
### Configuration and Options
95
96
Comprehensive configuration system with separate option categories for parsing, compression, mangling, and output formatting.
97
98
```typescript { .api }
99
interface MinifyOptions {
100
compress?: boolean | CompressOptions;
101
mangle?: boolean | MangleOptions;
102
format?: FormatOptions;
103
parse?: ParseOptions;
104
sourceMap?: boolean | SourceMapOptions;
105
ecma?: ECMA;
106
enclose?: boolean | string;
107
ie8?: boolean;
108
keep_classnames?: boolean | RegExp;
109
keep_fnames?: boolean | RegExp;
110
module?: boolean;
111
nameCache?: object;
112
safari10?: boolean;
113
toplevel?: boolean;
114
}
115
116
type ECMA = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020;
117
```
118
119
[Configuration](./configuration.md)
120
121
### Command Line Interface
122
123
Full-featured CLI tool with extensive options for batch processing, build system integration, and advanced configuration.
124
125
```typescript { .api }
126
async function run_cli(config: {
127
program: import('commander').Command;
128
packageJson: { name: string; version: string; [key: string]: any };
129
fs: typeof import('fs');
130
path: typeof import('path');
131
}): Promise<void>;
132
```
133
134
[Command Line Interface](./cli.md)
135
136
### Advanced Features
137
138
Advanced functionality including AST manipulation, custom identifier mangling, and Mozilla SpiderMonkey AST support.
139
140
```typescript { .api }
141
async function _default_options(): Promise<object>;
142
143
interface SimpleIdentifierMangler {
144
get(n: number): string;
145
}
146
147
interface WeightedIdentifierMangler extends SimpleIdentifierMangler {
148
consider(chars: string, delta: number): number;
149
reset(): void;
150
sort(): void;
151
}
152
```
153
154
[Advanced Features](./advanced.md)
155
156
## Types
157
158
```typescript { .api }
159
interface MinifyOutput {
160
code?: string;
161
map?: string;
162
decoded_map?: object | null;
163
timings?: {
164
parse: number;
165
rename: number;
166
compress: number;
167
scope: number;
168
mangle: number;
169
properties: number;
170
format: number;
171
total: number;
172
};
173
}
174
175
function to_ascii(base64: string): string;
176
177
type ECMA = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020;
178
179
type ConsoleProperty = keyof typeof console;
180
181
enum InlineFunctions {
182
Disabled = 0,
183
SimpleFunctions = 1,
184
WithArguments = 2,
185
WithArgumentsAndVariables = 3
186
}
187
188
enum OutputQuoteStyle {
189
PreferDouble = 0,
190
AlwaysSingle = 1,
191
AlwaysDouble = 2,
192
AlwaysOriginal = 3
193
}
194
```