Permalink
Browse files

--listen option for cli and inline support

  • Loading branch information...
1 parent fb338d8 commit d6cfffa4ee8945d56ab91e485ed837e9588961af @mafintosh mafintosh committed May 2, 2012
Showing with 50 additions and 7 deletions.
  1. +38 −5 cli/index.js
  2. +1 −1 cli/package.json
  3. +1 −1 index.js
  4. +10 −0 trees.js
View
@@ -2,22 +2,27 @@
var argv = require('optimist')
.alias('o', 'out')
- .alias('m', 'minify')
+ .alias('m', 'main')
+ .alias('c', 'minify')
.alias('h', 'help')
.alias('w', 'watch')
+ .alias('l', 'listen')
.argv;
+var cat = require('cat');
var rex = require('rex');
var fs = require('fs');
+var http = require('http');
var options = {};
if (process.argv.length < 3 || argv.help) {
console.error('\nusage: rex path [options]\n\n'+
'if you create a rex.json file in your js dir rex will use its settings\n\n'+
- '--main -m: specify a main js file that rex can assume is loaded before any other file\n'+
- '--minify : minify the compiled code\n'+
- '--out, -o: compile to a specific path or file suffix if input path is a dir\n'+
- '--watch, -w: watch the file and recompile if it or its dependencies changes. requires -o\n'
+ '--main -m: specify a main js file that rex can assume is loaded before any other file\n'+
+ '--minify -c: minify the compiled code\n'+
+ '--out, -o: compile to a specific path or file suffix if input path is a dir\n'+
+ '--watch, -w: watch the file and recompile if it or its dependencies changes. requires -o\n'+
+ '--listen, -l: start a rex server on a given port serving cwd. port defaults to 8888\n'
);
process.exit(0);
return;
@@ -39,6 +44,34 @@ options.onchange = function() {
var file = process.argv[2];
var parse = rex('.', options);
+
+if (argv.listen) {
+ var port = typeof argv.listen === 'number' ? argv.listen : 8888;
+
+ http.createServer(function(req, res) {
+ res.writeHead(200, {'Content-Type':'text/javascript'});
+
+ if (req.url === '/') {
+ res.end('document.write("<script src=\'http://'+req.headers.host+'/"+encodeURIComponent(""+location)+"\'></script>");');
+ return;
+ }
+
+ var url = decodeURIComponent(req.url.substr(1));
+
+ cat(url, function(err, str) {
+ if (err) return res.end(err.stack);
+
+ var src = (str.match(/<script[^>]+src=[^>]+>((?:\s|\S)+)<\/script>/i) || [])[1];
+
+ parse(new Function(src), function(err, js) {
+ if (err) return res.end(err.stack);
+ res.end(js);
+ });
+ });
+ }).listen(port);
+ return;
+}
+
var stat = fs.statSync(file);
var out = typeof argv.out === 'string' && argv.out;
View
@@ -2,7 +2,7 @@
"name":"rex-cli",
"description":"rex-cli is the cli equivivalent of rex",
"keywords": ["browser", "commonjs", "cli"],
- "version":"0.1.3",
+ "version":"0.1.4",
"author": "Ge.tt <hello@ge.tt>",
"contributors": [
"Mathias Buus Madsen <m@ge.tt>"
View
@@ -155,6 +155,6 @@ module.exports = function(root, options) {
};
return function(req, res, next) {
- return (typeof req === 'string') ? compile(req, res) : middleware(req, res, next);
+ return (typeof req === 'string' || typeof req === 'function') ? compile(req, res) : middleware(req, res, next);
};
};
View
@@ -41,6 +41,13 @@ var findModule = function(name, cwd, callback) {
var resolve = function(url, options, callback) {
var root = options.root;
var cache = {};
+ var inlined;
+
+ if (typeof url === 'function') {
+ inlined = url;
+ url = path.join(options.root, 'source.js');
+ }
+
var humanify = function(path) {
var dir = root.replace(/\/$/, '')+'/';
@@ -57,12 +64,14 @@ var resolve = function(url, options, callback) {
common.step([
function(next) {
+ if (inlined) return next(null, inlined.toString().replace(/^[^\{]*\{/g, '').replace(/\}\s*$/g, ''));
fs.readFile(url, 'utf-8', next);
},
function(source, next) {
mod.dependencies = {};
mod.source = source;
mod.id = md5(url);
+ mod.inlined = inlined;
reqs = parser.requires(source);
@@ -158,6 +167,7 @@ parser.visit = function(tree, fn) {
};
parser.watch = function(tree, fn) {
parser.visit(tree, function(mod) {
+ if (mod.inlined) return;
if (watching[mod.url]) return watching[mod.url].push(fn);
watching[mod.url] = [fn];

0 comments on commit d6cfffa

Please sign in to comment.