Skip to content

Instantly share code, notes, and snippets.

@LeonMrBonnie
Last active May 16, 2022 11:22
Show Gist options
  • Save LeonMrBonnie/c9816ae8efd333a010d6cb624be0c37a to your computer and use it in GitHub Desktop.
Save LeonMrBonnie/c9816ae8efd333a010d6cb624be0c37a to your computer and use it in GitHub Desktop.
import fs from "fs";
import path from "path";
import walk from "walk";
import terser from "terser";
const CLIENT_FILES_PATH = "./resources/main/client";
const OUTPUT_DIR = "client_minified";
let walker = walk.walk(CLIENT_FILES_PATH, { followLinks: false });
let files = [];
walker.on("file", (root, stat, next) => {
files.push(root + "/" + stat.name);
next();
});
walker.on("end", () => {
files.forEach(async (file) => {
try {
let source = await fs.promises.readFile(file);
let minified = terser.minify(source.toString(), {
ecma: 8,
keep_classnames: true,
keep_fnames: true,
mangle: false,
compress: {
ecma: 8,
},
});
if (minified.error)
return console.error(
`Error while minifying '${file}': ${minified.error.stack}`
);
let output = file.replace("client", OUTPUT_DIR);
let dirs = path.join(output, "..");
await fs.promises.mkdir(dirs, { recursive: true });
await fs.promises.writeFile(output, minified.code);
console.log(`Minfied '${file}'`);
} catch (e) {
console.error(`Error while writing '${file}': ${e.stack}`);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment