Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

changed middleware to accept single pattern or array of patterns to f…

…ind jade files, updated readme
  • Loading branch information...
commit 61b3b602f0183e14bc68cb8531479f372e161883 1 parent 8480552
@cpetzold cpetzold authored
Showing with 110 additions and 78 deletions.
  1. +11 −3 README.md
  2. +96 −49 index.js
  3. +0 −25 lib/utils.js
  4. +3 −1 package.json
View
14 README.md
@@ -37,11 +37,17 @@ var express = require('express')
, jade_browser = require('jade-browser')
, app = express.createServer();
-app.use(jade_browser('/js/templates.js', __dirname + '/views', options));
+app.use(jade_browser('/js/templates.js', '**', options));
```
+### Params
+ - `filename` The filename of the resulting compiled templates file
+ - `patterns` A single string or array of patterns used to glob for template files
+ - `options` Options object, see below (optional)
+
#### Options
- - `namespace` Namespace for the browser (default: 'jade')
+ - `root` The root of the views (default: __dirname)
+ - `namespace` Namespace for the browser (default: 'jade')
- `minify` Minifies the output (default: false)
- `maxAge` Time in seconds to cache the results (default: 86400)
@@ -70,6 +76,8 @@ jade.render('path/../to/../test');
## Contributors
* Nathan White ([nw](http://github.com/nw))
+ * Adrian Bravo ([adrianbravo](http://github.com/adrianbravo))
+ * Conner Petzold ([cpetzold](http://github.com/cpetzold))
## License
@@ -94,4 +102,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
145 index.js
@@ -1,5 +1,8 @@
var fs = require('fs')
+ , path = require('path')
, jade = require('jade')
+ , async = require('async')
+ , glob = require('glob')
, parser = require('uglify-js').parser
, compiler = require('uglify-js').uglify
, Expose = require('./lib/expose')
@@ -7,67 +10,111 @@ var fs = require('fs')
, utils = require('./lib/utils')
, built = false;
-module.exports = function(path, dir, options){
+module.exports = function(exportPath, patterns, options){
var options = options || {}
, ext = options.ext || 'jade'
, namespace = options.namespace || 'jade'
+ , recursive = (typeof options.recursive != 'undefined') ? options.recursive : true
+ , debug = options.debug || false
, minify = options.minify || false
, maxAge = options.maxAge || 86400
- , path = path.replace(/\/$/,'')
- , dir = dir.replace(/\/$/,'')
- , regexp = utils.toRegExp(path, true)
+ , exportPath = exportPath.replace(/\/$/,'')
+ , root = options.root ? options.root.replace(/\/$/,'') : __dirname
+ , regexp = utils.toRegExp(exportPath, true)
, payload = new Expose()
- , templates = {}
, headers = {
- 'Cache-Control': 'public, max-age=' + maxAge
- , 'Content-Type': 'text/javascript'
+ 'Cache-Control': 'public, max-age=' + maxAge
+ , 'Content-Type': 'text/javascript'
};
return function(req, res, next){
- if(!req.url.match(regexp)){
+ if (!req.url.match(regexp)) {
return next();
}
- if(built){
+
+ if (built) {
res.writeHead(200, headers);
res.end(built);
- } else {
- utils.walk(dir, ext, function(results){
- var file;
- (function getFile(){
- if((file = results.shift()) && file.match(ext+'$')){
- var filename = file.replace(dir+'/', '');
- fs.readFile(file, 'utf8', function(err, content){
- if(!err){
- var tmpl = jade.compile(content,{filename: filename, inline: false, compileDebug: false});
- if(typeof tmpl == 'function') templates[filename] = (function(){
- var fn = 'var jade='+namespace+'; return anonymous(locals);'+ tmpl.toString();
- return new Function('locals', fn);
- })();
- }
- process.nextTick(getFile);
- });
- } else {
- var code = jade.runtime.escape.toString() +';'+ jade.runtime.attrs.toString() + '; return attrs(obj);'
- payload.expose({
- attrs: new Function('obj', code)
- , escape: jade.runtime.escape
- , dirname: utils.dirname
- , normalize: utils.normalize
- , render: render(namespace)
- , templates: templates
- }, namespace, 'output');
- built = payload.exposed('output');
- if(minify){
- var code = parser.parse(built); // parse code and get the initial AST
- code = compiler.ast_mangle(code); // get a new AST with mangled names
- code = compiler.ast_squeeze(code); // get an AST with compression optimizations
- built = compiler.gen_code(code); // compressed code here
- }
- res.writeHead(200, headers);
- res.end(built);
- }
- })();
- });
- }
- }
+ } else {
+
+ if (typeof patterns == 'string') {
+ patterns = [patterns];
+ }
+
+ var files = [];
+ patterns.forEach(function(pattern) {
+ pattern = path.join(root, pattern);
+ try {
+ var matches = glob.globSync(pattern);
+ matches = matches.filter(function(match) {
+ return match.match(ext + '$');
+ });
+ files = files.concat(matches);
+ } catch(e) {}
+ });
+
+ async.map(files, getTemplate, expose);
+
+ function getTemplate(filename, cb) {
+
+ fs.readFile(filename, 'utf8', function(err, content){
+ if (err) {
+ return cb(err);
+ }
+
+ filename = filename.replace(root + '/', '');
+
+ var tmpl = jade.compile(content, {
+ filename: filename
+ , inline: false
+ , compileDebug: false
+ });
+
+ if (typeof tmpl == 'function') {
+ var fn = 'var jade=' + namespace + '; return anonymous(locals);'+ tmpl.toString();
+ fn = new Function('locals', fn);
+
+ cb(null, {
+ filename: filename
+ , fn: fn
+ });
+ } else {
+ cb(new Error('Failed to compile'));
+ }
+
+ });
+ }
+
+ function expose(e, results) {
+ var templates = {};
+ results.forEach(function(template) {
+ templates[template.filename] = template.fn;
+ });
+
+ var code = jade.runtime.escape.toString() +';'+ jade.runtime.attrs.toString() + '; return attrs(obj);'
+
+ payload.expose({
+ attrs: new Function('obj', code)
+ , escape: jade.runtime.escape
+ , dirname: utils.dirname
+ , normalize: utils.normalize
+ , render: render(namespace)
+ , templates: templates
+ }, namespace, 'output');
+
+ built = payload.exposed('output');
+
+ if (minify) {
+ var code = parser.parse(built);
+ code = compiler.ast_mangle(code);
+ code = compiler.ast_squeeze(code);
+ built = compiler.gen_code(code);
+ }
+
+ res.writeHead(200, headers);
+ res.end(built);
+ }
+
+ }
+ }
}
View
25 lib/utils.js
@@ -54,31 +54,6 @@ exports.normalize = function(path) {
return (isAbsolute ? '/' : '') + path;
};
-
-exports.walk = function walk(dir, ext, done) {
- var results = [], i = 0;
- fs.readdir(dir, function(err, list) {
- if (err || !list) return done(results);
- list.forEach(function(f) {
- fs.stat(dir + '/' + f, function(err, stat) {
- if (stat && stat.isDirectory()) {
- walk(dir + '/' + f, ext, function(r) {
- results = results.concat(r);
- if (++i === list.length) {
- done(results);
- }
- });
- } else {
- if (f.match(ext+'$')) results.push(dir + '/' + f);
- if (++i === list.length) {
- done(results);
- }
- }
- });
- });
- });
-};
-
exports.toRegExp = function toRegExp(str, begin, end){
return ((begin) ? '^' : '') +
str.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1') +
View
4 package.json
@@ -12,6 +12,8 @@
}
, "dependencies": {
"jade": "0.14.2"
+ , "async": "0.1.12"
+ , "glob": "2.0.9"
, "uglify-js": "1.0.6"
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.