From cb26c1b2f775c740fb65154a88dd3cb6394b5275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Sun, 27 Sep 2020 19:32:13 +0800 Subject: [PATCH] 2.1.0 --- .gitignore | 3 ++- index.js | 62 +++++++++++++++++++++++++++++++++------------------- index.mjs | 48 +++++++++++++++++++++++++++------------- lib/tool.js | 39 ++++++++++++++++----------------- lib/tool.mjs | 39 ++++++++++++++++----------------- package.json | 15 ++++--------- 6 files changed, 116 insertions(+), 90 deletions(-) diff --git a/.gitignore b/.gitignore index 351c0c3..e30f8f1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ .vscode -node_modules/ \ No newline at end of file +node_modules/ +cache/ \ No newline at end of file diff --git a/index.js b/index.js index 447a47d..6c747d1 100644 --- a/index.js +++ b/index.js @@ -14,17 +14,26 @@ function hash(str) { return Buffer.from(str).toString('hex') } +var cacheDir = path.resolve(__dirname, './cache/') + class Smarty { constructor(opt) { - this.opt = { cache: true, ext: '.htm' } + this.opt = { ext: '.htm' } if (opt) { Object.assign(this.opt, opt) } this.__REG__ = new RegExp(this.opt.ext + '$') this.tool = new Tool(this.opt) - this.__DATA__ = Object.create(null) // 预定义的变量储存 + + this.reset() this.__CACHE__ = Object.create(null) // 渲染缓存 + // 消除缓存目录 + fs.rm(cacheDir) + } + + reset() { + this.__DATA__ = Object.create(null) // 预定义的变量储存 } config(key, val) { @@ -53,46 +62,53 @@ class Smarty { /** * [render 模板渲染] - * @param {String} tpl 模板路径 + * @param {String} filePath 模板路径 * @param {Boolean} noParse 不解析直接读取 * @return {Promise} 返回一个Promise对象 */ - render(tpl = '', noParse = false) { - var key = null - var cache + render(filePath = '', noParse = false) { + var key, ckey, cache, needWrite + if (!this.opt.path) { throw new Error('Smarty engine must define path option') } - if (!tpl) { - return Promise.reject('argument[tpl] can not be empty') + if (!filePath) { + return Promise.reject('argument[filePath] can not be empty') } - if (!this.__REG__.test(tpl)) { - tpl += this.opt.ext + if (!this.__REG__.test(filePath)) { + filePath += this.opt.ext } - tpl = path.resolve(this.opt.path, tpl) + filePath = path.resolve(this.opt.path, filePath) - key = hash(tpl) + key = hash(filePath) + ckey = path.join(cacheDir, key) if (this.__CACHE__[key]) { - return Promise.resolve(fs.cat(path.resolve('./cache/', key))) + cache = fs.cat(ckey) + } else { + cache = fs.cat(filePath) + this.__CACHE__[key] = true + needWrite = true } - cache = this.tool.__readFile__(tpl, noParse) - + // 无需解析时, 直接输出 if (noParse) { - this.__CACHE__[key] = true - fs.echo(cache, path.resolve('./cache/', key)) + if (needWrite) { + fs.echo(cache, ckey) + } return Promise.resolve(cache) + } else { + if (needWrite) { + cache = this.tool.parse(cache) + fs.echo(cache, ckey) + } } try { - cache = this.tool.parse(cache, this.__DATA__) - if (this.opt.cache) { - this.__CACHE__[key] = true - fs.echo(cache, path.resolve('./cache/', key)) - } - return Promise.resolve(cache) + var body = this.tool.exec(cache, this.__DATA__) + this.reset() + return Promise.resolve(body) } catch (err) { return Promise.reject(err) } diff --git a/index.mjs b/index.mjs index 74fa0d3..14e08a4 100644 --- a/index.mjs +++ b/index.mjs @@ -7,6 +7,7 @@ import 'es.shim' import path from 'path' import fs from 'iofs' +import { fileURLToPath } from 'url' import Tool from './lib/tool.mjs' @@ -14,17 +15,27 @@ function hash(str) { return Buffer.from(str).toString('hex') } +var __dirname = path.dirname(fileURLToPath(import.meta.url)) +var cacheDir = path.resolve(__dirname, './cache/') + export default class Smarty { constructor(opt) { - this.opt = { cache: true, ext: '.htm' } + this.opt = { ext: '.htm' } if (opt) { Object.assign(this.opt, opt) } this.__REG__ = new RegExp(this.opt.ext + '$') this.tool = new Tool(this.opt) - this.__DATA__ = Object.create(null) // 预定义的变量储存 + + this.reset() this.__CACHE__ = Object.create(null) // 渲染缓存 + // 消除缓存目录 + fs.rm(cacheDir) + } + + reset() { + this.__DATA__ = Object.create(null) // 预定义的变量储存 } config(key, val) { @@ -58,8 +69,8 @@ export default class Smarty { * @return {Promise} 返回一个Promise对象 */ render(filePath = '', noParse = false) { - var key = null - var cache + var key, ckey, cache, needWrite + if (!this.opt.path) { throw new Error('Smarty engine must define path option') } @@ -73,26 +84,33 @@ export default class Smarty { filePath = path.resolve(this.opt.path, filePath) key = hash(filePath) + ckey = path.join(cacheDir, key) if (this.__CACHE__[key]) { - return Promise.resolve(fs.cat(path.resolve('./cache/', key))) + cache = fs.cat(ckey) + } else { + cache = fs.cat(filePath) + this.__CACHE__[key] = true + needWrite = true } - cache = this.tool.__readFile__(filePath, noParse) - + // 无需解析时, 直接输出 if (noParse) { - this.__CACHE__[key] = true - fs.echo(cache, path.resolve('./cache/', key)) + if (needWrite) { + fs.echo(cache, ckey) + } return Promise.resolve(cache) + } else { + if (needWrite) { + cache = this.tool.parse(cache) + fs.echo(cache, ckey) + } } try { - cache = this.tool.parse(cache, this.__DATA__) - if (this.opt.cache) { - this.__CACHE__[key] = true - fs.echo(cache, path.resolve('./cache/', key)) - } - return Promise.resolve(cache) + var body = this.tool.exec(cache, this.__DATA__) + this.reset() + return Promise.resolve(body) } catch (err) { return Promise.reject(err) } diff --git a/lib/tool.js b/lib/tool.js index bfd8012..05218c7 100644 --- a/lib/tool.js +++ b/lib/tool.js @@ -69,21 +69,20 @@ class Tool { } } - __readFile__(file, noParse) { - var buf = null + __readFile__(file) { if (!fs.exists(file)) { throw new Error(`Can not find template "${file}"`) } - buf = fs.cat(file).toString() + return this.__fixed__(fs.cat(file).toString()) + } - if (noParse) { - return buf - } - - return buf - .replace(/[\r\n\t]+/g, ' ') //去掉所有的换行/制表 + __fixed__(str) { + return str + .trim() + .replace(/[\r\n\t]+/g, ' ') // 去掉所有的换行/制表 .replace(/\\/g, '\\\\') + .replace(/`/g, '\\`') } //生成正则 @@ -312,24 +311,24 @@ class Tool { } //解析模板 - parse(str, data) { + parse(buf) { + var str = this.__fixed__(buf.toString()) + str = this.parseExtends(str) + str = this.parseNormal(str) + return str + } + + exec(str, data) { var vars = `"use strict"; let __filters__ = f; ` + var body = '' for (let i in data) { let tmp = JSON.stringify(data[i]) || '' vars += `let ${i} = ${tmp}; ` } - str = str - .trim() - .replace(/[\r\n\t]+/g, ' ') // 去掉所有的换行/制表 - .replace(/\\/g, '\\\\') - .replace(/`/g, '\\`') - str = this.parseExtends(str) - str = this.parseNormal(str) + body = `${vars} let tpl=\`${str}\`; return tpl;` - str = `${vars} let tpl=\`${str}\`; return tpl;` - - return new Function('f', str)(this.filters) + return new Function('f', body)(this.filters) } } diff --git a/lib/tool.mjs b/lib/tool.mjs index 18ff568..af20750 100644 --- a/lib/tool.mjs +++ b/lib/tool.mjs @@ -69,21 +69,20 @@ export default class Tool { } } - __readFile__(file, noParse) { - var buf = null + __readFile__(file) { if (!fs.exists(file)) { throw new Error(`Can not find template "${file}"`) } - buf = fs.cat(file).toString() + return this.__fixed__(fs.cat(file).toString()) + } - if (noParse) { - return buf - } - - return buf - .replace(/[\r\n\t]+/g, ' ') //去掉所有的换行/制表 + __fixed__(str) { + return str + .trim() + .replace(/[\r\n\t]+/g, ' ') // 去掉所有的换行/制表 .replace(/\\/g, '\\\\') + .replace(/`/g, '\\`') } //生成正则 @@ -312,23 +311,23 @@ export default class Tool { } //解析模板 - parse(str, data) { + parse(buf) { + var str = this.__fixed__(buf.toString()) + str = this.parseExtends(str) + str = this.parseNormal(str) + return str + } + + exec(str, data) { var vars = `"use strict"; let __filters__ = f; ` + var body = '' for (let i in data) { let tmp = JSON.stringify(data[i]) || '' vars += `let ${i} = ${tmp}; ` } - str = str - .trim() - .replace(/[\r\n\t]+/g, ' ') // 去掉所有的换行/制表 - .replace(/\\/g, '\\\\') - .replace(/`/g, '\\`') - str = this.parseExtends(str) - str = this.parseNormal(str) + body = `${vars} let tpl=\`${str}\`; return tpl;` - str = `${vars} let tpl=\`${str}\`; return tpl;` - - return new Function('f', str)(this.filters) + return new Function('f', body)(this.filters) } } diff --git a/package.json b/package.json index ea31a70..42250fb 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,16 @@ { "name": "smartyx", - "version": "2.0.1", + "version": "2.1.0", "description": "nodeJS模板引擎,理念源自于PHP的smarty模板引擎", - "keywords": [ - "fivejs", - "php", - "smarty", - "template", - "ejs", - "jade" - ], + "keywords": ["fivejs", "php", "smarty", "template", "ejs", "jade"], "author": "yutent ", "repository": { "type": "git", "url": "https://github.com/bytedo/smarty.git" }, "dependencies": { - "es.shim": "^2.0.0", - "iofs": "^1.4.0" + "es.shim": "^2.0.1", + "iofs": "^1.5.0" }, "main": "index.js", "exports": {