Introducing AssetRack Dynamic Asset Framework for Nodejs
Posted on

After many a long hot night, AssetRack is a reality. This little open source library is a god send for those looking to take their single page app development to the next level.
The Problem: Why You Need This
Let's say you are writing the next big client-rich app, and you want to use the hottest client side technologies like Less, Jade, Javascript Requires, Coffeescript etc. Well sooner or later you'll realize that compiling all these resources and getting them down to the client starts to become a complex task.
Should you write a build script? Compile all the files to disk? Phone a friend?
In search of the right solution, I identified a few major pain points that I wanted to handle:
- Compiling all assets Just-In-Time for quick dev cycles.
- Ability to serve all assets from an in-memory static cache.
- Ability to push compiled assets to S3 or any other CDN.
- High performance on the client side. *
- Had to work multi-server, multi-process out of the box. *
The Solution
In an attempt to handle these problems in a sane and scalable way, I wrote AssetRack.
Let's consider the classic asset problem of getting your three main resources for your app down to the client.
- /style.css: From your less file.
- /app.js: From all your javascript/coffescript code with node requires.
- /templates.js: From all your jade templates.
Now you can define and create all these assets with this:
var rack = require('asset-rack');
var assets = new rack.AssetRack([
new rack.LessAsset({
url: '/style.css',
filename: __dirname + '/path/to/file.less'
}),
new rack.BrowserifyAsset({
url: '/app.js',
filename: __dirname + '/path/to/app.coffee'
}),
new rack.JadeAsset({
url: '/templates.js',
dirname: __dirname + '/templates'
})
]);
assets.on('complete', function() {
var app = require('express').createServer();
app.use(assets);
app.listen(8000);
});
To learn more check out the complete feature set, documentation, and code on github.