0
# JavaScript Minification
1
2
Comprehensive JavaScript minification supporting multiple minifiers with configurable options and full async/await support.
3
4
## Capabilities
5
6
### JavaScript Minifier Function
7
8
Minifies JavaScript source code using the selected minifier backend.
9
10
```javascript { .api }
11
/**
12
* Minifies JavaScript source code using configurable minifiers
13
* @param source - JavaScript source code to minify
14
* @param userOptions - Configuration options for minification
15
* @returns Promise resolving to minified JavaScript code
16
*/
17
function minify.js(source: string, userOptions?: MinifyOptions): Promise<string>;
18
19
interface MinifyOptions {
20
js?: {
21
type?: 'putout' | 'terser' | 'esbuild' | 'swc';
22
putout?: PutoutOptions;
23
terser?: TerserOptions;
24
esbuild?: ESBuildOptions;
25
swc?: SWCOptions;
26
};
27
}
28
```
29
30
**Usage Examples:**
31
32
```javascript
33
import { minify } from "minify";
34
35
// Default minification using putout
36
const code = "function hello(world) {\n console.log(world);\n}";
37
const minified = await minify.js(code);
38
// Result: "function hello(a){console.log(a)}"
39
40
// Using terser
41
const terserResult = await minify.js(code, {
42
js: {
43
type: 'terser',
44
terser: {
45
mangle: false
46
}
47
}
48
});
49
50
// Using esbuild
51
const esbuildResult = await minify.js(code, {
52
js: {
53
type: 'esbuild',
54
esbuild: {
55
minifyWhitespace: true,
56
minifyIdentifiers: false
57
}
58
}
59
});
60
61
// Using swc
62
const swcResult = await minify.js(code, {
63
js: {
64
type: 'swc',
65
swc: {
66
mangle: false,
67
module: true
68
}
69
}
70
});
71
```
72
73
## Minifier Backends
74
75
### Putout (Default)
76
77
Fast and modern JavaScript minifier with AST-based transformations.
78
79
```javascript { .api }
80
interface PutoutOptions {
81
quote?: string; // Quote style: "'" or '"'
82
mangle?: boolean; // Enable variable name mangling
83
mangleClassNames?: boolean; // Enable class name mangling
84
removeUnusedVariables?: boolean; // Remove unused variable declarations
85
removeConsole?: boolean; // Remove console.* calls
86
removeUselessSpread?: boolean; // Remove unnecessary spread operators
87
}
88
```
89
90
**Usage:**
91
92
```javascript
93
const result = await minify.js(source, {
94
js: {
95
type: 'putout',
96
putout: {
97
quote: "'",
98
mangle: true,
99
removeConsole: true,
100
removeUnusedVariables: false
101
}
102
}
103
});
104
```
105
106
### Terser
107
108
Industry-standard JavaScript minifier with extensive configuration options.
109
110
```javascript { .api }
111
interface TerserOptions {
112
mangle?: boolean | object; // Variable name mangling options
113
compress?: boolean | object; // Code compression options
114
format?: object; // Output formatting options
115
sourceMap?: boolean | object; // Source map generation
116
ecma?: number; // ECMAScript version target
117
keep_fnames?: boolean; // Preserve function names
118
toplevel?: boolean; // Mangle top-level variables
119
}
120
```
121
122
**Usage:**
123
124
```javascript
125
const result = await minify.js(source, {
126
js: {
127
type: 'terser',
128
terser: {
129
mangle: {
130
toplevel: true
131
},
132
compress: {
133
drop_console: true,
134
drop_debugger: true
135
}
136
}
137
}
138
});
139
```
140
141
### ESBuild
142
143
Ultra-fast Go-based JavaScript minifier and bundler.
144
145
```javascript { .api }
146
interface ESBuildOptions {
147
minifyWhitespace?: boolean; // Remove unnecessary whitespace
148
minifyIdentifiers?: boolean; // Shorten variable names
149
minifySyntax?: boolean; // Use shorter syntax when possible
150
target?: string[]; // Target environments
151
format?: 'iife' | 'cjs' | 'esm'; // Output format
152
platform?: 'browser' | 'node'; // Target platform
153
}
154
```
155
156
**Usage:**
157
158
```javascript
159
const result = await minify.js(source, {
160
js: {
161
type: 'esbuild',
162
esbuild: {
163
minifyWhitespace: true,
164
minifyIdentifiers: true,
165
minifySyntax: true,
166
target: ['es2020']
167
}
168
}
169
});
170
```
171
172
### SWC
173
174
Rust-based JavaScript minifier with high performance and modern features.
175
176
```javascript { .api }
177
interface SWCOptions {
178
mangle?: boolean | object; // Variable name mangling
179
compress?: boolean | object; // Code compression settings
180
module?: boolean; // Enable module mode
181
toplevel?: boolean; // Mangle top-level scope
182
keep_fnames?: boolean; // Preserve function names
183
safari10?: boolean; // Safari 10 compatibility
184
}
185
```
186
187
**Usage:**
188
189
```javascript
190
const result = await minify.js(source, {
191
js: {
192
type: 'swc',
193
swc: {
194
mangle: true,
195
module: true,
196
compress: {
197
unused: true
198
}
199
}
200
}
201
});
202
```
203
204
## Error Handling
205
206
JavaScript minification handles various error conditions:
207
208
- **Invalid Syntax**: Syntax errors in source code are propagated from minifiers
209
- **Missing Source**: Throws assertion error if source parameter is empty
210
- **Minifier Errors**: Backend-specific errors are passed through with context
211
- **Type Validation**: Validates minifier type selection and options structure
212
213
**Error Examples:**
214
215
```javascript
216
// Empty source validation
217
try {
218
await minify.js("");
219
} catch (error) {
220
console.log(error); // AssertionError
221
}
222
223
// Invalid minifier type
224
try {
225
await minify.js(code, { js: { type: 'invalid' } });
226
} catch (error) {
227
console.log(error.message); // Minifier error
228
}
229
```