Add *.css.min support similar to the *.js.min support, rename less and js stuff
authorNick Downing <nick@ndcode.org>
Sat, 17 Nov 2018 07:32:35 +0000 (18:32 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 17 Nov 2018 07:32:47 +0000 (18:32 +1100)
Server.js
SiteRoot.js
package.json

index f69479c..c8d389b 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -1,4 +1,5 @@
 let BuildCache = require('@ndcode/build_cache')
+let CleanCSS = require('clean-css')
 let JSONCache = require('@ndcode/json_cache')
 let Site = require('./Site')
 let assert = require('assert')
@@ -17,6 +18,7 @@ let zetjs = require('zetjs')
 let fs_mkdir = util.promisify(fs.mkdir)
 let fs_readFile = util.promisify(fs.readFile)
 let yauzl_open = util.promisify(yauzl.open)
+let clean_css = new CleanCSS({returnPromise: true})
 
 let Server = function() {
   if (!this instanceof Server)
@@ -28,8 +30,9 @@ let Server = function() {
 
   this.build_cache_email = new BuildCache()
   this.build_cache_json = new BuildCache()
-  this.build_cache_less = new BuildCache()
-  this.build_cache_min = new BuildCache()
+  this.build_cache_css_less = new BuildCache()
+  this.build_cache_css_min = new BuildCache()
+  this.build_cache_js_min = new BuildCache()
   this.build_cache_text = new BuildCache()
   this.build_cache_zet = new BuildCache()
   this.build_cache_zip = new BuildCache()
@@ -68,13 +71,13 @@ Server.prototype.get_jst = function(pathname, root) {
   return /*await*/ jst(pathname, root, {_server: this})
 }
 
-Server.prototype.get_less = function(pathname, root) {
+Server.prototype.get_css_less = function(pathname, root) {
   let dirname = path.posix.dirname(pathname)
-  return /*await*/ this.build_cache_less.get(
+  return /*await*/ this.build_cache_css_less.get(
     pathname,
     async result => {
       let text = await fs_readFile(pathname, {encoding: 'utf-8'})
-      console.log('getting', pathname, 'as less')
+      console.log('getting', pathname, 'as css_less')
       let render = await less.render(
         text,
         {
@@ -105,13 +108,25 @@ Server.prototype.get_less = function(pathname, root) {
   )
 }
 
-Server.prototype.get_min = function(pathname) {
-  return /*await*/ this.build_cache_min.get(
+Server.prototype.get_css_min = function(pathname) {
+  return /*await*/ this.build_cache_css_min.get(
+    pathname,
+    async result => {
+      let text = await fs_readFile(pathname, {encoding: 'utf-8'})
+      console.log('getting', pathname, 'as css_min')
+      let render = await clean_css.minify(text)
+      result.value = Buffer.from(render.styles)
+    }
+  )
+}
+
+Server.prototype.get_js_min = function(pathname) {
+  return /*await*/ this.build_cache_js_min.get(
     pathname,
     async result => {
       let files = {}
       files[pathname] = await fs_readFile(pathname, {encoding: 'utf-8'})
-      console.log('getting', pathname, 'as min')
+      console.log('getting', pathname, 'as js_min')
       let render = uglify_es.minify(
         files,
         {
index 0090a1e..840d19e 100644 (file)
@@ -24,11 +24,14 @@ SiteRoot.prototype.get_json = function(pathname) {
 SiteRoot.prototype.get_jst = function(pathname) {
   return /*await*/ this.server.get_jst(this.root + pathname, this.root)
 }
-SiteRoot.prototype.get_less = function(pathname) {
-  return /*await*/ this.server.get_less(this.root + pathname, this.root)
+SiteRoot.prototype.get_css_less = function(pathname) {
+  return /*await*/ this.server.get_css_less(this.root + pathname, this.root)
 }
-SiteRoot.prototype.get_min = function(pathname) {
-  return /*await*/ this.server.get_min(this.root + pathname)
+SiteRoot.prototype.get_css_min = function(pathname) {
+  return /*await*/ this.server.get_css_min(this.root + pathname)
+}
+SiteRoot.prototype.get_js_min = function(pathname) {
+  return /*await*/ this.server.get_js_min(this.root + pathname)
 }
 SiteRoot.prototype.get_text = function(pathname) {
   return /*await*/ this.server.get_text(this.root + pathname)
@@ -83,37 +86,54 @@ SiteRoot.prototype.serve_jst = async function(env, pathname, ...args) {
   return true
 }
 
-SiteRoot.prototype.serve_less = async function(env, pathname) {
+SiteRoot.prototype.serve_css_less = async function(env, pathname) {
   if (pathname.slice(-9) !== '.css.less')
     return false
  
   let data 
   try {
-    data = await this.server.get_less(pathname, this.root)
+    data = await this.server.get_css_less(pathname, this.root)
   }
   catch (err) {
     if (!(err instanceof Error) || err.code !== 'ENOENT')
       throw err
     return false
   }
-  this.serve(env, 200, data, 'less')
+  this.serve(env, 200, data, 'css_less')
   return true
 }
 
-SiteRoot.prototype.serve_min = async function(env, pathname) {
+SiteRoot.prototype.serve_css_min = async function(env, pathname) {
+  if (pathname.slice(-8) !== '.css.min')
+    return false
+  let data 
+  try {
+    data = await this.server.get_css_min(pathname)
+  }
+  catch (err) {
+    if (!(err instanceof Error) || err.code !== 'ENOENT')
+      throw err
+    return false
+  }
+  this.serve(env, 200, data, 'css_min')
+  return true
+}
+
+SiteRoot.prototype.serve_js_min = async function(env, pathname) {
   if (pathname.slice(-7) !== '.js.min')
     return false
  
   let data 
   try {
-    data = await this.server.get_min(pathname)
+    data = await this.server.get_js_min(pathname)
   }
   catch (err) {
     if (!(err instanceof Error) || err.code !== 'ENOENT')
       throw err
     return false
   }
-  this.serve(env, 200, data, 'min')
+  this.serve(env, 200, data, 'js_min')
   return true
 }
 
@@ -151,9 +171,11 @@ SiteRoot.prototype.serve_file = async function(env, pathname) {
   //console.log(`serve_file ${pathname}`)
   if (await this.serve_jst(env, pathname + '.jst'))
     return
-  if (await this.serve_less(env, pathname + '.less'))
+  if (await this.serve_css_less(env, pathname + '.less'))
+    return
+  if (await this.serve_css_min(env, pathname + '.min'))
     return
-  if (await this.serve_min(env, pathname + '.min'))
+  if (await this.serve_js_min(env, pathname + '.min'))
     return
   if (await this.serve_fs(env, pathname))
     return
index 35e98e4..116bd57 100644 (file)
@@ -9,6 +9,7 @@
     "@ndcode/json_cache": "^0.1.0",
     "@ndcode/jst": "^0.1.2",
     "babel-cli": "^6.26.0",
+    "clean-css": "^4.2.1",
     "commander": "^2.18.0",
     "cookie": "^0.3.1",
     "emailjs": "^2.2.0",