-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (45 loc) · 1.64 KB
/
index.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
"use strict";
const fs = require("fs");
const path = require("path");
const htmlMinify = require("html-minifier").minify;
const UglifyJS = require("uglify-js");
const CleanCSS = require("clean-css");
const cleanCSS = new CleanCSS({ level: { 1: { specialComments: 0 } } });
function minify(dirPath, book) {
fs.readdir(dirPath, (err, files) => {
if (err) {
throw err;
}
files
.map(fileName => path.join(dirPath, fileName))
.forEach(filePath => {
if (fs.statSync(filePath).isDirectory()) {
minify(filePath, book);
} else if (fs.statSync(filePath).isFile()) {
if (filePath.match(/\.html$/) !== null) {
book.log.debug.ln('html-minifier "' + filePath + '"');
const html = fs.readFileSync(filePath, "utf8");
const minified = htmlMinify(html, { minifyCSS: true, minifyJS: true });
fs.writeFileSync(filePath, minified);
} else if (filePath.match(/(?<!\.min)\.js$/) !== null) {
book.log.debug.ln('js-minifier "' + filePath + '"');
const js = fs.readFileSync(filePath, "utf8");
const minified = UglifyJS.minify(js);
fs.writeFileSync(filePath, minified.code);
} else if (filePath.match(/(?<!\.min)\.css$/) !== null) {
book.log.debug.ln('css-minifier "' + filePath + '"');
const css = fs.readFileSync(filePath, "utf8");
const minified = cleanCSS.minify(css);
fs.writeFileSync(filePath, minified.styles);
}
}
});
});
}
module.exports = {
hooks: {
finish: function() {
minify(this.output.root(), this);
}
}
};