Provide get_html_min() similar to get_js_min() and friends, using html-minifier
authorNick Downing <nick@ndcode.org>
Sun, 18 Nov 2018 01:01:45 +0000 (12:01 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 18 Nov 2018 01:01:45 +0000 (12:01 +1100)
Server.js
SiteRoot.js
package.json

index 3fcb4eb..ca7616b 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -7,6 +7,7 @@ let assert = require('assert')
 let disk_build = require('@ndcode/disk_build')
 let emailjs = require('emailjs')
 let fs = require('fs')
+let html_minifier = require('html-minifier')
 let jst = require('@ndcode/jst')
 let less = require('less/lib/less-node')
 let path = require('path')
@@ -75,6 +76,7 @@ let Server = function() {
   this.build_cache_json = new BuildCache()
   this.build_cache_css_less = new BuildCache()
   this.build_cache_css_min = new BuildCache()
+  this.build_cache_html_min = new BuildCache()
   this.build_cache_js_min = new BuildCache()
   this.build_cache_svg_min = new BuildCache()
   this.build_cache_text = new BuildCache()
@@ -178,6 +180,29 @@ Server.prototype.get_css_min = function(pathname) {
   )
 }
 
+Server.prototype.get_html_min = function(pathname) {
+  return /*await*/ this.build_cache_html_min.get(
+    pathname,
+    async result => {
+      result.value = fs_readFile(
+        await disk_build(
+          pathname,
+          async built_pathname => {
+            console.log('getting', pathname, 'as html_min')
+            let text = await fs_readFile(pathname, {encoding: 'utf-8'})
+            console.log('building', built_pathname)
+            return /*await*/ fs_writeFile(
+              built_pathname,
+              html_minifier.minify(text, {collapseWhitespace: true}),
+              {encoding: 'utf-8'}
+            )
+          }
+        )
+      )
+    }
+  )
+}
+
 Server.prototype.get_js_min = function(pathname) {
   return /*await*/ this.build_cache_js_min.get(
     pathname,
index c7657b6..893f69a 100644 (file)
@@ -30,6 +30,9 @@ SiteRoot.prototype.get_css_less = function(pathname) {
 SiteRoot.prototype.get_css_min = function(pathname) {
   return /*await*/ this.server.get_css_min(this.root + pathname)
 }
+SiteRoot.prototype.get_html_min = function(pathname) {
+  return /*await*/ this.server.get_html_min(this.root + pathname)
+}
 SiteRoot.prototype.get_js_min = function(pathname) {
   return /*await*/ this.server.get_js_min(this.root + pathname)
 }
index 9e22a2a..76b0e65 100644 (file)
@@ -15,6 +15,7 @@
     "cookie": "^0.3.1",
     "emailjs": "^2.2.0",
     "fs": "^0.0.1-security",
+    "html-minifier": "^3.5.21",
     "http": "^0.0.0",
     "https": "^1.0.0",
     "less": "^3.8.1",