Created
December 19, 2012 19:53
-
-
Save brentjanderson/4339899 to your computer and use it in GitHub Desktop.
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.
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
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')); | |
}); |
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
/** | |
* 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!