main, begin to minify HTML.
This commit is contained in:
1 parent
ed934ea5d9
commit
25ff248b92
2 files changed
+23
-5
No files matched your search
@@ -14,6 +14,8 @@ const execSync = require('child_process').execSync;
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const { minify } = require('html-minifier-terser');
|
||||
|
||||
//// Global Variables ////
|
||||
|
||||
const DEBUG = false;
|
||||
@@ -44,6 +46,18 @@ const stringsToRemove = [
|
||||
|
||||
//// 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.
|
||||
*/
|
||||
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"
|
||||
for (let key in pages) {
|
||||
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);
|
||||
let html = "" + execSync(pages[key]);
|
||||
stringsToRemove.forEach(string => {
|
||||
html = html.replace(string, "");
|
||||
});
|
||||
html = await mini(html);
|
||||
res.send(html);
|
||||
});
|
||||
|
||||
@@ -199,9 +214,10 @@ async function main() {
|
||||
// Provide human-usable sitemap links.
|
||||
// TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE"
|
||||
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)
|
||||
res.send(sitemap_html);
|
||||
html = await mini(sitemap_html);
|
||||
res.send(mini(html));
|
||||
});
|
||||
|
||||
// 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!
|
||||
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.
|
||||
//console.log(req);
|
||||
//console.log(res);
|
||||
@@ -273,6 +289,7 @@ async function main() {
|
||||
stringsToRemove.forEach(string => {
|
||||
html = html.replace(string, "");
|
||||
});
|
||||
html = await mini(html);
|
||||
res.send(html);
|
||||
});
|
||||
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"express": ">=4.18.1 < 5.0.0"
|
||||
"express": ">=4.18.1 < 5.0.0",
|
||||
"html-minifier-terser": "^7.2.0"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "./run.sh"
|
||||
|
||||
Reference in new issue
Block a user