From 4118473df8179fa9cef44506f9588c91ea27eae7 Mon Sep 17 00:00:00 2001 From: gregorybesson Date: Wed, 7 Sep 2016 10:58:35 +0200 Subject: [PATCH] template precompilation optimization --- src/cli/controllers/Save.js | 8 +++----- src/cli/models/Manager.js | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/cli/controllers/Save.js b/src/cli/controllers/Save.js index 46e540f..d9d67ce 100755 --- a/src/cli/controllers/Save.js +++ b/src/cli/controllers/Save.js @@ -151,7 +151,7 @@ export function save(url, tplPath, json = null, text = '', type = '', previousSa text = Util.removeDataList(text) - var res = saveJsonAndHtml(tpl, obj, text, type) + var res = saveJsonAndHtml(tpl.replace(/^\/+/, ''), obj, text, type) obj = Hooks.instance.trigger('afterSave', obj) @@ -181,10 +181,8 @@ function splitArray(ar, chunkSize) { ) } -export function saveJsonAndHtml(tplPath, obj, html, type) { - - console.log(tplPath) - var page = new Page(tplPath, html, obj.json.content, true) +export function saveJsonAndHtml(templateId, obj, html, type) { + var page = new Page(templateId, html, obj.json.content, true) saveHtml(obj.html.path, page.html) saveJson(obj.json.path, obj.json.content) diff --git a/src/cli/models/Manager.js b/src/cli/models/Manager.js index 3b5c19e..8be1c28 100644 --- a/src/cli/models/Manager.js +++ b/src/cli/models/Manager.js @@ -58,8 +58,28 @@ class Manager { fse.readdirSync(path).forEach(function (file) { if (file.indexOf(".hbs") > -1) { - var tmpl = eval("(function(){return " + fse.readFileSync(fileUtils.concatPath(path, file)) + "}());"); - Handlebars.templates[file.replace('.hbs', '')] = Handlebars.template(tmpl); + let originalTemplatePath = fileUtils.concatPath(config.root, config.templates.url) + '/' + file.replace('.hbs', '.' + config.files.templates.extension) + + try{ + let originalTemplateStat = fse.statSync(originalTemplatePath); + let originalTemplateMdate = originalTemplateStat.mtime; + let stat = fse.statSync(fileUtils.concatPath(path, file)); + let mdate = stat.mtime; + + // if the original template has been updated after precompilation, I delete the precompiled file + // else I add it to the hbs template array + if(originalTemplateMdate>mdate){ + fse.unlinkSync(fileUtils.concatPath(path, file)); + } else { + var tmpl = eval("(function(){return " + fse.readFileSync(fileUtils.concatPath(path, file)) + "}());"); + Handlebars.templates[file.replace('.hbs', '')] = Handlebars.template(tmpl); + } + } + catch(err) { + console.log('The original template has not been found or the hbs template is corrupted'); + console.log(originalTemplatePath) + console.log(err) + } } }) }