add minifiycss

This commit is contained in:
Ashley 2023-03-11 14:34:31 +00:00
parent edbe41b078
commit 72820de13e

View file

@ -62,8 +62,31 @@ module.exports = function (app, config, renderTemplate) {
}); });
}); });
const path = require("path");
const fs = require("fs");
const CleanCSS = require("clean-css");
const cssDir = "./css/";
app.get("/css/:id", (req, res) => { app.get("/css/:id", (req, res) => {
const filePath = path.join(cssDir, req.params.id);
if (!fs.existsSync(filePath)) {
res.status(404).send("File not found");
return;
}
if (req.params.id.endsWith(".css")) {
// Minimize the CSS file
const css = fs.readFileSync(filePath, "utf8");
const minimizedCss = new CleanCSS().minify(css).styles;
// Serve the minimized CSS file
res.header("Content-Type", "text/css");
res.send(minimizedCss);
} else {
// Serve the original file
res.sendFile(req.params.id, { root: html_location }); res.sendFile(req.params.id, { root: html_location });
}
}); });
}; };