forked from GitSquared/edex-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild-minify.js
82 lines (77 loc) · 3.06 KB
/
prebuild-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
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
80
81
82
const fs = require("fs");
const path = require("path");
const UglifyJS = require("uglify-es");
const CleanCSS = require("clean-css");
JSON.minify = require("node-json-minify");
const writeMinified = (path, data) => {
fs.writeFile(path, data, (err) => {
if (err) {
console.log(path+" - ❌");
console.log("");
console.log("");
throw err;
}
console.log(path+" - ✓");
});
};
const recursiveMinify = (dirPath) => {
try { var files = fs.readdirSync(dirPath); }
catch(e) { return; }
if (files.length > 0) {
for (let i = 0; i < files.length; i++) {
let filePath = dirPath + '/' + files[i];
if (fs.statSync(filePath).isFile()) {
// Grid.json is a massive file that's already pre-minified. Do not process.
if (filePath.endsWith("grid.json")) return;
switch (filePath.split(".").pop()) {
case "js":
let minified = UglifyJS.minify(fs.readFileSync(filePath, {encoding: "utf-8"}), {
compress: {
dead_code: false,
unused: false,
warnings: true
},
output: {
beautify: false,
ecma: 6
}
});
if (!minified.error) {
writeMinified(filePath, minified.code);
break;
}
else {
console.log(filePath+" - ❌");
console.log("");
console.log("");
throw minified.error;
}
case "css":
let output = new CleanCSS({level:2}).minify(fs.readFileSync(filePath, {encoding:"utf-8"}));
if (output.errors.length >= 1) {
console.log(filePath+" - ❌");
console.log("");
console.log("");
throw output.errors;
} else {
writeMinified(filePath, output.styles);
break;
}
case "json":
try {
writeMinified(filePath, JSON.minify(fs.readFileSync(filePath, {encoding:"utf-8"})));
break;
} catch(err) {
console.log(filePath+" - ❌");
console.log("");
console.log("");
throw err;
}
}
} else {
recursiveMinify(filePath);
}
}
}
};
recursiveMinify(path.join(__dirname, "prebuild-src"));