-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathss-minifier.js
79 lines (69 loc) · 2.18 KB
/
ss-minifier.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs-extra');
const minify = require('html-minifier').minify;
const path = require('path');
const dist = 'dist/prerender/';
let orgSize = 0;
let newSize = 0;
fs.readdir(dist, (err, files) => {
if(err){
console.log(err);
return;
}
Promise.all(files.map(async file => {
const filePath = path.join(dist, file);
const fileStats = await fs.stat(filePath);
if(fileStats.isDirectory()){
return;
}
if(path.extname(filePath) !== ".html"){
return;
}
let buf = await fs.readFile(filePath);
let originalValue = buf.toString();
let minifiedValue = minify(originalValue, {
caseSensitive: false,
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: false,
collapseWhitespace: true,
conservativeCollapse: false,
decodeEntities: true,
html5: true,
includeAutoGeneratedTags: false,
keepClosingSlash: false,
minifyCSS: true,
minifyJS: true,
preserveLineBreaks: false,
preventAttributesEscaping: false,
processConditionalComments: true,
processScripts: ["text/html"],
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeEmptyElements: false,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeTagWhitespace: true,
sortAttributes: true,
sortClassName: true,
trimCustomFragments: true,
useShortDoctype: true
});
orgSize += originalValue.length;
newSize += minifiedValue.length;
minifiedValue = "<!--simplyalec.com-->" + minifiedValue + "<!--prod:" + new Date().toISOString() + "-->";
await fs.writeFile(filePath, minifiedValue);
console.log("Minified " + filePath + ".")
})).then(() => {
const diff = orgSize - newSize;
const savings = orgSize ? (100 * diff / orgSize).toFixed(2) : 0;
console.log("[SS] Successfully minified files." +
". Original size: " + orgSize +
". Minified size: " + newSize +
". Savings: " + diff + " (" + savings + "%)");
}).catch(e => {
console.log(e);
process.exit(1);
});
});