From b692c7f7a083d93c578790142767997ab7952505 Mon Sep 17 00:00:00 2001 From: Nick Downing Date: Sun, 18 Nov 2018 10:27:42 +1100 Subject: [PATCH] Split out disk date checking stuff into a helper module, @ndcode/disk_build --- jst.js | 120 ++++++++++++++++++++++----------------------------- package.json | 3 +- 2 files changed, 54 insertions(+), 69 deletions(-) diff --git a/jst.js b/jst.js index ef28e44..2669727 100644 --- a/jst.js +++ b/jst.js @@ -24,6 +24,7 @@ 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') @@ -32,78 +33,61 @@ let visitors = require('./visitors') 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 diff --git a/package.json b/package.json index 1681148..f6d927e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ndcode/jst", - "version": "0.1.2", + "version": "0.1.3", "description": "JavaScript Templates for HTML.", "keywords": [ "template", @@ -26,6 +26,7 @@ "dependencies": { "@ndcode/build_cache": "^0.1.0", "@ndcode/clean-css": "^0.1.0", + "@ndcode/disk_build": "^0.1.0", "astring": "^1.3.1", "html-escape": "^2.0.0", "rollup": "^0.45.0", -- 2.34.1