let BuildCache = require('@ndcode/build_cache')
-let CleanCSS = require('@ndcode/clean-css')
let JSONCache = require('@ndcode/json_cache')
+let JSTCache = require('@ndcode/jst_cache')
+let LessCSSCache = require('@ndcode/less_css_cache')
+let MinCSSCache = require('@ndcode/min_css_cache')
+let MinJSCache = require('@ndcode/min_js_cache')
+let MinHTMLCache = require('@ndcode/min_html_cache')
+let MinSVGCache = require('@ndcode/min_svg_cache')
let Site = require('./Site')
-let SVGO = require('svgo')
+let TextCache = require('@ndcode/text_cache')
+let ZipCache = require('@ndcode/zip_cache')
let assert = require('assert')
let disk_build = require('@ndcode/disk_build')
-//let emailjs = require('emailjs')
let fs = require('fs')
-let html_minifier = require('@ndcode/html-minifier')
-let jst = require('@ndcode/jst')
-let less = require('less/lib/less-node')
let path = require('path')
-let stream_buffers = require('stream-buffers')
-let uglify_es = require('uglify-es')
let util = require('util')
-let yauzl = require('yauzl')
-//let zettair = require('@ndcode/zettair')
let fs_mkdir = util.promisify(fs.mkdir)
let fs_readFile = util.promisify(fs.readFile)
let fs_stat = util.promisify(fs.stat)
let fs_writeFile = util.promisify(fs.writeFile)
-let yauzl_open = util.promisify(yauzl.open)
-let clean_css = new CleanCSS({returnPromise: true})
-let svgo = new SVGO(
- {
- plugins: [
- {cleanupAttrs: true},
- {removeDoctype: true},
- {removeXMLProcInst: true},
- {removeComments: true},
- {removeMetadata: true},
- {removeTitle: true},
- {removeDesc: true},
- {removeUselessDefs: true},
- {removeEditorsNSData: true},
- {removeEmptyAttrs: true},
- {removeHiddenElems: true},
- {removeEmptyText: true},
- {removeEmptyContainers: true},
- {removeViewBox: false},
- {cleanupEnableBackground: true},
- {convertStyleToAttrs: true},
- {convertColors: true},
- {convertPathData: true},
- {convertTransform: true},
- {removeUnknownsAndDefaults: true},
- {removeNonInheritableGroupAttrs: true},
- {removeUselessStrokeAndFill: true},
- {removeUnusedNS: true},
- {cleanupIDs: true},
- {cleanupNumericValues: true},
- {moveElemsAttrsToGroup: true},
- {moveGroupAttrsToElems: true},
- {collapseGroups: true},
- {removeRasterImages: false},
- {mergePaths: true},
- {convertShapeToPath: true},
- {sortAttrs: true},
- {removeDimensions: true}//,
- //{removeAttrs: {attrs: '(stroke|fill)'}}
- ]
- }
-)
let SiteRoot = function(server, root) {
if (!this instanceof SiteRoot)
Site.call(this, server)
this.root = root
- this.build_cache_email = new BuildCache()
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()
- this.build_cache_zet = new BuildCache()
- this.build_cache_zip = new BuildCache()
this.json_cache = new JSONCache(true)
+ this.jst_cache = new JSTCache(root, {_server: server, _site: this}, true)
+ this.less_css_cache = new LessCSSCache(root, true)
+ this.min_css_cache = new MinCSSCache(true)
+ this.min_js_cache = new MinJSCache(true)
+ this.min_html_cache = new MinHTMLCache(true)
+ this.min_svg_cache = new MinSVGCache(true)
+ this.text_cache = new TextCache(true)
+ this.zip_cache = new ZipCache(true)
}
SiteRoot.prototype = Object.create(Site.prototype)
-//SiteRoot.prototype.internal_get_email = function(pathname) {
-// return /*await*/ this.build_cache_email.get(
-// pathname,
-// async result => {
-// let text = await fs_readFile(pathname, {encoding: 'utf-8'})
-// console.log('getting', pathname, 'as email')
-// result.value = emailjs.server.connect(JSON.parse(text))
-// }
-// )
-//}
-
// this is for read-only JSON files
// they will be reloaded from disk if modified
SiteRoot.prototype.internal_get_json = function(pathname) {
)
}
-SiteRoot.prototype.internal_get_jst = function(pathname, root) {
- return /*await*/ jst(pathname, root, {_server: this.server, _site: this})
-}
-
-SiteRoot.prototype.internal_get_css_less = function(pathname, root) {
- return /*await*/ this.build_cache_css_less.get(
- pathname,
- async result => {
- let render = await disk_build(
- pathname,
- async temp_pathname => {
- let render = await less.render(
- await fs_readFile(pathname, {encoding: 'utf-8'}),
- {
- //color: true,
- compress: true,
- //depends: false,
- filename: pathname,
- //globalVars: null,
- //ieCompat: false,
- //insecure: false,
- //javascriptEnabled: false,
- //lint: false,
- //math: 0,
- //modifyVars: null,
- pathnames: [path.posix.dirname(pathname)],
- //plugins: [],
- //reUsePluginManager: true,
- //rewriteUrls: false,
- rootpathname: root//,
- //strictImports: false,
- //strictUnits: false,
- //urlArgs: ''
- }
- )
- await fs_writeFile(
- temp_pathname,
- render.css,
- {encoding: 'utf-8'}
- )
- return render.imports
- },
- true
- )
- console.log('getting', pathname, 'as css_less')
- result.deps = result.deps.concat(render.deps)
- result.value = await fs_readFile(render.pathname)
- }
- )
-}
-
-SiteRoot.prototype.internal_get_css_min = function(pathname) {
- return /*await*/ this.build_cache_css_min.get(
- pathname,
- async result => {
- let render = await disk_build(
- pathname,
- async temp_pathname => {
- let render = await clean_css.minify(
- await fs_readFile(pathname, {encoding: 'utf-8'})
- )
- for (let i = 0; i < render.warnings.length; ++i)
- console.log(`clean-css warning: ${render.warnings[i]}`)
- return /*await*/ fs_writeFile(
- temp_pathname,
- render.styles,
- {encoding: 'utf-8'}
- )
- },
- true
- )
- console.log('getting', pathname, 'as css_min')
- assert(render.deps.length === 0)
- result.value = await fs_readFile(render.pathname)
- }
- )
-}
-
-SiteRoot.prototype.internal_get_html_min = function(pathname) {
- return /*await*/ this.build_cache_html_min.get(
- pathname,
- async result => {
- let render = await disk_build(
- pathname,
- async temp_pathname => /*await*/ fs_writeFile(
- temp_pathname,
- html_minifier.minify(
- await fs_readFile(pathname, {encoding: 'utf-8'}),
- {
- collapseWhitespace: true,
- minifyCSS: true,
- minifyJS: true
- }
- ),
- {encoding: 'utf-8'}
- ),
- true
- )
- console.log('getting', pathname, 'as html_min')
- assert(render.deps.length === 0)
- result.value = await fs_readFile(render.pathname)
- }
- )
-}
-
-SiteRoot.prototype.internal_get_js_min = function(pathname) {
- return /*await*/ this.build_cache_js_min.get(
- pathname,
- async result => {
- let render = await disk_build(
- pathname,
- async temp_pathname => {
- let files = {}
- files[pathname] = await fs_readFile(pathname, {encoding: 'utf-8'})
- let render = uglify_es.minify(
- files,
- {
- //compress: {
- //arrows : true,
- //booleans : true,
- //collapse_vars : true,
- //comparisons : true,
- //computed_props: true,
- //conditionals : true,
- //dead_code : true,
- //drop_console : false,
- //drop_debugger : true,
- //ecma : 5,
- //evaluate : true,
- //expression : false,
- //global_defs : {},
- //hoist_funs : false,
- //hoist_props : true,
- //hoist_vars : false,
- //ie8 : false,
- //if_return : true,
- //inline : true,
- //join_vars : true,
- //keep_classnames: false,
- //keep_fargs : true,
- //keep_fnames : false,
- //keep_infinity : false,
- //loops : true,
- //negate_iife : true,
- //passes : 1,
- //properties : true,
- //pure_getters : true && "strict",
- //pure_funcs : null,
- //reduce_funcs : true,
- //reduce_vars : true,
- //sequences : true,
- //side_effects : true,
- //switches : true,
- //top_retain : null,
- //toplevel : !!(options && options["top_retain"]),
- //typeofs : true,
- //unsafe : false,
- //unsafe_arrows : false,
- //unsafe_comps : false,
- //unsafe_Function: false,
- //unsafe_math : false,
- //unsafe_methods: false,
- //unsafe_proto : false,
- //unsafe_regexp : false,
- //unsafe_undefined: false,
- //unused : true,
- //warnings : false,
- //},
- //ecma: undefined,
- //ie8: false,
- //keep_classnames: undefined,
- //keep_fnames: false,
- //mangle: {},
- //nameCache: null,
- //output: {
- //ascii_only : false,
- //beautify : false,
- //bracketize : false,
- //comments : false,
- //ecma : 5,
- //ie8 : false,
- //indent_level : 4,
- //indent_start : 0,
- //inline_script : true,
- //keep_quoted_props: false,
- //max_line_len : false,
- //preamble : null,
- //preserve_line : false,
- //quote_keys : false,
- //quote_style : 0,
- //safari10 : false,
- //semicolons : true,
- //shebang : true,
- //shorthand : undefined,
- //source_map : null,
- //webkit : false,
- //width : 80,
- //wrap_iife : false
- //},
- //parse: {
- //bare_returns : false,
- //ecma : 8,
- //expression : false,
- //filename : null,
- //html5_comments : true,
- //shebang : true,
- //strict : false,
- //toplevel : null
- //},
- //rename: undefined,
- //safari10: false,
- //sourceMap: false,
- //timings: false,
- toplevel: true //false,
- //warnings: false,
- //wrap: false
- }
- )
- if (render.error !== undefined)
- throw render.error
- return /*await*/ fs_writeFile(
- temp_pathname,
- render.code,
- {encoding: 'utf-8'}
- )
- },
- true
- )
- console.log('getting', pathname, 'as js_min')
- assert(render.deps.length === 0)
- result.value = fs_readFile(render.pathname)
- }
- )
-}
-
-SiteRoot.prototype.internal_get_svg_min = function(pathname) {
- return /*await*/ this.build_cache_svg_min.get(
- pathname,
- async result => {
- let render = await disk_build(
- pathname,
- async temp_pathname => {
- let render = await svgo.optimize(
- await fs_readFile(pathname, {encoding: 'utf-8'}),
- {path: pathname}
- )
- return /*await*/ fs_writeFile(
- temp_pathname,
- render.data,
- {encoding: 'utf-8'}
- )
- },
- true
- )
- assert(render.deps.length === 0)
- result.value = await fs_readFile(render.pathname)
- console.log('getting', pathname, 'as svg_min')
- }
- )
-}
-
-SiteRoot.prototype.internal_get_text = function(pathname) {
- return /*await*/ this.build_cache_text.get(
- pathname,
- async result => {
- let text = await fs_readFile(pathname, {encoding: 'utf-8'})
- console.log('getting', pathname, 'as text')
- result.value = text
- }
- )
-}
-
-//SiteRoot.prototype.internal_get_zet = function(pathname) {
-// return /*await*/ this.build_cache_zet.get(
-// pathname,
-// async result => {
-// console.log('getting', pathname, 'as zet')
-// result.deps = [
-// pathname + '.map.0',
-// pathname + '.param.0',
-// pathname + '.v.0',
-// pathname + '.vocab.0'
-// ]
-// result.value = new zettair.Index(pathname)
-// }
-// )
-//}
-
-SiteRoot.prototype.internal_get_zip = function(pathname) {
- return /*await*/ this.build_cache_zip.get(
- pathname,
- async result => {
- let zipfile = await yauzl_open(pathname, {autoClose: false})
- console.log('getting', pathname, 'as zip')
-
- let entries = []
- await new Promise(
- (resolve, reject) => {
- zipfile.
- on('entry', entry => {entries.push(entry)}).
- on('end', () => resolve())
- }
- )
-
- result.value = {}
- for (let i = 0; i < entries.length; ++i) {
- let read_stream = await new Promise(
- (resolve, reject) => {
- zipfile.openReadStream(
- entries[i],
- (err, stream) => {
- if (err)
- reject(err)
- resolve(stream)
- }
- )
- }
- )
-
- let write_stream = new stream_buffers.WritableStreamBuffer()
- let data = new Promise(
- (resolve, reject) => {
- write_stream.
- on('finish', () => {resolve(write_stream.getContents())}).
- on('error', err => {reject(err)})
- }
- )
- read_stream.pipe(write_stream)
- data = await data
-
- let entry_pathname = '/' + entries[i].fileName
- console.log('entry pathname', entry_pathname, 'size', data.length)
- result.value[entry_pathname] = data
- }
- await zipfile.close()
- }
- )
-}
-
SiteRoot.prototype.internal_ensure_dir = async function(pathname) {
try {
await fs_mkdir(pathname)
)
}
-//SiteRoot.prototype.get_email = function(pathname) {
-// return /*await*/ this.internal_get_email(this.root + pathname)
-//}
SiteRoot.prototype.get_json = function(pathname) {
return /*await*/ this.internal_get_json(this.root + pathname)
}
+
SiteRoot.prototype.get_jst = function(pathname) {
- return /*await*/ this.internal_get_jst(this.root + pathname, this.root)
+ return /*await*/ this.jst_cache.get(this.root + pathname)
}
-SiteRoot.prototype.get_css_less = function(pathname) {
- return /*await*/ this.internal_get_css_less(this.root + pathname, this.root)
+
+SiteRoot.prototype.get_less_css = function(pathname) {
+ return /*await*/ this.less_css_cache.get(this.root + pathname)
}
-SiteRoot.prototype.get_css_min = function(pathname) {
- return /*await*/ this.internal_get_css_min(this.root + pathname)
+
+SiteRoot.prototype.get_min_css = function(pathname) {
+ return /*await*/ this.min_css_cache.get(this.root + pathname)
}
-SiteRoot.prototype.get_html_min = function(pathname) {
- return /*await*/ this.internal_get_html_min(this.root + pathname)
+
+SiteRoot.prototype.get_min_html = function(pathname) {
+ return /*await*/ this.min_html_cache.get(this.root + pathname)
}
-SiteRoot.prototype.get_js_min = function(pathname) {
- return /*await*/ this.internal_get_js_min(this.root + pathname)
+
+SiteRoot.prototype.get_min_js = function(pathname) {
+ return /*await*/ this.min_js_cache.get(this.root + pathname)
}
-SiteRoot.prototype.get_svg_min = function(pathname) {
- return /*await*/ this.internal_get_svg_min(this.root + pathname)
+
+SiteRoot.prototype.get_min_svg = function(pathname) {
+ return /*await*/ this.min_svg_cache.get(this.root + pathname)
}
+
SiteRoot.prototype.get_text = function(pathname) {
- return /*await*/ this.internal_get_text(this.root + pathname)
+ return /*await*/ this.text_cache.get(this.root + pathname)
}
-//SiteRoot.prototype.get_zet = function(pathname) {
-// return /*await*/ this.internal_get_zet(this.root + pathname)
-//}
+
SiteRoot.prototype.get_zip = function(pathname) {
- return /*await*/ this.internal_get_zip(this.root + pathname)
+ return /*await*/ this.zip_cache.get(this.root + pathname)
}
SiteRoot.prototype.ensure_dir = async function(pathname) {
default_value
)
}
+
SiteRoot.prototype.write_json = async function(pathname, value, timeout) {
return /*await*/ this.internal_write_json(
this.root + pathname,
timeout
)
}
+
SiteRoot.prototype.modify_json =
async function(pathname, default_value, modify_func, timeout) {
return /*await*/ this.internal_modify_json(
SiteRoot.prototype.serve_jst = async function(env, pathname, ...args) {
let template
try {
- template = await this.internal_get_jst(pathname, this.root)
+ template = await this.jst_cache.get(pathname)
}
catch (err) {
if (!(err instanceof Error) || err.code !== 'ENOENT')
return true
}
-SiteRoot.prototype.serve_css_less = async function(env, pathname) {
+SiteRoot.prototype.serve_less_css = async function(env, pathname) {
if (pathname.slice(-9) !== '.css.less')
return false
let data
try {
- data = await this.internal_get_css_less(pathname, this.root)
+ data = await this.less_css_cache.get(pathname)
}
catch (err) {
if (!(err instanceof Error) || err.code !== 'ENOENT')
throw err
return false
}
- this.serve(env, 200, data, 'css_less')
+ this.serve(env, 200, data, 'less_css')
return true
}
-SiteRoot.prototype.serve_css_min = async function(env, pathname) {
+SiteRoot.prototype.serve_min_css = async function(env, pathname) {
if (pathname.slice(-8) !== '.css.min')
return false
let data
try {
- data = await this.internal_get_css_min(pathname)
+ data = await this.min_css_cache.get(pathname)
}
catch (err) {
if (!(err instanceof Error) || err.code !== 'ENOENT')
throw err
return false
}
- this.serve(env, 200, data, 'css_min')
+ this.serve(env, 200, data, 'min_css')
return true
}
-SiteRoot.prototype.serve_html_min = async function(env, pathname) {
+SiteRoot.prototype.serve_min_html = async function(env, pathname) {
if (pathname.slice(-9) !== '.html.min')
return false
let data
try {
- data = await this.internal_get_html_min(pathname)
+ data = await this.internal_get_min_html(pathname)
}
catch (err) {
if (!(err instanceof Error) || err.code !== 'ENOENT')
throw err
return false
}
- this.serve(env, 200, data, 'html_min')
+ this.serve(env, 200, data, 'min_html')
return true
}
-SiteRoot.prototype.serve_js_min = async function(env, pathname) {
+SiteRoot.prototype.serve_min_js = async function(env, pathname) {
if (pathname.slice(-7) !== '.js.min')
return false
let data
try {
- data = await this.internal_get_js_min(pathname)
+ data = await this.min_js_cache.get(pathname)
}
catch (err) {
if (!(err instanceof Error) || err.code !== 'ENOENT')
throw err
return false
}
- this.serve(env, 200, data, 'js_min')
+ this.serve(env, 200, data, 'min_js')
return true
}
-SiteRoot.prototype.serve_svg_min = async function(env, pathname) {
+SiteRoot.prototype.serve_min_svg = async function(env, pathname) {
if (pathname.slice(-8) !== '.svg.min')
return false
let data
try {
- data = await this.internal_get_svg_min(pathname)
+ data = await this.min_svg_cache.get(pathname)
}
catch (err) {
if (!(err instanceof Error) || err.code !== 'ENOENT')
throw err
return false
}
- this.serve(env, 200, data, 'svg_min')
+ this.serve(env, 200, data, 'min_svg')
return true
}
SiteRoot.prototype.serve_zip = async function(env, zipname, pathname) {
let zip
try {
- zip = await this.internal_get_zip(zipname)
+ zip = await this.zip_cache.get(zipname)
}
catch (err) {
if (!(err instanceof Error) || err.code !== 'ENOENT')
//console.log(`serve_file ${pathname}`)
if (await this.serve_jst(env, pathname + '.jst'))
return
- if (await this.serve_css_less(env, pathname + '.less'))
+ if (await this.serve_less_css(env, pathname + '.less'))
return
- if (await this.serve_css_min(env, pathname + '.min'))
+ if (await this.serve_min_css(env, pathname + '.min'))
return
- if (await this.serve_html_min(env, pathname + '.min'))
+ if (await this.serve_min_html(env, pathname + '.min'))
return
- if (await this.serve_js_min(env, pathname + '.min'))
+ if (await this.serve_min_js(env, pathname + '.min'))
return
- if (await this.serve_svg_min(env, pathname + '.min'))
+ if (await this.serve_min_svg(env, pathname + '.min'))
return
if (await this.serve_fs(env, pathname))
return