auto find and minify js in static folder

This commit is contained in:
Kentai Radiquum 2025-01-31 21:57:12 +05:00
parent 070a581d7b
commit 5aba7e3b60
Signed by: Radiquum
GPG key ID: 858E8EE696525EED

View file

@ -32,8 +32,8 @@ let videos: string[] = [];
let config = { let config = {
endpoint: process.env.ENDPOINT, endpoint: process.env.ENDPOINT,
bucket: process.env.BUCKET, bucket: process.env.BUCKET,
prefix: process.env.PREFIX prefix: process.env.PREFIX,
} };
async function listAllObjects(bucketName: string, prefix: string) { async function listAllObjects(bucketName: string, prefix: string) {
let isTruncated: boolean = true; let isTruncated: boolean = true;
let continuationToken: string = ""; let continuationToken: string = "";
@ -67,8 +67,14 @@ async function listAllObjects(bucketName: string, prefix: string) {
if (!fs.existsSync(".cache")) fs.mkdirSync(".cache"); if (!fs.existsSync(".cache")) fs.mkdirSync(".cache");
if (!fs.existsSync(".cache/objects.json")) { if (!fs.existsSync(".cache/objects.json")) {
log.info("Listing all objects in S3 bucket . . ."); log.info("Listing all objects in S3 bucket . . .");
await listAllObjects(process.env.BUCKET as string, process.env.PREFIX as string); await listAllObjects(
fs.writeFileSync(".cache/objects.json", JSON.stringify({ config, images, videos })); process.env.BUCKET as string,
process.env.PREFIX as string
);
fs.writeFileSync(
".cache/objects.json",
JSON.stringify({ config, images, videos })
);
log.info( log.info(
`Total: ${images.length + videos.length} | ${images.length} Images | ${ `Total: ${images.length + videos.length} | ${images.length} Images | ${
videos.length videos.length
@ -79,7 +85,7 @@ if (!fs.existsSync(".cache/objects.json")) {
const _cacheFile = fs.readFileSync(".cache/objects.json", { const _cacheFile = fs.readFileSync(".cache/objects.json", {
encoding: "utf-8", encoding: "utf-8",
}); });
config = JSON.parse(_cacheFile)["config"] config = JSON.parse(_cacheFile)["config"];
images = JSON.parse(_cacheFile)["images"]; images = JSON.parse(_cacheFile)["images"];
videos = JSON.parse(_cacheFile)["videos"]; videos = JSON.parse(_cacheFile)["videos"];
log.info( log.info(
@ -149,10 +155,17 @@ if (environment == "dev") {
minifyWhitespace: true, minifyWhitespace: true,
}); });
const populate_index_file = fs.readFileSync("src/static/populate_index.js", { const files = fs.readdirSync("src/static");
encoding: "utf-8", const minify = files.filter((file) => file.endsWith(".js"));
minify.forEach((file) => {
const ext = file.split(".")[file.split(".").length - 1];
const name = file.split(".")[0];
const orFile = fs.readFileSync(`src/static/${file}`, {
encoding: "utf-8",
});
const minFile = transpiler.transformSync(orFile);
fs.writeFileSync(`out/static/${name}.min.${ext}`, minFile);
}); });
const populate_index_minified = transpiler.transformSync(populate_index_file);
fs.writeFileSync("out/static/populate_index.min.js", populate_index_minified);
} }
fs.writeFileSync("out/index.html", `<!DOCTYPE html>${html}`); fs.writeFileSync("out/index.html", `<!DOCTYPE html>${html}`);