Simple Browserify middleware for Express with basic in-memory caching and watchify support.
I created this as a Gist instead of Node module because Browserify bundles are one of those things better left customizable. This function, as it stands, is very limited. It has no support for things like transforms and plugins. Copy this into your app and add any desired functionality there.
Depends on:
- Browserify - tested on v6.1, expected to work on v4.0+
- Watchify - tested on v2.0, expected to work on v1.0+. Watchify is optional. Remove watchify
require
statement if not desired. - Express - tested with v4.9, expected to work with v3.0+
browserifyMiddleware( entryFileName [, options ] )
entryFileName
(String) - The entry file to be browserified. Relative to current working directory.options
(Object) - A hash of options. A few properties are specific to this middleware and the rest are passed to the Browserify bundle.watchify
(Boolean) - Use watchify. Clears the cache whenever files change, forcing the bundle to be rebuilt on next request. Useless ifcacheResult
is false.cacheResult
(Boolean) - Cache the result in memory. Without watchify enabled, the browserify bundle will only compile once. The default value istrue
.
This example creates a bundle from the file client/index.js
, with watchify and serves it up at /browser.js
.
app.get("/browser.js", browserifyMiddleware("client/index.js", { debug: true, watchify: true });
(c) 2014 Tyler Johnson MIT License