Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
defunctzombie committed Nov 26, 2012
0 parents commit a45c718
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
39 changes: 39 additions & 0 deletions README.md
@@ -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
```
63 changes: 63 additions & 0 deletions index.js
@@ -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);
});
};
}

24 changes: 24 additions & 0 deletions package.json
@@ -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"
}
}

0 comments on commit a45c718

Please sign in to comment.