-
Notifications
You must be signed in to change notification settings - Fork 293
/
minify.js
44 lines (41 loc) · 1.03 KB
/
minify.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
const UglifyJS = require("uglify-js");
const UglifyCSS = require("uglifycss");
const fs = require("fs");
const baseDir = "./lib";
function uglifyJS(fileName) {
fs.writeFileSync(
fileName,
UglifyJS.minify(
{ [fileName]: fs.readFileSync(fileName, "utf8") },
{ mangle: false }
).code,
"utf8"
);
}
function uglifyCSS(fileName) {
fs.writeFileSync(fileName, UglifyCSS.processFiles([fileName]), "utf8");
}
const patterm = {
js: "(.*?).(js)$",
css: "(.*?).(css)$",
dir: "^([^.]+)$"
};
function goThroughAllFiles(dir) {
const filesAndMaybeDir = fs.readdirSync(dir);
filesAndMaybeDir.forEach(path => {
const fileName = `${dir}/${path}`;
if (path.match(patterm.js)) {
console.log(`uglifing ${fileName}`);
uglifyJS(fileName);
}
if (path.match(patterm.css)) {
console.log(`uglifing ${fileName}`);
uglifyCSS(fileName);
}
if (path.match(patterm.dir)) {
console.log(`Found dir ${path}`);
goThroughAllFiles(`${dir}/${path}`);
}
});
}
goThroughAllFiles(baseDir);