jade-cache
Version:
Jade cache is a recursive compiler (and middleware), compile all your jade to make it available in cache for fast rendering.
6 lines • 1.79 kB
JavaScript
/*!
* @license jade-cache - v0.0.5
* (c) 2013 Julien VALERY https://github.com/darul75/jade-cache
* License: MIT
*/
!function(){var a={},b=require("fs"),c=(require("colors"),require("jade")),d=require("path");"undefined"!=typeof module&&module.exports&&(module.exports=a);var e,f={debug:!1,routes:["/partials"],views:"views",exclude:[".svn",".DS_Store"],cache:"jade-compiled-templates"},g={},h=".jade";a.init=function(a,b,c){return a=a||{},k.extend(f,a),this.compileJadeTmpl(b),c()},a.handle=function(b,c,d){var e=a.options.routes;if(j("debug: route: ".yellow+b.path),b.app.enabled("jcc")){for(var f=0;f<e.length;f++)if(0!==b.path.indexOf(e[f]))return d();j("debug: using jcc for route: ".yellow+b.path);try{var h=g[b.path];return h?c.send(h()):(j("this route is not in cache".red+b.path),d())}catch(i){throw Error("jade compiled view not found for path: "+b.path+" activate debug option first.")}}return d()},a.compileJadeTmpl=function(b){var c=d.resolve(b.get("views"));e=c,i(c),b.set(a.options.cache,g)};var i=function(f,k){var l,m=(a.options.debug,b.readdirSync(f,"r"));for(var n in m){var o=m[n],p=f+"/"+o,q=d.basename(p);if(!(a.options.exclude.indexOf(q)>=0)&&b.existsSync(p)){var r=b.statSync(p).isDirectory();if(r)k=k?o+"/":k+o+"/",i(p,k);else{if(d.extname(p)!==h){j("debug: ".yellow+o+" is not a jade file, might not be placed here.".yellow);continue}try{l=c.compile(b.readFileSync(d.resolve(f,o),"utf8"),{filename:f+"/"+o});var s=p.replace(e,"").replace(h,"");j("debug: file ".yellow+o+" compiled, put in cache (key) route: ".yellow+s),g[s]=l}catch(t){j("error while compiling".red+p.red)}}k=""}}k+="/"},j=function(b){a.options.debug&&console.log(b)},k={extend:function(a,b){if(!b||"function"==typeof b)return a;for(var c in b)a[c]=b[c];return a}};a.functions=k,a.options=f}();