Implement Sass CSS preprocessor
authorNick Downing <nick@ndcode.org>
Wed, 12 Jan 2022 01:21:27 +0000 (12:21 +1100)
committerNick Downing <nick@ndcode.org>
Wed, 12 Jan 2022 01:21:27 +0000 (12:21 +1100)
Site.js
link.sh
package.json

diff --git a/Site.js b/Site.js
index 052e46c..fa5479c 100644 (file)
--- a/Site.js
+++ b/Site.js
@@ -31,6 +31,7 @@ let MinJSCache = require('@ndcode/min_js_cache')
 let MinHTMLCache = require('@ndcode/min_html_cache')
 let MinSVGCache = require('@ndcode/min_svg_cache')
 let Resources = require('./Resources')
+let SassCSSCache = require('@ndcode/sass_css_cache')
 let TextCache = require('@ndcode/text_cache')
 let ZipCache = require('@ndcode/zip_cache')
 let assert = require('assert')
@@ -141,6 +142,12 @@ Site.prototype.start = async function() {
     async () => new MinSVGCache(true)
   )
 
+  assert(this.sass_css_cache === undefined)
+  this.sass_css_cache = await this.resources.ref(
+    `sass_css_cache:${this.root}`,
+    async () => new SassCSSCache(this.root, true)
+  )
+
   assert(this.text_cache === undefined)
   this.text_cache = await this.resources.ref(
     'text_cache',
@@ -182,6 +189,9 @@ Site.prototype.stop = async function() {
   assert(this.min_svg_cache !== undefined)
   await this.resources.unref('min_svg_cache')
 
+  assert(this.sass_css_cache !== undefined)
+  await this.resources.unref(`sass_css_cache:${this.root}`)
+
   assert(this.text_cache !== undefined)
   await this.resources.unref('text_cache')
 
@@ -217,6 +227,9 @@ Site.prototype.kick = async function() {
   assert(this.min_svg_cache !== undefined)
   this.min_svg_cache.kick()
 
+  assert(this.sass_css_cache !== undefined)
+  this.sass_css_cache.kick()
+
   assert(this.text_cache !== undefined)
   this.text_cache.kick()
 
@@ -337,6 +350,10 @@ Site.prototype.get_min_svg = function(pathname) {
   return /*await*/ this.min_svg_cache.get(this.root + pathname)
 }
 
+Site.prototype.get_sass_css = function(pathname) {
+  return /*await*/ this.sass_css_cache.get(this.root + pathname)
+}
+
 Site.prototype.get_text = function(pathname) {
   return /*await*/ this.text_cache.get(this.root + pathname)
 }
@@ -394,8 +411,8 @@ Site.prototype.serve_jst = async function(env, pathname, ...args) {
 Site.prototype.serve_less_css = async function(env, pathname) {
   if (pathname.slice(-9) !== '.css.less')
     return false
-  let data 
+
+  let data
   try {
     data = await this.less_css_cache.get(pathname)
   }
@@ -411,8 +428,8 @@ Site.prototype.serve_less_css = async function(env, pathname) {
 Site.prototype.serve_min_css = async function(env, pathname) {
   if (pathname.slice(-8) !== '.css.min')
     return false
-  let data 
+
+  let data
   try {
     data = await this.min_css_cache.get(pathname)
   }
@@ -428,8 +445,8 @@ Site.prototype.serve_min_css = async function(env, pathname) {
 Site.prototype.serve_min_html = async function(env, pathname) {
   if (pathname.slice(-9) !== '.html.min')
     return false
-  let data 
+
+  let data
   try {
     data = await this.min_html_cache.get(pathname)
   }
@@ -445,8 +462,8 @@ Site.prototype.serve_min_html = async function(env, pathname) {
 Site.prototype.serve_min_js = async function(env, pathname) {
   if (pathname.slice(-7) !== '.js.min')
     return false
-  let data 
+
+  let data
   try {
     data = await this.min_js_cache.get(pathname)
   }
@@ -462,8 +479,8 @@ Site.prototype.serve_min_js = async function(env, pathname) {
 Site.prototype.serve_min_svg = async function(env, pathname) {
   if (pathname.slice(-8) !== '.svg.min')
     return false
-  let data 
+
+  let data
   try {
     data = await this.min_svg_cache.get(pathname)
   }
@@ -476,6 +493,26 @@ Site.prototype.serve_min_svg = async function(env, pathname) {
   return true
 }
 
+Site.prototype.serve_sass_css = async function(env, pathname) {
+  if (
+    pathname.slice(-9) !== '.css.sass' &&
+      pathname.slice(-9) !== '.css.scss'
+  )
+    return false
+
+  let data
+  try {
+    data = await this.sass_css_cache.get(pathname)
+  }
+  catch (err) {
+    if (!(err instanceof Error) || err.code !== 'ENOENT')
+      throw err
+    return false
+  }
+  this.serve(env, 200, data, 'sass_css')
+  return true
+}
+
 Site.prototype.serve_fs = async function(env, pathname) {
   // see serve_internal()
   // since the file may be huge we need to cache it for as long as reasonable
@@ -515,7 +552,7 @@ Site.prototype.serve_fs = async function(env, pathname) {
     )
     env.response.setHeader('Accept-Ranges', 'bytes')
     env.response.setHeader('Content-Length', end - start)
+
     // create video read stream for this particular chunk
     stream = fs.createReadStream(pathname, {start: start, end: end - 1})
   }
@@ -561,7 +598,7 @@ Site.prototype.serve_fs = async function(env, pathname) {
 }
 
 Site.prototype.serve_zip = async function(env, zipname, pathname) {
-  let zip 
+  let zip
   try {
     zip = await this.zip_cache.get(zipname)
   }
@@ -590,6 +627,10 @@ Site.prototype.serve_file = async function(env, pathname) {
     return
   if (await this.serve_min_svg(env, pathname + '.min'))
     return
+  if (await this.serve_sass_css(env, pathname + '.sass'))
+    return
+  if (await this.serve_sass_css(env, pathname + '.scss'))
+    return
   if (await this.serve_fs(env, pathname))
     return
   this.die(env, `file not found ${env.parsed_url.pathname}`)
diff --git a/link.sh b/link.sh
index 0971874..378c8d2 100755 (executable)
--- a/link.sh
+++ b/link.sh
@@ -1,5 +1,5 @@
 #!/bin/sh
 rm -rf node_modules package-lock.json
-npm link @ndcode/json_cache @ndcode/json_cache_rw @ndcode/jst_cache @ndcode/less_css_cache @ndcode/menu_cache @ndcode/min_css_cache @ndcode/min_html_cache @ndcode/min_js_cache @ndcode/min_svg_cache @ndcode/text_cache @ndcode/zip_cache
+npm link @ndcode/json_cache @ndcode/json_cache_rw @ndcode/jst_cache @ndcode/less_css_cache @ndcode/menu_cache @ndcode/min_css_cache @ndcode/min_html_cache @ndcode/min_js_cache @ndcode/min_svg_cache @ndcode/sass_css_cache @ndcode/text_cache @ndcode/zip_cache
 npm install
 npm link
index d118ad8..b7227d0 100644 (file)
     "@ndcode/min_html_cache": "^0.1.0",
     "@ndcode/min_js_cache": "^0.1.0",
     "@ndcode/min_svg_cache": "^0.1.0",
+    "@ndcode/sass_css_cache": "^0.1.0",
     "@ndcode/text_cache": "^0.1.0",
     "@ndcode/zip_cache": "^0.1.0",
     "querystring": "^0.2.0",
     "socket.io": "^2.1.1",
     "url": "^0.11.0"
   },
-  "devDependencies": {},
   "scripts": {},
   "maintainers": "Nick Downing <nick@ndcode.org>",
   "license": "MIT"