Permalink
Browse files

cli version and all generated code is common.js - require browserside…

… templates
  • Loading branch information...
1 parent f804ff3 commit 5401a9fea395e10c1ef97a72af4c43d3997efa0a @mafintosh committed Dec 21, 2012
Showing with 41 additions and 7 deletions.
  1. +26 −0 cli.js
  2. +11 −6 index.js
  3. +4 −1 package.json
View
@@ -0,0 +1,26 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+var pejs = require('./index');
+
+var filename = process.argv[2];
+
+if (!filename || filename[0] === '-') {
+ console.error('usage: pejs filename');
+ process.exit(1);
+}
+
+if (!fs.existsSync(filename)) {
+ console.error(filename+' does not exist');
+ process.exit(2);
+}
+
+filename = fs.realpathSync(filename);
+
+pejs.parse(filename, function(err, src) {
+ if (err) {
+ console.error(err.message);
+ process.exit(3);
+ }
+ console.log(src);
+});
View
@@ -142,11 +142,10 @@ var compile = function(tree) {
return $t;
};
- var out = '(function() {\n';
-
- out += '\tvar template = '+template.toString()+';\n';
- out += '\tvar reduce = '+stringify(tree, '').split('\n').join('\n\t')+';\n';
- out += '\treturn function (locals) {\n\t\tvar $t = template();\n\t\treduce($t,locals || {});\n\t\treturn $t.toString();\n\t};\n}());';
+ var out = '';
+ out += 'var template = '+template.toString().split('\n\t').join('\n')+';\n';
+ out += 'var reduce = '+stringify(tree, '')+';\n';
+ out += 'module.exports = function (locals) {\n\tvar $t = template();\n\treduce($t,locals || {});\n\treturn $t.toString();\n};';
return out;
};
@@ -284,6 +283,12 @@ exports.parse = function(name, callback) {
});
};
+var requireSource = function(source) {
+ var module = {exports:{}};
+ vm.runInNewContext(source, {console:console, module:module});
+ return module.exports;
+};
+
exports.render = function(name, locals, callback) {
if (typeof locals === 'function') return exports.render(name, {}, locals);
@@ -303,7 +308,7 @@ exports.render = function(name, locals, callback) {
if (err) return callback(err);
try {
- cache[name].render = cache[name].render || vm.runInNewContext(source, {console:console});
+ cache[name].render = cache[name].render || requireSource(source);
} catch (err) {
return callback(err);
}
View
@@ -1,10 +1,13 @@
{
"name":"pejs",
- "version":"0.3.5",
+ "version":"0.4.0",
"keywords": ["template", "ejs", "inheritance", "render"],
"description":"Pre-compiled EJS with inheritance, block and file support that works both in the client and on the server",
"repository" : "git://github.com/gett/pejs",
"author": "Ge.tt <hello@ge.tt>",
+ "bin": {
+ "pejs":"./cli.js"
+ },
"dependencies": {
"find-module": "~0.1.1"
},

0 comments on commit 5401a9f

Please sign in to comment.