0
# Minify
1
2
Minify is a comprehensive minification tool for web assets including JavaScript, CSS, HTML, and image files. It provides both a command-line interface and a programmatic API, supporting multiple minifiers for JavaScript (putout, terser, esbuild, swc), CSS (clean-css, lightningcss), HTML minification via html-minifier, and CSS base64 image inlining. The tool offers maximum flexibility with configurable options via .minify.json files and supports multiple output formats and minification strategies.
3
4
## Package Information
5
6
- **Package Name**: minify
7
- **Package Type**: npm
8
- **Language**: JavaScript (ES modules)
9
- **Installation**: `npm install minify` (global: `npm install minify -g`)
10
- **Node.js Version**: >=20
11
- **Deno Support**: `import {minify} from 'npm:minify';`
12
13
## Core Imports
14
15
```javascript
16
import { minify } from "minify";
17
```
18
19
For CommonJS:
20
21
```javascript
22
const { minify } = require("minify");
23
```
24
25
Individual minifier imports:
26
27
```javascript
28
import { minify } from "minify";
29
// Access individual minifiers as properties
30
const jsMinified = await minify.js(source);
31
const cssMinified = await minify.css(source);
32
const htmlMinified = minify.html(source); // Synchronous
33
const autoMinified = await minify.auto(source);
34
```
35
36
## Basic Usage
37
38
### Programmatic Usage
39
40
```javascript
41
import { minify } from "minify";
42
43
// Minify a file by path (auto-detects type from extension)
44
const minifiedContent = await minify("./script.js");
45
console.log(minifiedContent);
46
47
// With custom options
48
const options = {
49
js: {
50
type: "terser",
51
terser: {
52
mangle: false
53
}
54
}
55
};
56
const result = await minify("./app.js", options);
57
```
58
59
### CLI Usage
60
61
```bash
62
# Minify a file and output to console
63
minify script.js
64
65
# Minify and save to file
66
minify script.js > script.min.js
67
68
# Process stdin with specific type
69
cat script.js | minify --js
70
71
# Multiple files
72
minify *.js *.css *.html
73
```
74
75
## Architecture
76
77
Minify is built around several key components:
78
79
- **Main Function**: File-based minification with auto-detection by extension
80
- **Individual Minifiers**: Dedicated functions for each asset type (js, css, html, img, auto)
81
- **Configuration System**: .minify.json file support with directory traversal
82
- **CLI Interface**: Full command-line tool with stdin/stdout support
83
- **Plugin Architecture**: Multiple minifier backends for each asset type
84
- **Error Handling**: Comprehensive validation and error reporting
85
86
## Capabilities
87
88
### JavaScript Minification
89
90
Minifies JavaScript source code using configurable minifiers including putout (default), terser, esbuild, and swc.
91
92
```javascript { .api }
93
function minify.js(source: string, userOptions?: MinifyOptions): Promise<string>;
94
95
interface MinifyOptions {
96
js?: {
97
type?: 'putout' | 'terser' | 'esbuild' | 'swc';
98
putout?: object;
99
terser?: object;
100
esbuild?: object;
101
swc?: object;
102
};
103
}
104
```
105
106
[JavaScript Minification](./javascript-minification.md)
107
108
### CSS Minification
109
110
Minifies CSS data using configurable CSS minifiers including lightningcss (default) and clean-css, with automatic base64 image inlining.
111
112
```javascript { .api }
113
function minify.css(data: string, userOptions?: MinifyOptions): Promise<string>;
114
115
interface MinifyOptions {
116
css?: {
117
type?: 'lightningcss' | 'clean-css';
118
'clean-css'?: object;
119
};
120
img?: {
121
maxSize?: number;
122
};
123
}
124
```
125
126
[CSS Minification](./css-minification.md)
127
128
### HTML Minification
129
130
Minifies HTML data using html-minifier-next with comprehensive default settings for aggressive minification.
131
132
```javascript { .api }
133
function minify.html(data: string, userOptions?: MinifyOptions): string;
134
135
interface MinifyOptions {
136
html?: {
137
removeComments?: boolean;
138
collapseWhitespace?: boolean;
139
removeAttributeQuotes?: boolean;
140
minifyJS?: boolean;
141
minifyCSS?: boolean;
142
// ... other html-minifier options
143
};
144
}
145
```
146
147
[HTML Minification](./html-minification.md)
148
149
### Image Processing (Base64 Inlining)
150
151
Converts CSS images to base64 inline format for reduced HTTP requests. Typically used automatically during CSS minification.
152
153
```javascript { .api }
154
function minify.img(name: string, data: string, userOptions?: MinifyOptions): Promise<string>;
155
156
interface MinifyOptions {
157
img?: {
158
maxSize?: number; // Default: 102400 (100KB)
159
};
160
}
161
```
162
163
[Image Processing](./image-processing.md)
164
165
### Auto-Detection Minification
166
167
Automatically detects content format and applies the appropriate minifier. Tries multiple formats until one succeeds.
168
169
```javascript { .api }
170
function minify.auto(data: string, options?: MinifyOptions): Promise<string>;
171
```
172
173
[Auto-Detection](./auto-detection.md)
174
175
### File-based Minification
176
177
Main minification function that processes files by extension (.js, .css, .html) and applies appropriate minifiers.
178
179
```javascript { .api }
180
function minify(name: string, userOptions?: MinifyOptions): Promise<string>;
181
```
182
183
### CLI Interface
184
185
Command-line interface supporting file processing, stdin/stdout, and various output formats.
186
187
```bash
188
Usage: minify [options] [files...]
189
Options:
190
-h, --help display this help and exit
191
-v, --version display version and exit
192
--js minify javascript from stdin
193
--css minify css from stdin
194
--html minify html from stdin
195
--auto auto detect format from stdin
196
```
197
198
[CLI Interface](./cli-interface.md)
199
200
### Configuration System
201
202
Support for .minify.json configuration files with directory traversal for CLI usage. Configuration files are automatically loaded by the CLI tool only.
203
204
[Configuration](./configuration.md)
205
206
## Types
207
208
```javascript { .api }
209
interface MinifyOptions {
210
js?: {
211
type?: 'putout' | 'terser' | 'esbuild' | 'swc';
212
putout?: PutoutOptions;
213
terser?: TerserOptions;
214
esbuild?: ESBuildOptions;
215
swc?: SWCOptions;
216
};
217
css?: {
218
type?: 'lightningcss' | 'clean-css';
219
'clean-css'?: CleanCSSOptions;
220
};
221
html?: HTMLMinifierOptions;
222
img?: {
223
maxSize?: number;
224
};
225
}
226
227
interface PutoutOptions {
228
quote?: string;
229
mangle?: boolean;
230
mangleClassNames?: boolean;
231
removeUnusedVariables?: boolean;
232
removeConsole?: boolean;
233
removeUselessSpread?: boolean;
234
}
235
236
interface HTMLMinifierOptions {
237
removeComments?: boolean;
238
removeCommentsFromCDATA?: boolean;
239
removeCDATASectionsFromCDATA?: boolean;
240
collapseWhitespace?: boolean;
241
collapseBooleanAttributes?: boolean;
242
removeAttributeQuotes?: boolean;
243
removeRedundantAttributes?: boolean;
244
useShortDoctype?: boolean;
245
removeEmptyAttributes?: boolean;
246
removeEmptyElements?: boolean;
247
removeOptionalTags?: boolean;
248
removeScriptTypeAttributes?: boolean;
249
removeStyleLinkTypeAttributes?: boolean;
250
minifyJS?: boolean;
251
minifyCSS?: boolean;
252
}
253
```
254
255
## Error Handling
256
257
Minify provides comprehensive error handling:
258
259
- **File Type Validation**: Throws error for unsupported file extensions
260
- **Empty Name Validation**: Throws "name could not be empty!" for missing file paths
261
- **Minifier Errors**: Propagates errors from underlying minification tools
262
- **Configuration Errors**: Handles .minify.json parsing and validation errors