Comparing version 0.1.6 to 0.2.0
97
index.js
@@ -5,13 +5,5 @@ var layer = require('./lib/layer.js'); | ||
exports.init = function(settings, app) { | ||
var engine; | ||
if (arguments.length === 1) { | ||
app = settings; | ||
settings = {}; | ||
} | ||
function yogViewEngine(app, engine, settings) { | ||
// 将res注入到locals中提供模板渲染时使用 | ||
app.use(function(req, res, next) { | ||
app.use(function (req, res, next) { | ||
res.locals.__res__ = res; | ||
@@ -22,44 +14,55 @@ next(); | ||
settings.views = app.get('views'); | ||
engine = new (_.resolveEngine(settings.engine || 'yog-swig'))(app, settings); | ||
return function(filepath, locals, done) { | ||
var engineClass = _.resolveEngine(engine); | ||
this.engine = new engineClass(app, settings); | ||
this.settings = settings; | ||
} | ||
var res = locals.__res__; | ||
yogViewEngine.prototype.cleanCache = function () { | ||
this.engine.cleanCache && this.engine.cleanCache(); | ||
}; | ||
// 初始化 layer 层。 | ||
// 提供 addScript, addStyle, resolve, addPagelet 各种接口。 | ||
// 用来扩展模板层能力。 | ||
var prototols = layer(res, settings); | ||
yogViewEngine.prototype.renderFile = function (filepath, locals, done) { | ||
var res = locals.__res__; | ||
var settings = this.settings; | ||
var sentData = false; | ||
// 初始化 layer 层。 | ||
// 提供 addScript, addStyle, resolve, addPagelet 各种接口。 | ||
// 用来扩展模板层能力。 | ||
var prototols = layer(res, settings); | ||
engine.makeStream(filepath, _.mixin(locals, {_yog: prototols})) | ||
var sentData = false; | ||
// 合并 tpl 流 和 bigpipe 流。 | ||
.pipe(combine(prototols)) | ||
this.engine.makeStream(filepath, _.mixin(locals, { | ||
_yog: prototols | ||
})) | ||
// 合并 tpl 流 和 bigpipe 流。 | ||
.pipe(combine(prototols)) | ||
// 设置默认content-type | ||
.on('data', function () { | ||
sentData = true; | ||
if (!res.get('Content-Type')) { | ||
res.type('html'); | ||
} | ||
}) | ||
// bigpipe异步回调异常 | ||
.on('error', function (error) { | ||
// 属于 chunk error | ||
if (sentData) { | ||
if (typeof settings.chunkErrorHandler === 'function') { | ||
settings.chunkErrorHandler(error, res); | ||
} else { | ||
res.write('<script>window.console && console.error("chunk error", "' + error.message.replace( | ||
/"/g, | ||
"\\\"") + '")</script>'); | ||
} | ||
res.end(); | ||
} else { | ||
// 模板渲染前报错,传递至next | ||
done(error); | ||
} | ||
}) | ||
// 直接输出到 response. | ||
.pipe(res); | ||
}; | ||
.on('data', function() { | ||
sentData = true; | ||
if (!res.get('Content-Type')) { | ||
res.type('html'); | ||
} | ||
}) | ||
.on('error', function(error) { | ||
// 属于 chunk error | ||
if (sentData) { | ||
if (typeof settings.chunkErrorHandler === 'function') { | ||
settings.chunkErrorHandler(error, res); | ||
} else { | ||
res.write('<script>window.console && console.error("chunk error", "'+ error.message.replace(/"/g, "\\\"") +'")</script>'); | ||
} | ||
res.end(); | ||
} else { | ||
// 模板渲染前报错,传递至next | ||
done(error); | ||
} | ||
}) | ||
// 直接输出到 response. | ||
.pipe(res); | ||
}; | ||
}; | ||
module.exports = yogViewEngine; |
{ | ||
"name": "yog-view", | ||
"version": "0.1.6", | ||
"description": "An express.js middleware for optimizing the order of js\\css output, and enabling render template in bigpipe mode.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/fex-team/yog-view.git" | ||
}, | ||
"keywords": [ | ||
"express", | ||
"middleware", | ||
"chunk", | ||
"view" | ||
], | ||
"author": "fex-team", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/fex-team/yog-view/issues" | ||
}, | ||
"homepage": "https://github.com/fex-team/yog-view", | ||
"dependencies": { | ||
"caller": "0.0.1" | ||
} | ||
} | ||
"name": "yog-view", | ||
"version": "0.2.0", | ||
"description": "An express.js middleware for optimizing the order of js\\css output, and enabling render template in bigpipe mode.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/fex-team/yog-view.git" | ||
}, | ||
"keywords": [ | ||
"express", | ||
"middleware", | ||
"chunk", | ||
"view" | ||
], | ||
"author": "fex-team", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/fex-team/yog-view/issues" | ||
}, | ||
"homepage": "https://github.com/fex-team/yog-view", | ||
"dependencies": { | ||
"caller": "0.0.1" | ||
} | ||
} |
22330
562