Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a45c718
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# enchilada | ||
|
||
serve up your javascript files all wrapped up. Yum! | ||
|
||
```javascript | ||
var app = express(); | ||
|
||
// serves up all your javascript files, handling all require() calls | ||
app.use(enchilada(__dirname + '/public')); | ||
|
||
// fallback for other static resources | ||
app.use(express.static(__dirname + '/public')); | ||
``` | ||
|
||
## with ingredients | ||
|
||
No one likes a stale enchilada. Out in the real world, you want to leverage browser caching for rarely changing files. | ||
|
||
Just add the proper ingredients and your enchilada will be served up as you requested. | ||
|
||
```javascript | ||
app.use(enchilada({ | ||
src: __dirname + '/public', | ||
routes: { | ||
// key is the url route, value is either a file under src or a module | ||
'/js/jquery.js': '/js/jquery.js', | ||
// here we serve up a module | ||
'/js/engine.io.js': 'engine.io-client' | ||
} | ||
}); | ||
``` | ||
## install | ||
Install with [npm](https://npmjs.org) | ||
```shell | ||
npm install enchilada | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// builtin | ||
var path = require('path'); | ||
|
||
// vendor | ||
var mime = require('mime'); | ||
var script = require('script'); | ||
|
||
module.exports = function enchilada(opt) { | ||
|
||
// if just a path is passed in, treat as public file dir | ||
if (typeof opt === 'string') { | ||
opt = { src: opt }; | ||
} | ||
|
||
var pubdir = opt.src; | ||
var routes = opt.routes || {}; | ||
var bundles = {}; | ||
|
||
var externs = Object.keys(routes).map(function(id) { | ||
var name = routes[id]; | ||
|
||
// if the name is not relative, then it is a module | ||
if (name[0] !== '/') { | ||
return bundles[id] = script.module(name); | ||
} | ||
|
||
var jsfile = path.join(pubdir, id); | ||
return bundles[id] = script.file(jsfile); | ||
}); | ||
|
||
return function(req, res, next) { | ||
if (mime.lookup(req.url) !== 'application/javascript') { | ||
return next(); | ||
}; | ||
|
||
res.contentType('application/javascript'); | ||
|
||
if (req.url === '/js/require.js') { | ||
res.sendfile(script.client.filename); | ||
return; | ||
} | ||
|
||
var bundle = bundles[req.url]; | ||
|
||
// lookup in filesystem | ||
if (!bundle) { | ||
var jsfile = path.join(pubdir, req.url); | ||
bundle = script.file(jsfile, { | ||
main: true, | ||
external: externs | ||
}); | ||
} | ||
|
||
bundle.generate(function(err, src) { | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
res.send(src); | ||
}); | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "enchilada", | ||
"version": "0.0.0", | ||
"description": "express/connect middleware for javascript bundles", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/shtylman/node-enchilada.git" | ||
}, | ||
"keywords": [ | ||
"express", | ||
"script", | ||
"bundle" | ||
], | ||
"author": "Roman Shtylman <shtylman@gmail.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"mime": "1.2.7", | ||
"script": "0.1.0-pre3" | ||
} | ||
} |