Comparing version 0.0.34 to 0.0.35
@@ -76,3 +76,3 @@ 'use strict'; | ||
this.__val = function (i) { | ||
return args.valuesQueue.getAt(i); | ||
return args.jsValues.getAt(i); | ||
}; | ||
@@ -174,23 +174,17 @@ | ||
let cacheId = viewName + "#" + args.filePath; | ||
let dataCache = args.partialsCache[cacheId]; | ||
// Read file and complie to JS. | ||
let partial = args.findPartialSync(viewName, args.filePath, args.er, args.partialsCache); | ||
compileOpt.template = partial.data; | ||
compileOpt.filePath = partial.filePath; | ||
if (dataCache) { | ||
// Use cached JS. | ||
compileOpt.js = dataCache.js; | ||
compileOpt.valuesQueue = dataCache.values; | ||
if (partial.js) { // if it's taken from cache | ||
compileOpt.js = partial.js; | ||
compileOpt.jsValues = partial.jsValues; | ||
} | ||
else { | ||
// Read file and complie to JS. | ||
let { data, filePath } = args.findPartialSync(viewName, args.filePath, args.er); | ||
compileOpt.template = data; | ||
compileOpt.filePath = filePath; | ||
} | ||
let { html, precompiled } = compileSync(compileOpt); | ||
partial.js = precompiled.js; // put to cache | ||
partial.jsValues = precompiled.jsValues; // put to cache | ||
args.html += html; | ||
if (!dataCache) | ||
args.partialsCache[cacheId] = precompiled; | ||
return ''; // for the case of call as expression <div>@Html.partial()</div> | ||
@@ -213,8 +207,8 @@ }; | ||
toScript(valuesQueue) { | ||
return toScript(this, valuesQueue); | ||
toScript(jsValues) { | ||
return toScript(this, jsValues); | ||
} | ||
} | ||
function toScript(block, valuesQueue) { | ||
function toScript(block, jsValues) { | ||
if (block.type === type.section) { | ||
@@ -226,3 +220,3 @@ let secMarker = `\r\nHtml.__sec("${block.name}");`; | ||
let sectionBlock = block.blocks[n]; | ||
script += toScript(sectionBlock, valuesQueue); | ||
script += toScript(sectionBlock, jsValues); | ||
} | ||
@@ -238,6 +232,6 @@ | ||
case type.html: | ||
i = valuesQueue.enq(block.text); | ||
i = jsValues.enq(block.text); | ||
return "\r\nHtml.raw(Html.__val(" + i + "));"; | ||
case type.expr: | ||
i = valuesQueue.enq(block.text); | ||
i = jsValues.enq(block.text); | ||
return "\r\nHtml.encode(eval(Html.__val(" + i + ")));"; | ||
@@ -329,3 +323,3 @@ case type.code: | ||
return { html: htmlArgs.html, precompiled: { js: htmlArgs.js, values: htmlArgs.valuesQueue } }; | ||
return { html: htmlArgs.html, precompiled: { js: htmlArgs.js, jsValues: htmlArgs.jsValues } }; | ||
} | ||
@@ -338,3 +332,3 @@ | ||
let js = this.args.js; | ||
let valuesQueue = this.args.valuesQueue; | ||
let jsValues = this.args.jsValues; | ||
let template = this.args.template; | ||
@@ -354,4 +348,4 @@ | ||
this.parseHtml(this.blocks); | ||
valuesQueue = new Queue(); | ||
var scripts = this.blocks.map(b => b.toScript(valuesQueue)); | ||
jsValues = new Queue(); | ||
var scripts = this.blocks.map(b => b.toScript(jsValues)); | ||
js = scripts.join(""); | ||
@@ -362,3 +356,3 @@ } | ||
html: '', | ||
valuesQueue, | ||
jsValues, | ||
js, | ||
@@ -365,0 +359,0 @@ template, |
@@ -50,5 +50,5 @@ require('./utils'); | ||
}, | ||
findPartialSync: (layoutName, filePath, errorsFactory) => { | ||
findPartialSync: (layoutName, filePath, errorsFactory, cache) => { | ||
var startDir = path.dirname(filePath); | ||
return this.findPartialSync(startDir, layoutName, [], errorsFactory); | ||
return this.findPartialSync(startDir, layoutName, [], errorsFactory, cache); | ||
} | ||
@@ -87,3 +87,3 @@ }; | ||
findPartialSync(startDir, partialViewName, searchedLocations, errorsFactory) { | ||
findPartialSync(startDir, partialViewName, searchedLocations, errorsFactory, cache) { | ||
searchedLocations = searchedLocations || []; | ||
@@ -103,3 +103,3 @@ | ||
if (partialViewName[0] === '/') { // it's relative to the `views` root folder | ||
if (!viewPath.startsWithIC(this.viewsDir)) // for linux onlry (in Windows an absolute path cannot start with '/') | ||
if (!viewPath.startsWithIC(this.viewsDir)) // for linux only (in Windows an absolute path cannot start with '/') | ||
viewPath = path.join(this.viewsDir, viewPath); | ||
@@ -114,2 +114,7 @@ // [#2.4.1], [#2.4.2], [#2.4.3] | ||
searchedLocations.push(viewPath); | ||
let cachedData = cache && cache[viewPath]; | ||
if (cachedData) | ||
return cachedData; | ||
let data = fs.readFileSync(viewPath); | ||
@@ -133,2 +138,7 @@ let dataStr = this.addFileNameIfDev(data, viewPath); | ||
searchedLocations.push(filePath); | ||
let cachedData = cache && cache[filePath]; | ||
if (cachedData) | ||
return cachedData; | ||
let data = fs.readFileSync(filePath); | ||
@@ -153,3 +163,8 @@ let dataStr = this.addFileNameIfDev(data, filePath); | ||
function successResult(data, filePath) { | ||
return { data, filePath }; | ||
var result = { data, filePath }; | ||
if (cache) | ||
cache[filePath] = result; | ||
return result; | ||
} | ||
@@ -239,2 +254,12 @@ } | ||
} | ||
// static checkFileReadAccessSync(path){ | ||
// try{ | ||
// fs.accessSync(path, fs.constants.R_OK); | ||
// return true; | ||
// } | ||
// catch(exc){ | ||
// return false; | ||
// } | ||
// } | ||
} |
{ | ||
"name": "raz", | ||
"description": "Razor-like syntax for templating views in Express framework by mixing HTML with JavaScript.", | ||
"version": "0.0.34", | ||
"version": "0.0.35", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Sergey", |
164417
3730