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')
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)
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()
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,
{
)
}
-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,
{
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)
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
}
//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