Comparing version 0.0.3 to 0.0.4
var fs = require('fs'), | ||
path = require('path'), | ||
util = require('util'), | ||
uglify = require('uglify-js'); | ||
uglify = require('uglify-js'), | ||
url = require('url'); | ||
@@ -198,2 +199,27 @@ var JSLoader = function(srcDirs, opt) { | ||
JSLoader.handleRequest = function(req, res, jsloader) { | ||
var files, content, query; | ||
query = url.parse(req.url, true).query; | ||
files = query.sources.split(','); | ||
minify = false; | ||
if (query.minify) minify = true; | ||
content = jsloader.getContent(files, function(err, content) { | ||
res.writeHead(200, {'Content-Type': 'text/javascript'}); | ||
res.end(content); | ||
}, minify); | ||
}; | ||
JSLoader.connect = function(urlPath, srcDirs) { | ||
var jsloader = new JSLoader(srcDirs); | ||
return function(req, res, next) { | ||
var pathname = url.parse(req.url).pathname; | ||
if (pathname === urlPath) { | ||
JSLoader.handleRequest(req, res, jsloader); | ||
} else { | ||
req.jsloader = jsloader; | ||
next(); | ||
} | ||
}; | ||
}; | ||
module.exports.JSLoader = JSLoader; |
{ "name": "js-loader" | ||
, "description": "On-the-fly javascript contacatenator, minifier and dependency resolver for client-side JS" | ||
, "version": "0.0.3" | ||
, "version": "0.0.4" | ||
, "homepage": "https://github.com/dmcquay/node-js-loader" | ||
@@ -5,0 +5,0 @@ , "repository": "git://github.com/dmcquay/node-js-loader.git" |
@@ -22,5 +22,9 @@ #Node JS Loader (client-side) | ||
##Using as a standalone server | ||
Usage: jsloader ADDRESS PORT JS_SOURCE_DIR [JS_SOURCE_DIR...] | ||
Run the server | ||
jsloader /home/dmcquay/myproject/js | ||
jsloader 127.0.0.1 1234 /path/to/js | ||
@@ -31,3 +35,2 @@ Request your files | ||
And if the first line of c.js looks like this: | ||
@@ -45,4 +48,36 @@ | ||
jsloader /home/dmcquay/myproject/js1 /home/dmcquay/myproject/js2 | ||
jsloader 127.0.0.1 1234 /path/to/js1 /path/to/js2 | ||
##Advanced integration with Node.js projects | ||
If your project is written in Node.js and you are using connect, then you have two more options. First, | ||
set up the connect middleware. | ||
app.configure(function() { | ||
... | ||
require('js-loader').JSLoader.connect('/js', ['/path/to/js']) | ||
... | ||
}); | ||
The first parameter is the pathname that the jsloader should handle. The second is an array of javascript | ||
source directories. With this in place, you can use your existing connect server to serve your js. | ||
<script type="text/javascript" src="http://www.mysite.com/js?sources=a.js,b.js,c.js"></script> | ||
The connect middleware will also make the jsloader instance available on all requests so you can request | ||
JavaScript content on-the-fly and embed it directly in the page. To do this, you'll want to generate | ||
the JavaScript in your controller and pass it to your view. | ||
req.jsloader.getContent(['cool.js'], function(err, jsCode) { | ||
res.render('myview.ejs', { | ||
locals: { | ||
jsCode: jsCode | ||
} | ||
}); | ||
}); | ||
Then, in your view, display the content. Be sure not to escape it. | ||
<script type="text/javascript"><%- jsCode %></script> | ||
#Planned features | ||
@@ -49,0 +84,0 @@ |
Sorry, the diff of this file is not supported yet
31616
314
85