From 04204b1440e68fc0da4c0e39321bcc76b2071d1e Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Tue, 15 Mar 2011 23:01:12 -0700 Subject: [PATCH] Added client-side support via: $ make jade.js $ make jade.min.js --- .gitignore | 2 + Makefile | 14 +++- Readme.md | 2 +- lib/jade.js | 2 + support/compile.js | 156 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 support/compile.js diff --git a/.gitignore b/.gitignore index 1271c38fc..651ac53c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .DS_Store lib-cov testing +jade.js +jade.min.js diff --git a/Makefile b/Makefile index 22a1c4e57..65439a05c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ TESTS = test/*.js +SRC = $(shell find lib -name "*.js" -type f) +UGLIFY_FLAGS = --no-mangle test: @./support/expresso/bin/expresso \ @@ -15,4 +17,14 @@ benchmark: node benchmarks/haml2.js && \ node benchmarks/ejs.js -.PHONY: test benchmark +jade.js: $(SRC) + @node support/compile.js $^ + +jade.min.js: jade.js + @uglifyjs $(UGLIFY_FLAGS) $< > $@ + +clean: + rm -f jade.js + rm -f jade.min.js + +.PHONY: test benchmark clean diff --git a/Readme.md b/Readme.md index 9de056ddb..1f626fcad 100644 --- a/Readme.md +++ b/Readme.md @@ -6,7 +6,7 @@ ## Features - - high performance parser + - client-side support - great readability - flexible indentation - block-expansion diff --git a/lib/jade.js b/lib/jade.js index dbefaa2bd..57ecd6031 100755 --- a/lib/jade.js +++ b/lib/jade.js @@ -11,7 +11,9 @@ var Parser = require('./parser') , Compiler = require('./compiler') +// if node , fs = require('fs'); +// end /** * Library version. diff --git a/support/compile.js b/support/compile.js new file mode 100644 index 000000000..850f73586 --- /dev/null +++ b/support/compile.js @@ -0,0 +1,156 @@ + +/** + * Module dependencies. + */ + +var fs = require('fs'); + +/** + * Arguments. + */ + +var args = process.argv.slice(2) + , pending = args.length + , files = {}; + +// note about uglify + +console.log(''); +console.log(' compiling jade.js'); +console.log(''); +console.log(' NOTE: to compile jade.min.js first `$ npm install uglify-js`'); +console.log(' and then execute `$ make jade.min.js`.'); +console.log(''); + +// parse arguments + +args.forEach(function(file){ + var mod = file.replace('lib/', ''); + fs.readFile(file, 'utf8', function(err, js){ + if (err) throw err; + console.log(' \033[90mcompile : \033[0m\033[36m%s\033[0m', file); + files[file] = parse(js); + --pending || compile(); + }); +}); + +/** + * Parse the given `js`, currently supporting:. + * + * 'if' ['node' | 'browser'] + * 'end' + * + */ + +function parse(js) { + var lines = js.split('\n') + , len = lines.length + , buffer = true + , buf = [] + , line + , cond; + + for (var i = 0; i < len; ++i) { + line = lines[i]; + if (/^\/\/ *if *(node|browser)/gm.exec(line)) { + cond = RegExp.$1; + buffer = 'browser' == cond; + } else if (/^\/\/ *end/.test(line)) { + buffer = true; + } else if (buffer) { + buf.push(line); + } + } + + return buf.join('\n'); +} + +/** + * Compile the files. + */ + +function compile() { + var buf = ''; + buf += '\n// CommonJS require()\n\n'; + buf += browser.require + '\n\n'; + buf += 'require.modules = {};\n\n'; + buf += 'require.resolve = ' + browser.resolve + ';\n\n'; + buf += 'require.register = ' + browser.register + ';\n\n'; + buf += 'require.relative = ' + browser.relative + ';\n\n'; + args.forEach(function(file){ + var js = files[file]; + file = file.replace('lib/', ''); + buf += '\nrequire.register("' + file + '", function(module, exports, require){\n'; + buf += js; + buf += '\n}); // module: ' + file + '\n'; + }); + fs.writeFile('jade.js', buf, function(err){ + if (err) throw err; + console.log(' \033[90m create : \033[0m\033[36m%s\033[0m', 'jade.js'); + console.log(); + }); +} + +// refactored version of weepy's +// https://github.com/weepy/brequire/blob/master/browser/brequire.js + +var browser = { + + /** + * Require a module. + */ + + require: function require(p){ + var path = require.resolve(p) + , mod = require.modules[path]; + if (!mod) throw new Error('failed to require "' + p + '"'); + if (!mod.exports) { + mod.exports = {}; + mod.call(mod.exports, mod, mod.exports, require.relative(path)); + } + return mod.exports; + }, + + /** + * Resolve module path. + */ + + resolve: function(path){ + var orig = path + , reg = path + '.js' + , index = path + '/index.js'; + return require.modules[reg] && reg + || require.modules[index] && index + || orig; + }, + + /** + * Return relative require(). + */ + + relative: function(parent) { + return function(p){ + if ('.' != p[0]) return require(p); + + var path = parent.split('/') + , segs = p.split('/'); + path.pop(); + + for (var i = 0; i < segs.length; i++) { + var seg = segs[i]; + if ('..' == seg) path.pop(); + else if ('.' != seg) path.push(seg); + } + + return require(path.join('/')); + }; + }, + + /** + * Register a module. + */ + + register: function(path, fn){ + require.modules[path] = fn; + } +}; \ No newline at end of file