Skip to content

Instantly share code, notes, and snippets.

@brentjanderson
Created December 19, 2012 19:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
An assets management module with convenience methods for quickly creating the objects you actually want to use. Includes Handlebars support through a helper function `asset` which can be used like `{{{asset '/app.js'}}}` to generate the correct link to the correct asset.
var express = require('express')
, http = require('http')
, hbs = require('hbs')
, assets = require('./assets');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
// Setup views using handlebars
app.set('views', __dirname + '/client/views');
app.set('view engine', 'html');
app.engine('html', require('hbs').__express);
app.use(assets); // Serve up compiled assets
});
//
hbs.handlebars.registerHelper('asset', function(tag) {
return assets.tag(tag);
});
app.get('/', function(req, res){
res.render('index');
});
var server = app.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
/**
* Setup asset-rack (See https://github.com/techpines/asset-rack for details)
*/
var rack = require('asset-rack');
/**
* Convenience method to easily create snockets objects using an array of hashmaps
*/
function generateAssets(type, assets, prefix) {
var compiled = [];
for (var i = 0; i < assets.length; i++) {
var options = {
url: assets[i].url,
filename: prefix + assets[i].filename
};
compiled.push(new rack[type](options));
}
return compiled;
}
var snockets = generateAssets("SnocketsAsset", [
{
url: '/app.js',
filename: '/app.js'
},
{
url: '/jquery.js',
filename: '/libs/jquery-1.8.3.min.js'
},
{
url: '/handlebars.js',
filename: '/libs/handlebars.runtime-1.0.rc.1.min.js'
},
{
url: '/ember.js',
filename: '/libs/ember.js'
},
{
url: '/ember-data.js',
filename: '/libs/ember-data.js'
}], __dirname + '/client/js'); // /client/js is the path to the project.
var browserify = generateAssets("BrowserifyAsset", [], '');
var assets = new rack.AssetRack([].concat(snockets, browserify));
assets.on('complete', function() {
console.log('info: asset-rack compilation successful');
});
module.exports = assets;
@brentjanderson
Copy link
Author

Of note, the generateAssets function is poorly designed and does not support passing any options to a rack asset constructor other than "url" and "filename". I'll post a revision later, but for now be warned!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment