Express middleware to help with the bundling, minifying, obfuscating and versioning of static assets for delivery over a CDN.
Connect/Express-style middleware to help with the bundling, minifying, obfuscating and versioning of static assets for delivery over a CDN.
$ npm install exstatic
varexpress = require'express'exstatic = require'exstatic'staticAssets = exstaticapp = express;staticAssetscreateAssetsFromDirectory'/static/:version/:dirname/:basename.:cacheId:extname'__dirname + '/public';appusestaticAssetsmiddleware;applisten80;
A directory structure like this:
app+- public| +- css| | +- style.css| +- js| +- jquery.js| +- app.js+- app.js
will generate asset URLs similar to the following:
GET /static/1359528874/css/style.d9b28cb1.cssGET /static/1359528874/js/jquery.83f534af.jsGET /static/1359528874/js/app.057d0df0.js
exstatic can be started with some options to help structure generated URLs.
var exstatic = require'exstatic'staticAssets = exstatichost: ''cachePath: __dirname + '/static_cache'version: 'v1';
Creates versioned asset URLs from the contents of one or more directories.
pattern: A pattern string to build the asset URLs. Variables in the pattern are preceded by a colon, just like path parameters in Express routing. The available variables are:
directory: A string or array of strings of directories of files to be served.
Creates a single asset to be served with the specified settings.
type: The preprocessing/compression strategy to use on this collection of files.
settings: An object containing the following parameters:
id (required): A string identifier for this asset.
pattern (required): A pattern string to build the asset URL. Variables in the pattern are preceded by a colon, just like path parameters in Express routing. The available variables are:
files (required): An array of strings of file paths to be preprocessed and/or compressed per the type argument, and be concatenated into a single file.
preprocess: An optional function to allow further preprocessing of a file, after any normal preprocessing done by that type, before being compressed and concatenated with the other members of the files array. The function receives three arguments: the path of the current file, the contents of the current file (a string if a text file, a buffer if binary), and a callback. The callback should be invoked when the work of the function is complete. The callback accepts two arguments: an error object (null if no error occurred) and the result of the processing of the file contents.
In order to start serving the defined assets and reference their generated URLs
from your templates, the exstatic middleware method must be
used by your
varexpress = require'express'exstatic = require'exstatic'staticAssets = exstaticapp = express;// assets get defined here// express configuration gets started hereappusestaticAssetsmiddleware;applisten80;
To render the CDN-friendly asset URL from the asset defined in the above
createAsset example, just use the
When creating assets using
createAsset, a type must be specified as the first
argument. The following types are currently available:
Additional types can be created simply by inheriting the
Asset object defined
in lib/Asset.js. The built-in types (in lib/assets/) can help in getting
To use your own types, make sure the folder they're kept in is included in the
typeSearchPath setting initially passed to exstatic.
Examples are available with the git repository. Each requires Express v3.x, and should be run from within it's own directory.
Assuming express is installed and accessible from where
git clone is run:
$ git clone firstname.lastname@example.org:2sidedfigure/exstatic.git$ cd exstatic/examples/js$ node index.js
± ryan (email@example.com). Development was and continues to be sponsored by YellowBot.
Copyright (c) 2013 Ryan Ettipio
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.