scrat-swig
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -17,3 +17,8 @@ var swig = require('swig'); | ||
} | ||
if(options.logger){ | ||
Resource.setLogger(options.logger); | ||
} | ||
return function(req, res, next){ | ||
res.locals._query = req._query; | ||
res.locals._body = req._body; | ||
var pagelets = req.get('X-Pagelets'); | ||
@@ -20,0 +25,0 @@ if(pagelets){ |
@@ -8,2 +8,3 @@ 'use strict' | ||
var options = null; | ||
var logger = console; | ||
@@ -46,2 +47,12 @@ /** | ||
Resource.prototype.destroy = function(){ | ||
this._collect = null; | ||
this._script = null; | ||
this._loaded = null; | ||
this._pageletsStack = null; | ||
this._pagelets = null; | ||
this._datalets = null; | ||
this._title = ''; | ||
}; | ||
/** | ||
@@ -55,2 +66,6 @@ * | ||
Resource.setLogger = function(l){ | ||
logger = l; | ||
}; | ||
/** | ||
@@ -96,9 +111,9 @@ * script标签占位 | ||
} else { | ||
// TODO warning | ||
//TODO | ||
} | ||
} else { | ||
// TODO error | ||
logger.error('missing resource map'); | ||
} | ||
} else { | ||
// TODO error | ||
logger.error('missing resource map'); | ||
} | ||
@@ -108,7 +123,7 @@ } | ||
Resource.prototype.normalize = function (id, ext) { | ||
ext = ext || '' | ||
if (id.indexOf('.') === -1) { | ||
id += '/' + id.split('/').pop() + ext | ||
ext = ext || ''; | ||
if(id.indexOf('.') === -1){ | ||
id += '/' + id.substring(id.lastIndexOf('/') + 1) + ext; | ||
} | ||
return id.replace(/^(?!(views|components)\/)/, PREFIX + '/') | ||
return id.replace(/^(?!(views|components)\/)/, PREFIX + '/'); | ||
} | ||
@@ -122,8 +137,13 @@ | ||
Resource.prototype.require = function (id) { | ||
if (this._usePagelet && !this._isPageletOpen) return | ||
id = this.normalize(id, '.js') | ||
if (this._loaded.hasOwnProperty(id)) return | ||
var res = this.getResById(id) | ||
if (this._usePagelet && !this._isPageletOpen) return true; | ||
var rId = this.normalize(id, '.js'); | ||
if (this._loaded.hasOwnProperty(rId)) return true; | ||
var res = this.getResById(rId); | ||
if(!res){ | ||
rId = this.normalize(id, '.css'); | ||
if (this._loaded.hasOwnProperty(rId)) return true; | ||
res = this.getResById(rId); | ||
} | ||
if (res) { | ||
this._loaded[id] = true | ||
this._loaded[rId] = true | ||
if (res.deps && res.deps.length) { | ||
@@ -136,3 +156,5 @@ res.deps.forEach(this.require.bind(this)) | ||
this._collect[res.type].push(res.uri) | ||
return res | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
@@ -148,12 +170,16 @@ } | ||
Resource.prototype.include = function (file, options, ctx) { | ||
var id = file, html = '' | ||
var id = file, html = '', got = false; | ||
if (file.indexOf('.') === -1) { | ||
file = this.normalize(file, '.tpl') | ||
var res = this.getResById(file) | ||
file = this.normalize(file, '.tpl'); | ||
var res = this.getResById(file); | ||
if (res) { | ||
got = true; | ||
id = file; | ||
html = swig.compileFile(res.uri, options)(ctx) | ||
html = swig.compileFile(res.uri, options)(ctx); | ||
} | ||
} | ||
this.require(id) | ||
got = this.require(id) || got; | ||
if(!got){ | ||
logger.error('unable to load resource [' + id + ']'); | ||
} | ||
return html | ||
@@ -274,12 +300,13 @@ } | ||
} else { | ||
var p = out.indexOf(this.CSS_HOOK) | ||
var p = out.indexOf(this.CSS_HOOK); | ||
if (p > -1) { | ||
out = out.substring(0, p) + this.renderCss() + out.substring(p + this.CSS_HOOK.length) | ||
out = out.substring(0, p) + this.renderCss() + out.substring(p + this.CSS_HOOK.length); | ||
} | ||
p = out.lastIndexOf(this.JS_HOOK) | ||
p = out.lastIndexOf(this.JS_HOOK); | ||
if (p > -1) { | ||
out = out.substring(0, p) + this.renderJs() + out.substring(p + this.JS_HOOK.length) | ||
out = out.substring(0, p) + this.renderJs() + out.substring(p + this.JS_HOOK.length); | ||
} | ||
} | ||
return out | ||
this.destroy(); | ||
return out; | ||
} | ||
@@ -293,4 +320,4 @@ | ||
if (ids) { | ||
this._usePagelet = true | ||
var self = this | ||
this._usePagelet = true; | ||
var self = this; | ||
ids.split(/\s*,\s*/).forEach(function (id) { | ||
@@ -376,3 +403,3 @@ self._pagelets[id] = ''; | ||
} | ||
return html | ||
return html; | ||
} | ||
@@ -387,7 +414,7 @@ | ||
if (this._usePagelet) { | ||
this._title = title | ||
this._title = title; | ||
} | ||
return title | ||
return title; | ||
} | ||
module.exports = Resource | ||
module.exports = Resource; |
{ | ||
"name": "scrat-swig", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "Extensions of swig for scrat", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -22,3 +22,3 @@ exports.compile = function (compiler, args, content, parents, options, blockName) { | ||
attrs = attrs.length ? ' ' + attrs.join(' ').replace(/["\\]/g, '\\$&') : ''; | ||
ret.push('_output += "<' + tag + ' data-pagelet=\\"" + _ext._resource.pageletId(' + id + ') + "\\"' + attrs + '>";'); | ||
ret.push('_output += "<' + tag + attrs + ' data-pagelet=\\"" + _ext._resource.pageletId(' + id + ') + "\\">";'); | ||
} else { | ||
@@ -25,0 +25,0 @@ ret.push('_output += "<!-- pagelet[" + _ext._resource.pageletId(' + id + ') + "] start -->";'); |
23145
791