Skip to content

Commit

Permalink
template precompilation optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Sep 7, 2016
1 parent d1592a5 commit 4118473
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/cli/controllers/Save.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 22 additions & 2 deletions src/cli/models/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
})
}
Expand Down

0 comments on commit 4118473

Please sign in to comment.