assetgraph-middleware

Express middleware for optimizing and manipulating HTML pages and their related assets while serving them

assetgraph-middleware

Middleware that allows you to use AssetGraph to manipulate HTML pages and their related assets while they are being served.

Make sure you have node.js and npm installed, then run:

npm install assetgraph-middleware

An Express-based static file server that bundles JavaScript and CSS, inlines JavaScript, and assigns md5-derived urls to CSS, shortcut icons and background images:

var express = require('express'),
    root = '/path/to/static/files';

express.createServer()
    .use(express.logger())
    .use(require('assetgraph-middleware')({
        root: root,
        transform: function (assetGraph, cb) {
            assetGraph
                .populate({
                    followRelations: {
                        type: ['HtmlStyle', 'HtmlScript', 'CssImage', 'HtmlShortcutIcon'],
                        to: {
                            url: /^file:/
                        }
                    }
                })
                .bundleRelations({type: ['HtmlStyle', 'HtmlScript']})
                .inlineRelations({type: 'HtmlScript'})
                .prettyPrintAssets({type: ['Html', 'JavaScript', 'Css']})
                .moveAssetsInOrder({type: assetGraph.constructor.query.not('Html')}, function (asset) {
                    return '/static/' + asset.md5Hex + asset.extension;
                })
                .run(cb);
        }
    }))
    .use(express['static'](root))
    .use(express.errorHandler())
    .listen(3000);

3-clause BSD license -- see the LICENSE file for details.