0
# Minification
1
2
The standalone minify function provides code compression and optimization functionality without transformation or compilation features.
3
4
## Capabilities
5
6
### Minify Function
7
8
Creates a Rollup plugin specifically for code minification using esbuild's fast minification engine.
9
10
```typescript { .api }
11
/**
12
* Creates a standalone minification plugin
13
* @param options - Minification configuration options
14
* @returns Rollup plugin instance for minification only
15
*/
16
function minify(options?: MinifyOptions): RollupPlugin;
17
18
interface MinifyOptions {
19
// Source maps
20
sourceMap?: boolean;
21
22
// Minification control
23
minify?: boolean;
24
minifyWhitespace?: boolean;
25
minifyIdentifiers?: boolean;
26
minifySyntax?: boolean;
27
28
// Output configuration
29
target?: string | string[];
30
charset?: "ascii" | "utf8";
31
format?: "iife" | "cjs" | "esm";
32
globalName?: string;
33
34
// Code preservation
35
keepNames?: boolean;
36
legalComments?: "none" | "inline" | "eof" | "linked" | "external";
37
ignoreAnnotations?: boolean;
38
treeShaking?: boolean;
39
40
// Advanced minification
41
mangleProps?: RegExp;
42
reserveProps?: RegExp;
43
mangleQuoted?: boolean;
44
mangleCache?: { [key: string]: string | false };
45
46
// Code elimination
47
drop?: ("console" | "debugger")[];
48
dropLabels?: string[];
49
}
50
```
51
52
**Usage Examples:**
53
54
```typescript
55
import { minify } from "rollup-plugin-esbuild";
56
57
// Basic minification
58
export default {
59
plugins: [
60
minify()
61
]
62
};
63
64
// Custom minification options
65
export default {
66
plugins: [
67
minify({
68
target: "es2017",
69
keepNames: true,
70
legalComments: "none"
71
})
72
]
73
};
74
75
// Selective minification
76
export default {
77
plugins: [
78
minify({
79
minifyWhitespace: true,
80
minifyIdentifiers: false,
81
minifySyntax: true
82
})
83
]
84
};
85
86
// Production minification with console removal
87
export default {
88
plugins: [
89
minify({
90
minify: true,
91
drop: ["console", "debugger"],
92
legalComments: "none"
93
})
94
]
95
};
96
```
97
98
### Minification Control
99
100
Fine-grained control over different aspects of minification.
101
102
```typescript { .api }
103
interface MinificationControl {
104
/** Enable full minification (combines all minify options) */
105
minify?: boolean;
106
/** Remove unnecessary whitespace and formatting */
107
minifyWhitespace?: boolean;
108
/** Shorten variable and function names */
109
minifyIdentifiers?: boolean;
110
/** Transform syntax for smaller output */
111
minifySyntax?: boolean;
112
}
113
```
114
115
**Behavior:**
116
- `minify: true` - Enables all minification options
117
- Individual options allow selective minification
118
- Can be combined for custom minification levels
119
120
### Name Preservation
121
122
Control how function and class names are handled during minification.
123
124
```typescript { .api }
125
interface NameOptions {
126
/** Preserve original function and class names */
127
keepNames?: boolean;
128
/** Pattern for properties to mangle */
129
mangleProps?: RegExp;
130
/** Pattern for properties to preserve */
131
reserveProps?: RegExp;
132
/** Whether to mangle quoted properties */
133
mangleQuoted?: boolean;
134
/** Cache for consistent property name mangling */
135
mangleCache?: { [key: string]: string | false };
136
}
137
```
138
139
**Examples:**
140
```typescript
141
// Preserve all names for debugging
142
{
143
keepNames: true
144
}
145
146
// Mangle private properties (starting with _)
147
{
148
mangleProps: /^_/,
149
mangleQuoted: false
150
}
151
152
// Preserve specific properties
153
{
154
mangleProps: /.*/,
155
reserveProps: /^(public|api)_/
156
}
157
```
158
159
### Code Elimination
160
161
Remove specific types of code during minification.
162
163
```typescript { .api }
164
interface CodeElimination {
165
/** Remove console calls and debugger statements */
166
drop?: ("console" | "debugger")[];
167
/** Remove labeled statements by label name */
168
dropLabels?: string[];
169
}
170
```
171
172
**Examples:**
173
```typescript
174
// Remove console and debugger for production
175
{
176
drop: ["console", "debugger"]
177
}
178
179
// Remove development-only code blocks
180
{
181
dropLabels: ["DEV", "DEBUG", "TEST"]
182
}
183
```
184
185
### Legal Comments Handling
186
187
Control how legal comments (copyright notices, licenses) are handled.
188
189
```typescript { .api }
190
interface LegalCommentsOptions {
191
/** How to handle legal comments */
192
legalComments?: "none" | "inline" | "eof" | "linked" | "external";
193
}
194
```
195
196
**Options:**
197
- `"none"` - Remove all legal comments
198
- `"inline"` - Keep legal comments inline with code
199
- `"eof"` - Move legal comments to end of file
200
- `"linked"` - Create separate .LEGAL.txt file and link in bundle
201
- `"external"` - Create separate .LEGAL.txt file without linking
202
203
### Output Format Configuration
204
205
Configure output formatting for minified code.
206
207
```typescript { .api }
208
interface OutputFormatOptions {
209
/** Target JavaScript version/environment */
210
target?: string | string[];
211
/** Character encoding for output */
212
charset?: "ascii" | "utf8";
213
/** Output module format */
214
format?: "iife" | "cjs" | "esm";
215
/** Global variable name for IIFE format */
216
globalName?: string;
217
}
218
```
219
220
**Examples:**
221
```typescript
222
// Target modern browsers
223
{
224
target: ["es2020", "chrome80", "firefox78"],
225
charset: "utf8"
226
}
227
228
// IIFE bundle for browser
229
{
230
format: "iife",
231
globalName: "MyLibrary",
232
target: "es2015"
233
}
234
235
// CommonJS for Node.js
236
{
237
format: "cjs",
238
target: "node14"
239
}
240
```
241
242
### Advanced Optimization Options
243
244
Additional optimization settings for fine-tuned minification.
245
246
```typescript { .api }
247
interface AdvancedOptions {
248
/** Enable tree shaking dead code elimination */
249
treeShaking?: boolean;
250
/** Ignore @__PURE__ annotations */
251
ignoreAnnotations?: boolean;
252
}
253
```
254
255
**Usage:**
256
- `treeShaking: true` - Remove unused code
257
- `ignoreAnnotations: true` - Ignore pure function annotations for more aggressive minification