main, begin to minify HTML.

This commit is contained in:
me committed 2026-10-29 21:53:08 -07:00
1 parent ed934ea5d9
commit 25ff248b92
2 files changed
+23 -5

No files matched your search

+21 -4
View File
@@ -14,6 +14,8 @@ const execSync = require('child_process').execSync;
const fs = require('fs'); const fs = require('fs');
const { minify } = require('html-minifier-terser');
//// Global Variables //// //// Global Variables ////
const DEBUG = false; const DEBUG = false;
@@ -44,6 +46,18 @@ const stringsToRemove = [
//// Functions //// //// Functions ////
async function mini(html) {
//console.log(`html=${html}`);
const minified = await minify(html, {
collapseWhitespace: true,
removeComments: true,
minifyJS: true,
minifyCSS: true
});
//console.log(`minified=${minified}`);
return minified.toString();
}
/* Code exists inside a main function so that we may use async/await. /* Code exists inside a main function so that we may use async/await.
*/ */
async function main() { async function main() {
@@ -149,12 +163,13 @@ async function main() {
// TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE" // TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE"
for (let key in pages) { for (let key in pages) {
console.log(" * Creating router for", key); console.log(" * Creating router for", key);
router.get("/" + key, function (req,res) { router.get("/" + key, async function (req,res) {
console.log(key, "fulfilling request to", req.socket.remoteAddress, "asking for", req.url); console.log(key, "fulfilling request to", req.socket.remoteAddress, "asking for", req.url);
let html = "" + execSync(pages[key]); let html = "" + execSync(pages[key]);
stringsToRemove.forEach(string => { stringsToRemove.forEach(string => {
html = html.replace(string, ""); html = html.replace(string, "");
}); });
html = await mini(html);
res.send(html); res.send(html);
}); });
@@ -199,9 +214,10 @@ async function main() {
// Provide human-usable sitemap links. // Provide human-usable sitemap links.
// TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE" // TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE"
console.log(" * Creating router for sitemap*"); console.log(" * Creating router for sitemap*");
router.get('/sitemap*', function (req, res) { router.get('/sitemap*', async function (req, res) {
console.log("sitemap.html being provided to", req.socket.remoteAddress) console.log("sitemap.html being provided to", req.socket.remoteAddress)
res.send(sitemap_html); html = await mini(sitemap_html);
res.send(mini(html));
}); });
// Provide a robots.txt for where search engines should find things. // Provide a robots.txt for where search engines should find things.
@@ -264,7 +280,7 @@ async function main() {
// Originally a test, now a catch-all redirection to Home! // Originally a test, now a catch-all redirection to Home!
console.log(" * Creating router for redirection"); console.log(" * Creating router for redirection");
router.get('/*', function (req, res) { router.get('/*', async function (req, res) {
// WARNING: These are huge so only look when you need to. // WARNING: These are huge so only look when you need to.
//console.log(req); //console.log(req);
//console.log(res); //console.log(res);
@@ -273,6 +289,7 @@ async function main() {
stringsToRemove.forEach(string => { stringsToRemove.forEach(string => {
html = html.replace(string, ""); html = html.replace(string, "");
}); });
html = await mini(html);
res.send(html); res.send(html);
}); });
+2 -1
View File
@@ -1,6 +1,7 @@
{ {
"dependencies": { "dependencies": {
"express": ">=4.18.1 < 5.0.0" "express": ">=4.18.1 < 5.0.0",
"html-minifier-terser": "^7.2.0"
}, },
"scripts": { "scripts": {
"start": "./run.sh" "start": "./run.sh"