Resave Browserify
A middleware for compiling and saving Browserify bundles. Use with Connect or Express and static middleware. Built with Resave.
const connect = require('connect');
const resaveBrowserify = require('resave-browserify');
const serveStatic = require('serve-static');
const app = connect();
app.use(serveStatic('./public'));
app.use(resaveBrowserify({
bundles: {
'/main.js': './source/main-bundle.js'
},
savePath: './public'
}));
app.listen(3000);
Table Of Contents
Install
Install Resave Browserify with npm:
npm install resave-browserify
Getting Started
Require in Resave Browserify:
const resaveBrowserify = require('resave-browserify');
Use the created middleware in your application:
const connect = require('connect');
const app = connect();
app.use(resaveBrowserify({
bundles: {
'/main.js': './source/main-bundle.js'
}
}));
In the example above, requests to /main.js
will load the file ./source/main-bundle.js
, run it through Browserify, and serve it up.
This isn't great in production environments as it can be quite slow. In these cases you can save the output to a file which will get served by another middleware:
const connect = require('connect');
const serveStatic = require('serve-static');
const app = connect();
app.use(serveStatic('./public'));
app.use(resaveBrowserify({
bundles: {
'/main.js': './source/main-bundle.js'
},
savePath: './public'
}));
In the example above the first time /main.js
is requested it will get browserified and saved into public/main.js
. On the next request, the serve-static
middleware will find the created file and serve it up with proper caching etc.
Options
basePath
(string)
The directory to look for bundle files in. Defaults to process.cwd()
.
browserify
(object)
A configuration object which will get passed into Browserify. See the Browserify options documentation for more information.
If NODE_ENV
is 'production'
, it defaults to:
{
debug: false
}
If NODE_ENV
is not 'production'
, it defaults to:
{
debug: true
}
bundles
(object)
A map of bundle URLs and source paths. The source paths are relative to the basePath
option. In the following example requests to /foo.js
will load, compile and serve source/foo-bundle.js
:
app.use(resaveBrowserify({
basePath: 'source'
bundles: {
'/foo.js': 'foo-bundle.js'
}
}));
log
(object)
An object which implments the methods error
and info
which will be used to report errors and information.
app.use(resaveBrowserify({
log: console
}));
savePath
(string)
The directory to save bundled files to. This is optional, but is recommended in production environments. This should point to a directory which is also served by your application. Defaults to null
.
Example of saving bundles only in production:
app.use(resaveBrowserify({
savePath: (process.env.NODE_ENV === 'production' ? './public' : null)
}));
Examples
Basic Example
Bundle some JavaScript files together with Browserify and serve them up.
node example/basic
Contributing
To contribute to Resave Browserify, clone this repo locally and commit your code on a separate branch.
Please write unit tests for your code, and check that everything works by running the following before opening a pull-request:
make lint test
License
Resave Browserify is licensed under the MIT license.
Copyright © 2015, Rowan Manning