0
# Output Formatting
1
2
Comprehensive code generation and formatting system controlling whitespace, comments, quotes, semicolons, and structural formatting with extensive customization options for production and development builds.
3
4
## Capabilities
5
6
### Format Options
7
8
Complete output formatting configuration for controlling every aspect of generated JavaScript code appearance.
9
10
```typescript { .api }
11
interface FormatOptions {
12
/** Generate ASCII-only output (escape Unicode characters) */
13
ascii_only?: boolean;
14
/** @deprecated Legacy beautify option (not implemented) */
15
beautify?: boolean;
16
/** Always use braces for single-statement blocks */
17
braces?: boolean;
18
/** Comment preservation and filtering */
19
comments?: boolean | 'all' | 'some' | RegExp | CommentFilter;
20
/** Target ECMAScript version for output syntax */
21
ecma?: ECMA;
22
/** Internet Explorer 8 compatibility mode */
23
ie8?: boolean;
24
/** Preserve original numeric literal formats */
25
keep_numbers?: boolean;
26
/** Base indentation level for formatted output */
27
indent_level?: number;
28
/** Starting indentation for first line */
29
indent_start?: number;
30
/** Generate HTML script-tag compatible output */
31
inline_script?: boolean;
32
/** Preserve quoted property names in objects */
33
keep_quoted_props?: boolean;
34
/** Maximum line length before wrapping */
35
max_line_len?: number | false;
36
/** Code preamble (license headers, etc.) */
37
preamble?: string;
38
/** Preserve TypeScript-style type annotations */
39
preserve_annotations?: boolean;
40
/** Force quotes on all object property keys */
41
quote_keys?: boolean;
42
/** Quote style preference for strings */
43
quote_style?: OutputQuoteStyle;
44
/** Safari 10 compatibility mode */
45
safari10?: boolean;
46
/** Include semicolons in output */
47
semicolons?: boolean;
48
/** Preserve Unix shebang lines */
49
shebang?: boolean;
50
/** Use shorthand property syntax when possible */
51
shorthand?: boolean;
52
/** Source map generation options */
53
source_map?: SourceMapOptions;
54
/** WebKit browser compatibility mode */
55
webkit?: boolean;
56
/** Output width for line wrapping */
57
width?: number;
58
/** Wrap immediately invoked function expressions */
59
wrap_iife?: boolean;
60
/** Wrap function arguments in parentheses */
61
wrap_func_args?: boolean;
62
}
63
64
type CommentFilter = (node: any, comment: CommentObject) => boolean;
65
66
interface CommentObject {
67
value: string;
68
type: 'comment1' | 'comment2' | 'comment3' | 'comment4';
69
pos: number;
70
line: number;
71
col: number;
72
}
73
```
74
75
### Comment Handling
76
77
Advanced comment preservation and filtering system for maintaining documentation and license information.
78
79
**Usage Examples:**
80
81
```typescript
82
// Preserve all comments
83
const result = await minify(code, {
84
format: {
85
comments: 'all' // Keep every comment
86
}
87
});
88
89
// Remove all comments
90
const result = await minify(code, {
91
format: {
92
comments: false // Strip all comments
93
}
94
});
95
96
// Preserve license comments only
97
const result = await minify(code, {
98
format: {
99
comments: /license|copyright|@preserve/i
100
}
101
});
102
103
// Custom comment filter
104
const result = await minify(code, {
105
format: {
106
comments: (node, comment) => {
107
// Keep JSDoc comments and license headers
108
return comment.value.includes('@') ||
109
comment.value.toLowerCase().includes('license');
110
}
111
}
112
});
113
114
// Default behavior (some comments preserved)
115
const result = await minify(code, {
116
format: {
117
comments: 'some' // Keep JSDoc-style comments
118
}
119
});
120
```
121
122
### Quote Style Configuration
123
124
Control string and property key quote formatting for consistency and size optimization.
125
126
```typescript { .api }
127
enum OutputQuoteStyle {
128
PreferDouble = 0, // Use double quotes when possible
129
AlwaysSingle = 1, // Force single quotes
130
AlwaysDouble = 2, // Force double quotes
131
AlwaysOriginal = 3 // Preserve original quotes
132
}
133
```
134
135
**Usage Examples:**
136
137
```typescript
138
// Minimize quote escaping
139
const result = await minify(code, {
140
format: {
141
quote_style: OutputQuoteStyle.PreferDouble,
142
quote_keys: false // Don't quote object keys unnecessarily
143
}
144
});
145
146
// Consistent single quotes
147
const result = await minify(code, {
148
format: {
149
quote_style: OutputQuoteStyle.AlwaysSingle
150
}
151
});
152
153
// Preserve original formatting
154
const result = await minify(code, {
155
format: {
156
quote_style: OutputQuoteStyle.AlwaysOriginal,
157
keep_quoted_props: true // Keep quoted property names
158
}
159
});
160
```
161
162
### Whitespace and Formatting
163
164
Control indentation, line breaks, and structural formatting for readable or compact output.
165
166
**Usage Examples:**
167
168
```typescript
169
// Compact output (default)
170
const result = await minify(code, {
171
format: {
172
semicolons: true, // Always use semicolons
173
braces: false // Omit braces when possible
174
}
175
});
176
177
// Readable output
178
const result = await minify(code, {
179
format: {
180
indent_level: 2, // 2-space indentation
181
max_line_len: 80, // Wrap at 80 characters
182
braces: true, // Always use braces
183
semicolons: true // Always use semicolons
184
}
185
});
186
187
// Development-friendly formatting
188
const result = await minify(code, {
189
compress: false, // No compression
190
mangle: false, // No mangling
191
format: {
192
beautify: true, // @deprecated but works
193
indent_level: 4,
194
comments: true
195
}
196
});
197
```
198
199
### Browser Compatibility
200
201
Special formatting modes for legacy browser support and compatibility issues.
202
203
**Usage Examples:**
204
205
```typescript
206
// Internet Explorer 8 support
207
const result = await minify(code, {
208
format: {
209
ie8: true, // IE8 compatibility mode
210
ascii_only: true, // Escape Unicode for IE8
211
quote_keys: true // Quote all object keys
212
}
213
});
214
215
// Safari 10 compatibility
216
const result = await minify(code, {
217
format: {
218
safari10: true // Avoid Safari 10 bugs
219
}
220
});
221
222
// WebKit compatibility
223
const result = await minify(code, {
224
format: {
225
webkit: true // WebKit-specific fixes
226
}
227
});
228
229
// Inline script compatibility
230
const result = await minify(code, {
231
format: {
232
inline_script: true // Safe for <script> tags
233
}
234
});
235
```
236
237
### License and Preamble Handling
238
239
Manage license headers, copyright notices, and code preambles.
240
241
**Usage Examples:**
242
243
```typescript
244
// Add license header
245
const result = await minify(code, {
246
format: {
247
preamble: `/*!
248
* MyLibrary v1.0.0
249
* Copyright (c) 2024 My Company
250
* Licensed under MIT
251
*/`,
252
comments: /license|copyright/i // Preserve license comments
253
}
254
});
255
256
// Preserve shebang for CLI tools
257
const result = await minify(cliCode, {
258
format: {
259
shebang: true // Keep #!/usr/bin/env node
260
}
261
});
262
```
263
264
### Line Length and Wrapping
265
266
Control line length and code wrapping for readability and diff-friendliness.
267
268
**Usage Examples:**
269
270
```typescript
271
// Unlimited line length (compact)
272
const result = await minify(code, {
273
format: {
274
max_line_len: false // No line wrapping
275
}
276
});
277
278
// Wrap at 120 characters
279
const result = await minify(code, {
280
format: {
281
max_line_len: 120,
282
width: 120 // Consistent wrapping width
283
}
284
});
285
286
// Short lines for mobile/narrow displays
287
const result = await minify(code, {
288
format: {
289
max_line_len: 60
290
}
291
});
292
```
293
294
### Advanced Formatting Options
295
296
Specialized formatting controls for specific use cases and code patterns.
297
298
**Usage Examples:**
299
300
```typescript
301
// Function expression formatting
302
const result = await minify(code, {
303
format: {
304
wrap_iife: true, // (function(){})() style
305
wrap_func_args: true // Wrap function arguments
306
}
307
});
308
309
// ES6+ feature formatting
310
const result = await minify(code, {
311
format: {
312
shorthand: true, // Use {a} instead of {a: a}
313
ecma: 2020 // Modern syntax allowed
314
}
315
});
316
317
// Preserve type information
318
const result = await minify(tsCode, {
319
format: {
320
preserve_annotations: true // Keep type annotations
321
}
322
});
323
324
// Numeric literal preservation
325
const result = await minify(code, {
326
format: {
327
keep_numbers: true // Keep 0xFF instead of 255
328
}
329
});
330
```
331
332
### Source Map Integration
333
334
Generate source maps with different output configurations.
335
336
**Usage Examples:**
337
338
```typescript
339
// Generate external source map
340
const result = await minify(code, {
341
sourceMap: {
342
filename: 'output.js.map',
343
url: 'output.js.map'
344
}
345
});
346
console.log(result.map); // Source map JSON string
347
348
// Inline source map
349
const result = await minify(code, {
350
sourceMap: {
351
url: 'inline' // Embed source map as data URI
352
}
353
});
354
// Source map embedded in result.code
355
```
356
357
## Types
358
359
```typescript { .api }
360
enum OutputQuoteStyle {
361
PreferDouble = 0,
362
AlwaysSingle = 1,
363
AlwaysDouble = 2,
364
AlwaysOriginal = 3
365
}
366
367
interface CommentObject {
368
value: string;
369
type: 'comment1' | 'comment2' | 'comment3' | 'comment4';
370
pos: number;
371
line: number;
372
col: number;
373
}
374
375
type CommentFilter = (node: any, comment: CommentObject) => boolean;
376
377
type ECMA = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020;
378
```