edex-ui/prebuild-minify.js

93 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-10-11 14:02:50 +00:00
const fs = require("fs");
const path = require("path");
2019-02-06 17:52:17 +00:00
const stdout = process.stdout;
2020-12-21 10:15:25 +00:00
const UglifyJS = require("terser");
2018-01-05 10:44:59 +00:00
const CleanCSS = require("clean-css");
2017-10-11 14:02:50 +00:00
JSON.minify = require("node-json-minify");
2019-02-06 17:52:17 +00:00
function writeMinified(path, data) {
return new Promise((res, rej) => {
fs.writeFile(path, data, (err) => {
if (err) {
stdout.write(" - ❌\n\n\n", () => {
rej(err);
});
}
stdout.write(" - ✓\n", () => {
res();
});
});
2017-10-11 14:02:50 +00:00
});
2019-02-06 17:52:17 +00:00
}
2017-10-11 14:02:50 +00:00
2019-02-06 17:52:17 +00:00
async function recursiveMinify(dirPath) {
2017-10-11 14:02:50 +00:00
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()) {
// Do not process grid.json because it's heavy and pre-minified, and themes and keyboard files to leave them in a human-readable state
2019-02-06 17:52:17 +00:00
if (filePath.endsWith(".json") && !filePath.endsWith("icons.json")) return;
// See #446
if (filePath.endsWith("file-icons-match.js")) return;
await stdout.write(filePath.slice(filePath.indexOf('prebuild-src/')+13)+'...');
2018-01-05 10:44:59 +00:00
switch (filePath.split(".").pop()) {
2017-10-11 14:02:50 +00:00
case "js":
2020-12-21 10:15:25 +00:00
let minified = await UglifyJS.minify(fs.readFileSync(filePath, {encoding: "utf-8"}), {
2018-01-05 10:44:59 +00:00
compress: {
dead_code: false,
unused: false,
warnings: true
},
output: {
beautify: false,
ecma: 6
}
});
if (!minified.error) {
2019-02-06 17:52:17 +00:00
await writeMinified(filePath, minified.code).catch(e => {
throw e;
});
} else {
stdout.write(" - ❌\n\n\n");
2018-01-05 11:08:29 +00:00
throw minified.error;
2018-01-05 10:44:59 +00:00
}
2019-02-06 17:52:17 +00:00
break;
2017-10-11 14:02:50 +00:00
case "css":
2018-01-05 10:44:59 +00:00
let output = new CleanCSS({level:2}).minify(fs.readFileSync(filePath, {encoding:"utf-8"}));
if (output.errors.length >= 1) {
2019-02-06 17:52:17 +00:00
stdout.write(" - ❌\n\n\n");
2018-01-05 11:08:29 +00:00
throw output.errors;
2018-01-05 10:44:59 +00:00
} else {
2019-02-06 17:52:17 +00:00
await writeMinified(filePath, output.styles).catch(e => {
throw e;
});
2018-01-05 10:44:59 +00:00
}
2019-02-06 17:52:17 +00:00
break;
2017-10-11 14:02:50 +00:00
case "json":
2019-02-06 17:52:17 +00:00
let out;
2018-01-05 10:44:59 +00:00
try {
2019-02-06 17:52:17 +00:00
out = JSON.minify(fs.readFileSync(filePath, {encoding:"utf-8"}));
2018-01-05 10:44:59 +00:00
} catch(err) {
2019-02-06 17:52:17 +00:00
stdout.write(" - ❌\n\n\n");
2018-01-05 10:44:59 +00:00
throw err;
}
2019-02-06 17:52:17 +00:00
await writeMinified(filePath, out).catch(e => {
throw e;
});
break;
default:
stdout.write("\n");
2017-10-11 14:02:50 +00:00
}
} else {
2019-02-06 17:52:17 +00:00
await recursiveMinify(filePath);
2017-10-11 14:02:50 +00:00
}
}
}
2019-02-06 17:52:17 +00:00
}
2017-10-11 14:02:50 +00:00
recursiveMinify(path.join(__dirname, "prebuild-src"));