forked from hyc-official/HomeCard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini.cjs
49 lines (47 loc) · 1.7 KB
/
mini.cjs
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
const fs = require("fs");
const path = require("path");
const HTMLMinifier = require("html-minifier").minify;
const UglifyJS = require("uglify-js").minify;
const CleanCSS = require("clean-css");
function mini(dir) {
const flist = fs.readdirSync(dir);
flist.forEach((file) => {
const fullpth = path.join(dir, file);
const stat = fs.statSync(fullpth);
if (stat.isDirectory()) {
mini(fullpth);
} else if (path.extname(file) === ".html") {
const data = fs.readFileSync(fullpth, "utf-8");
const res = HTMLMinifier(data, {
caseSensitive: true,
collapseWhitespace: true,
conservativeCollapse: true,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
});
fs.writeFileSync(fullpth, res, "utf-8");
} else if (path.extname(file) === ".js") {
const data = fs.readFileSync(fullpth, "utf-8");
const res = UglifyJS(data, {
keep_fargs: true,
keep_fnames: true,
toplevel: true,
});
fs.writeFileSync(fullpth, res.code, "utf-8");
} else if (path.extname(file) === ".css") {
const data = fs.readFileSync(fullpth, "utf-8");
const res = new CleanCSS({}).minify(data);
fs.writeFileSync(fullpth, res.styles, "utf-8");
}
});
}
try {
mini("public");
console.log("Minify finished");
} catch(err) {
console.error("Minify failed: %o", err);
}