-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminify.js
More file actions
52 lines (47 loc) · 2.15 KB
/
minify.js
File metadata and controls
52 lines (47 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const fs = require('fs');
const { minify } = require('html-minifier');
const inputFile = 'index.html';
const outputFile = 'index.html';
const html = fs.readFileSync(inputFile, 'utf8');
// Use very conservative minification options to avoid parsing errors
const minifyOptions = {
collapseWhitespace: true,
removeComments: false, // Keep comments to avoid breaking display code
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyCSS: true,
minifyJS: false,
removeEmptyAttributes: false, // Safer
removeOptionalTags: false,
removeTagWhitespace: false,
sortAttributes: false,
sortClassName: false,
caseSensitive: false,
keepClosingSlash: false,
decodeEntities: false, // Keep entities as-is
ignoreCustomComments: [/^!/], // Preserve important comments
processScripts: [] // Don't process scripts
};
try {
const minified = minify(html, minifyOptions);
fs.writeFileSync(outputFile, minified);
console.log(`✓ Minified ${inputFile} (${html.length} → ${minified.length} bytes, ${Math.round((1 - minified.length/html.length) * 100)}% reduction)`);
} catch (error) {
// Fallback: aggressive but safe whitespace removal without full parsing
const basicMinified = html
.replace(/>\s+</g, '><') // Remove whitespace between tags
.replace(/\s+/g, ' ') // Collapse multiple spaces to single space
.replace(/\s+>/g, '>') // Remove spaces before closing tags
.replace(/<\s+/g, '<') // Remove spaces after opening tags
.replace(/\s*{\s*/g, '{') // Remove spaces around CSS braces
.replace(/\s*}\s*/g, '}') // Remove spaces around CSS braces
.replace(/\s*:\s*/g, ':') // Remove spaces around CSS colons
.replace(/\s*;\s*/g, ';') // Remove spaces around CSS semicolons
.replace(/<!--[\s\S]*?-->/g, '') // Remove HTML comments
.trim();
fs.writeFileSync(outputFile, basicMinified);
console.log(`✓ Safe minification applied (${html.length} → ${basicMinified.length} bytes, ${Math.round((1 - basicMinified.length/html.length) * 100)}% reduction)`);
console.log(' Note: Full minification failed due to HTML structure, using safe fallback');
}