-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
41 lines (34 loc) · 1.14 KB
/
build.js
File metadata and controls
41 lines (34 loc) · 1.14 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
const minify = require('html-minifier').minify;
const fs = require('fs');
const input = fs.readFileSync('index.html', 'utf8');
const output = minify(input, {
removeAttributeQuotes: true,
collapseWhitespace: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyCSS: true,
minifyJS: true,
includeAutoGeneratedTags: false
});
// Manual hack to remove final closing tags if they exist
let finalOutput = output
.replace('</title>', '')
.replace('</a>', '')
.replace('</h1>', '')
.replace('</pre>', '')
.replace('</body>', '')
.replace('</html>', '');
// Also try to optimize meta viewport if safe (but width=device-width requires quotes due to =)
// So we keep quotes there.
fs.writeFileSync('dist/index.html', finalOutput);
const stats = fs.statSync('dist/index.html');
console.log(`Original size: ${input.length} bytes`);
console.log(`Minified size: ${stats.size} bytes`);
if (stats.size > 1024) {
console.error('ERROR: File size exceeds 1024 bytes!');
process.exit(1);
}