-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.js
43 lines (37 loc) · 1.15 KB
/
compress.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
/**
* 压缩vuepress生成的html
*/
const fs = require('fs');
const path = require('path')
const minifier = require('html-minifier').minify;
const minifierRules = {
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
collapseWhitespace: true,
removeComments: true,
minifyJS: true,
minifyCSS: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true
}
const output = path.resolve(__dirname, 'output');
function minifyHtml(dir) {
fs.readdir(dir, (err, files) => {
if (err) throw err;
files.forEach(file => {
// 判断是否为文件夹,如果是文件夹,则递归展示
const filePath = path.resolve(dir, file);
if (fs.statSync(filePath).isDirectory()) {
minifyHtml(filePath);
return;
}
if (!file.endsWith('.html')) {
return;
}
let source = fs.readFileSync(filePath, 'utf8');
let result = minifier(source, minifierRules);
fs.writeFileSync(filePath, result);
});
});
}
minifyHtml(path.resolve(__dirname, './public'))