let BuildCache = require('@ndcode/build_cache')
let acorn = require('./dist/acorn')
let astring = require('astring')
+let disk_build = require('@ndcode/disk_build')
let fs = require('fs')
let html_escape = require('html-escape')
let path = require('path')
let util = require('util')
let fs_readFile = util.promisify(fs.readFile)
-let fs_stat = util.promisify(fs.stat)
let fs_writeFile = util.promisify(fs.writeFile)
let build_cache = new BuildCache()
-let jst = async (pathname, root, args) => {
- let dirname = path.posix.dirname(pathname)
- return build_cache.get(
- pathname,
- async result => {
- let arg_names = ['_require', '_html_escape', '_pathname', '_root']
- let arg_values = [
- async pathname => {
- let temp = dirname
- while (pathname.charAt(0) === '/') {
- temp = root
- pathname = pathname.slice(1)
- }
- pathname = path.posix.resolve(temp, pathname)
- return jst(pathname, root, args)
- },
- html_escape,
- pathname,
- root
- ]
- if (args !== undefined)
- for (let i in args)
- if (Object.prototype.hasOwnProperty.call(args, i)) {
- arg_names.push(i)
- arg_values.push(args[i])
- }
-
- let stats = await fs_stat(pathname)
-
- let js_pathname = path.posix.resolve(
- dirname,
- `.${path.posix.basename(pathname)}.js`
- )
+let jst = (pathname, root, args) => /*await*/ build_cache.get(
+ pathname,
+ async result => {
+ let arg_names = ['_require', '_html_escape', '_pathname', '_root']
+ let arg_values = [
+ async require_pathname => {
+ let temp = path.posix.dirname(pathname)
+ while (require_pathname.charAt(0) === '/') {
+ temp = root
+ require_pathname = require_pathname.slice(1)
+ }
+ require_pathname = path.posix.resolve(temp, require_pathname)
+ return jst(require_pathname, root, args)
+ },
+ html_escape,
+ pathname,
+ root
+ ]
+ if (args !== undefined)
+ for (let i in args)
+ if (Object.prototype.hasOwnProperty.call(args, i)) {
+ arg_names.push(i)
+ arg_values.push(args[i])
+ }
- let js_stats
- try {
- js_stats = await fs_stat(js_pathname)
- }
- catch (err) {
- if (err.code !== 'ENOENT') // err type???
- throw err
- //js_stats = undefined
- }
- if (js_stats === undefined || stats.mtimeMs > js_stats.mtimeMs) {
- text = await fs_readFile(pathname, {encoding: 'utf-8'})
- console.log('compiling', pathname)
- await fs_writeFile(
- js_pathname,
- astring.generate(
- transform.transform(
- visitors,
- acorn.parse(
- `module.exports = async (${arg_names.join(', ')}) => {${text}}`
- )
+ let full_built_pathname = require.resolve(
+ await disk_build(
+ pathname,
+ async built_pathname => {
+ let text = await fs_readFile(pathname, {encoding: 'utf-8'})
+ console.log('building', pathname)
+ return /*await*/ fs_writeFile(
+ built_pathname,
+ astring.generate(
+ transform.transform(
+ visitors,
+ acorn.parse(
+ `module.exports = async (${arg_names.join(', ')}) => {${text}}`
+ )
+ ),
+ {} //indent: ''}
),
- {} //indent: ''}
- ),
- {encoding: 'utf-8'}
- )
- }
- else
- console.log('reloading', js_pathname)
-
- let full_js_pathname = require.resolve(js_pathname)
- delete require.cache[full_js_pathname]
- result.value = await (require(full_js_pathname)).apply(null, arg_values)
- }
- )
-}
+ {encoding: 'utf-8'}
+ )
+ }
+ )
+ )
+ delete require.cache[full_built_pathname]
+ result.value = await (
+ require(full_built_pathname)
+ ).apply(null, arg_values)
+ }
+)
module.exports = jst