Giter Site home page Giter Site logo

papandreou / assetgraph-middleware Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 112 KB

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

License: BSD 3-Clause "New" or "Revised" License

JavaScript 100.00%

assetgraph-middleware's Introduction

assetgraph-middleware

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

Note: This module should be considered experimental for now. It doesn't yet persist the manipulated assets to disc, so it will break if your server has multiple workers.

Installation

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

npm install assetgraph-middleware

Features

  • Use any of the existing assetgraph transforms to express the manipulations you want performed (very high level syntax).
  • Very composable. The original HTML files can originate from any other other middleware, eg. a static file server, a templating engine, or even an http proxy.
  • Acts as a cache for the generated assets so the optimizations don't need to be performed on every request.
  • Makes good use of the downstream middleware's support for conditional GET via ETags.

Example

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,
        processUrls: /\/$|\.html$/,
        transform: function (assetGraph, cb) {
            // This transform will be run on an assetgraph object that contains only the Html asset.
            assetGraph
                // Populate the assetgraph with the related assets by following <script>,
                // <link rel="stylesheet">, url(...), and <link ref="shortcut icon">:
                .populate({
                    followRelations: {
                        type: ['HtmlStyle', 'HtmlScript', 'CssImage', 'HtmlShortcutIcon'],
                        to: {
                            url: /^file:/
                        }
                    }
                })
                // Bundle Css and JavaScripts:
                .bundleRelations({type: ['HtmlStyle', 'HtmlScript']})
                // Turn <script src=...> into inline scripts:
                .inlineRelations({type: 'HtmlScript'})
                // Pretty-print Html, JavaScript, and Css assets:
                .prettyPrintAssets({type: ['Html', 'JavaScript', 'Css']})
                // Rename assets to <md5 of contents>.<original extension>
                .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);

License

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

assetgraph-middleware's People

Contributors

papandreou avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

assetgraph-middleware's Issues

Eliminate race conditions

Eg.if a second request wants an asset that's being manipulated, the work will be done twice. Use some sort of queue for that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.