0
# Minification
1
2
Core minification functionality providing comprehensive JavaScript code optimization with configurable compression, mangling, and output formatting options.
3
4
## Capabilities
5
6
### Minify Function
7
8
Asynchronous minification function that processes JavaScript code and returns optimized output with optional source maps.
9
10
```typescript { .api }
11
/**
12
* Minify JavaScript code asynchronously
13
* @param files - Input JavaScript code as string, array of strings, or object mapping filenames to code
14
* @param options - Minification configuration options
15
* @returns Promise resolving to minification result
16
*/
17
function minify(
18
files: string | string[] | { [file: string]: string },
19
options?: MinifyOptions
20
): Promise<MinifyOutput>;
21
```
22
23
**Usage Examples:**
24
25
```typescript
26
import { minify } from "terser";
27
28
// Single file minification
29
const code = `
30
function greet(name) {
31
console.log("Hello, " + name + "!");
32
return true;
33
}
34
`;
35
36
const result = await minify(code, {
37
compress: { drop_console: true },
38
mangle: true
39
});
40
console.log(result.code); // function greet(e){return!0}
41
42
// Multiple files with source map
43
const files = {
44
"input1.js": "function add(a, b) { return a + b; }",
45
"input2.js": "function multiply(x, y) { return x * y; }"
46
};
47
48
const result = await minify(files, {
49
sourceMap: { filename: "bundle.js.map" },
50
compress: true,
51
mangle: true
52
});
53
```
54
55
### Minify Sync Function
56
57
Synchronous minification function for cases where blocking execution is acceptable or required.
58
59
```typescript { .api }
60
/**
61
* Minify JavaScript code synchronously
62
* @param files - Input JavaScript code as string, array of strings, or object mapping filenames to code
63
* @param options - Minification configuration options
64
* @returns Minification result
65
*/
66
function minify_sync(
67
files: string | string[] | { [file: string]: string },
68
options?: MinifyOptions
69
): MinifyOutput;
70
```
71
72
**Usage Examples:**
73
74
```typescript
75
import { minify_sync } from "terser";
76
77
// Synchronous minification
78
const code = "var unused = 123; function test() { console.log('test'); }";
79
const result = minify_sync(code, {
80
compress: {
81
unused: true,
82
dead_code: true
83
}
84
});
85
console.log(result.code); // function test(){console.log("test")}
86
```
87
88
### Minification Output
89
90
Result object containing the minified code and optional source map information.
91
92
```typescript { .api }
93
interface MinifyOutput {
94
/** The minified JavaScript code */
95
code?: string;
96
/** Source map as JSON string */
97
map?: string;
98
/** Decoded source map object for programmatic access */
99
decoded_map?: object | null;
100
/** Performance timing information (when timings: true) */
101
timings?: {
102
parse: number;
103
rename: number;
104
compress: number;
105
scope: number;
106
mangle: number;
107
properties: number;
108
format: number;
109
total: number;
110
};
111
}
112
```
113
114
### Input Formats
115
116
Terser accepts multiple input formats for flexible integration:
117
118
```typescript { .api }
119
// Single code string
120
type SingleInput = string;
121
122
// Array of code strings (processed in order)
123
type MultipleInputs = string[];
124
125
// Object mapping filenames to code
126
type FileMapping = { [filename: string]: string };
127
128
type InputFiles = SingleInput | MultipleInputs | FileMapping;
129
```
130
131
**Usage Examples:**
132
133
```typescript
134
// String input
135
await minify("console.log('hello');");
136
137
// Array input (concatenated)
138
await minify([
139
"var a = 1;",
140
"var b = 2;",
141
"console.log(a + b);"
142
]);
143
144
// Object input (named files)
145
await minify({
146
"utils.js": "function helper() { return true; }",
147
"main.js": "helper(); console.log('done');"
148
});
149
```
150
151
### Error Handling
152
153
Minification functions can throw errors for various reasons including syntax errors, invalid options, or internal processing failures.
154
155
```typescript
156
try {
157
const result = await minify("invalid javascript syntax {{{");
158
} catch (error) {
159
console.error("Minification failed:", error.message);
160
// Handle parse errors, option validation errors, etc.
161
}
162
```
163
164
### Performance Considerations
165
166
- Use `minify_sync` only when necessary as it blocks the event loop
167
- For large codebases, consider processing files in smaller batches
168
- Enable compression passes sparingly as they can significantly increase processing time
169
- Source map generation adds overhead but is essential for debugging production code