Resource compiler and cacher for Express.
A generic resource compiler meant for use with Express.
compiler middleware will run any data read in through the asyncronous
compile function and
then save the results to the
dest directory. If the source file has not changed, then the compiler
will immediately call
next to allow the next middleware (such as
static) to finish handling the
request, otherwise the
compile function is called with an
outputStream ends (emits the
end event) then
next will be called so the next middleware
can finish the request.
'src' : // The directory to read files from.'dest' : // The directory to save files to.'compile' : // A function to run files through before saving them to disk.
var app = require 'express' ;var compiler = require 'resource-compiler' compiler;// Set up the compiler middleware.appuse '/scripts' compilersrc : __dirname + '/scripts'dest : __dirname + '/static/scripts'// Read in data from input and write it to output.inputStreampipe outputStream ;;// Use static to then serve the data to the client.appuse '/scripts' expressstatic __dirname + '/static/scripts' ;
combiner middleware will read everything in a directory, combine it into one file, and save it
to a file somewhere else.
'src' : // The directory to read files from.'dest' : // The directory to save files to.'ext' : // The file extension to look for when combining files.
This example sets up the combiner to read in files from the
__dirname + '/scripts', combine any
with the extension
.js and write them to the
__dirname + '/static/scripts' directory. Note that
files will be combined alphabetically by file name, so
a-file.js will come before
var app = require 'express' ;var combiner = require 'resource-compiler' combiner;// Set up the combiner middleware.appuse '/scripts' combinersrc : __dirname + '/scripts'dest : __dirname + '/static/scripts'ext : '.js';// Use static to then serve the data to the client.appuse '/scripts' expressstatic __dirname + '/static/scripts' ;
If you had the directory structure:
- scripts/- app/- app.js- ignored.json- someFile.js- widgets/- awesomeWidget.js- coolestWidgetEver.js- /lib- angular.js- jQuery.js- lib.js
And a request of
GET /scripts/app.js came in, the combiner would read in the files, in this order:
Then combine them into one stream and save that to
/static/scripts/app.js. Notice that the file
/scipts/app/ignored.json will not be combined in the final script and that the directory
/scripts/app/widgets was recursed into and each
.js file added as well.