Skip to content

Commit

Permalink
Merge pull request #17 from bengourley/master
Browse files Browse the repository at this point in the history
Support for Express 2.x and 3.x (fixes #14), also fixes #10
  • Loading branch information
Paul Serby committed Jul 7, 2012
2 parents cc8c020 + e9c6528 commit 72c561b
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 181 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ compact.addNamespace('comments', __dirname + 'libs/comments/public/src/' )
If you have created a `global` namespace, apply it to all routes like so:

```js
app.use(compact.js(['global']));
app.use(compact.middleware(['global']));
```

This will expose the view helper `compactJsHtml()` in your templates, so you can output the necessary `<script>` tags.
Expand All @@ -73,17 +73,23 @@ Selectively apply namespaces to routes:
```js
// Add some compacted JavaScript for just this route. Having the namespaces
// in separate arrays will produce a javascript file per array.
app.get('/', compact.js(['home'], ['profile']));
app.get('/', function (req, res) {
app.get(
'/',
compact.js(['home'], ['profile']),
function (req, res) {
/* Homepage logic here */
});
}
);

// Having different namespaces joined together
// will combine and output as one javascript file.
app.get('/blog', compact.js(['comments', 'profile']));
app.get('/', function (req, res) {
/* Blog page logic here */
});
app.get(
'/',
compact.js(['comments', 'profile']),
function (req, res) {
/* Blog page logic here */
}
);
```

Note: compact must be applied to your route *before* the route logic. This is so that the view helper is available when you try to render your layout.
Expand Down
29 changes: 15 additions & 14 deletions lib/compact.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,29 @@ module.exports.createCompact = function(options, globalUglifyOptions) {
}
}

function compactJavascript() {
function middleware() {

if (arguments.length === 0) {
throw new Error('You must pass one or more arrays containing valid namespace names');
}

var namespaceGroups = Array.prototype.slice.call(arguments);

return function(req, res, next) {
processNamespaceGroups(namespaceGroups, function(error, results) {
if (error) {
return next(error);
}
var app = req.app;
app.configure(function() {
app.helpers({
compactJs: function() {
return results;
},
compactJsHtml: function() {
return results.map(function(filename) {
return '<script src="' + filename + '"></script>';
}).join('');
}
});

res.locals({
compactJs: function () {
return results;
},
compactJsHtml: function() {
return results.map(function(filename) {
return '<script src="' + filename + '"></script>';
}).join('');
}
});

next();
Expand All @@ -231,7 +231,8 @@ module.exports.createCompact = function(options, globalUglifyOptions) {

return {
addNamespace: addNamespace,
js: compactJavascript,
middleware: middleware,
js: middleware,
ns: namespaces,
globalUglifyOptions: globalUglifyOptions
};
Expand Down
Loading

0 comments on commit 72c561b

Please sign in to comment.