Skip to content

Commit 09d561a

Browse files
committed
improve precompile caching + bugs
1 parent b2bcf6e commit 09d561a

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed

docs/abe-config.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"files": {
4949
"templates": {
5050
"extension": "html",
51-
"assets": "_files"
51+
"assets": "_files",
52+
"precompile": true
5253
}
5354
},
5455
"cookie": {
@@ -79,6 +80,8 @@ Wysiwyg configuration
7980
8081
file extension + [ folder ]_files assets
8182

83+
__precompile__ (false by default): If set to true, the templates are precompiled by Handlebars. Big performance improvement. This precompilation occurs when a page is displayed in Abe. While you design your templates, set this option to false and clear the hbs directory (in your templates directory) for reinitializing the cache.
84+
8285
> cookie
8386
8487
Enable/disable cookie secure

docs/abe-hooks.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ beforeGetJson | (path, abe) | change json path before it load
4242
afterGetJson | (json, abe) | change json data after load
4343
beforeGetTemplate | (file, abe) | change html template path
4444
afterGetTemplate | (text, abe) | change text of template
45-
beforePageText | (text, json, Handlebars, abe) | change text before page render
46-
afterPageText | (text, json, Handlebars, abe) | change after page rendered
4745
beforePageJson | (json, abe) | change json before page render
48-
afterPageJson | (json, abe) | change json after page render
4946
beforeAddWorkflow | (flows, userFlow, currentFlow, text, abe) | Allow to add stuff **before** to the toolbar
5047
afterAddWorkflow | (flows, userFlow, currentFlow, text, abe) | Allow to add stuff **after** to the toolbar
5148
beforeListPage | (file, workflow, index, action, text, abe) | Call **before** each row is created on table list

src/cli/models/Manager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class Manager {
4343
return this
4444
}
4545

46+
addHbsTemplate(templateId) {
47+
const path = fileUtils.concatPath(config.root, config.templates.url, 'hbs', templateId) + '.hbs';
48+
var tmpl = eval("(function(){return " + fse.readFileSync(path) + "}());");
49+
Handlebars.templates[templateId] = Handlebars.template(tmpl);
50+
}
51+
4652
loadHbsTemplates() {
4753
const path = fileUtils.concatPath(config.root, config.templates.url, 'hbs');
4854

src/cli/models/Page.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,46 @@ export default class Page {
8787

8888
// je rajoute les attributs pour les tags Abe (qui ne sont pas dans un attribut HTML)
8989
this._updateAbeAsTag()
90+
91+
// Don't know what it does...
92+
var source = config.source.name
93+
if(typeof json[source] !== 'undefined' && json[source] !== null) {
94+
var keys = Object.keys(json[source])
95+
96+
for(var i in keys) {
97+
var replaceEach = new RegExp(`<!-- \\[\\[${keys[i]}\\]\\][\\s\\S]*?-->`, 'g')
98+
this.template = this.template.replace(replaceEach, '')
99+
100+
var patAttrSource = new RegExp(' ([A-Za-z0-9\-\_]+)=["|\'].*?({{' + keys[i] + '}}).*?["|\']', 'g')
101+
var patAttrSourceMatch = this.template.match(patAttrSource)
102+
103+
if(typeof patAttrSourceMatch !== 'undefined' && patAttrSourceMatch !== null) {
104+
var patAttrSourceInside = new RegExp('(\\S+)=["\']?((?:.(?!["\']?\\s+(?:\\S+)=|[>"\']))+.)["\']?({{' + keys[i] + '}}).*?["|\']', 'g')
105+
Array.prototype.forEach.call(patAttrSourceMatch, (pat) => {
106+
var patAttrSourceCheck = patAttrSourceInside.exec(pat)
107+
if(typeof patAttrSourceCheck !== 'undefined' && patAttrSourceCheck !== null) {
108+
var checkEscaped = /["|'](.*?)["|']/
109+
checkEscaped = checkEscaped.exec(patAttrSourceCheck[0])
110+
if(typeof checkEscaped !== 'undefined' && checkEscaped !== null && checkEscaped.length > 0) {
111+
checkEscaped = escape(checkEscaped[1])
112+
this.template = this.template.replace(
113+
patAttrSourceCheck[0],
114+
` data-abe-attr="${patAttrSourceCheck[1]}" data-abe-attr-escaped="${checkEscaped}" data-abe="${keys[i]}" ${patAttrSourceCheck[0]}`
115+
)
116+
}
117+
}
118+
})
119+
}
120+
121+
var eachSource = new RegExp(`({{#each ${keys[i]}}[\\s\\S a-z]*?{{\/each}})`, 'g')
122+
var matches = this.template.match(eachSource)
123+
if(typeof matches !== 'undefined' && matches !== null) {
124+
Array.prototype.forEach.call(matches, (match) => {
125+
this.template = this.template.replace(match, `${match}<!-- [[${keys[i]}]] ${util.encodeAbe(match)} -->`)
126+
})
127+
}
128+
}
129+
}
90130
}
91131

92132
this._addSource(json)
@@ -97,9 +137,11 @@ export default class Page {
97137
// It's time to replace the [index] by {{@index}} (concerning each blocks)
98138
this.template = this.template.replace(/\[index\]\./g, '{{@index}}-')
99139

100-
// Let's persist the precompiled template for future use (kind of cache)
101-
fse.writeFileSync(this.HbsTemplatePath, Handlebars.precompile(this.template), 'utf8')
102-
Manager.instance.loadHbsTemplates()
140+
if(config.files.templates.precompile){
141+
// Let's persist the precompiled template for future use (kind of cache)
142+
fse.writeFileSync(this.HbsTemplatePath, Handlebars.precompile(this.template), 'utf8')
143+
Manager.instance.addHbsTemplate(templateId)
144+
}
103145

104146
// I compile the text
105147
var compiledTemplate = Handlebars.compile((!this._onlyHTML) ? util.insertDebugtoolUtilities(this.template) : this.template)
@@ -170,11 +212,16 @@ export default class Page {
170212

171213
if(!this._onlyHTML) {
172214

173-
// je rajoute un data-able-block avec index sur tous les tags html du bloc each
215+
var voidData = {}
216+
voidData[key] = [{}]
217+
var blockCompiled = Handlebars.compile(block.replace(/{{abe (.*?)}}/g, '[[abe $1]]').replace(new RegExp(`\\.\\.\/${config.meta.name}`, 'g'), config.meta.name))
218+
var blockHtml = blockCompiled(voidData, {data: {intl: config.intlData}}).replace(/\[\[abe (.*?)\]\]/g, '{{abe $1}}')
219+
220+
// je rajoute un data-abe-block avec index sur tous les tags html du bloc each
174221
var textEachWithIndex = block.replace(/(<(?![\/])[A-Za-z0-9!-]*)/g, '$1 data-abe-block="' + key + '{{@index}}"')
175222

176223
// je remplace le block dans le texte par ça
177-
this.template = this.template.replace(block, textEachWithIndex)
224+
this.template = this.template.replace(block, textEachWithIndex + `<!-- [[${key}]] ${util.encodeAbe(blockHtml)} -->`)
178225
}
179226

180227
// Pour chaque tag Abe, je mets en forme ce tag avec des data- supplémentaires

src/server/helpers/page.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import mkdirp from 'mkdirp'
12
import {
23
Util,
34
fileAttr,

0 commit comments

Comments
 (0)