requisite

A cosmic javascript bundler.

npm install requisite
3 downloads in the last day
142 downloads in the last week
144 downloads in the last month

Introduction

Requisite bundles client-side code and templates. It features asynchronous module loading for optimal performance of large applications, CoffeeScript and Jade language support out of the box and comes with a connect/express middleware for rapid development.

Features

  • CommonJS modules in the browser!
  • Customizable compiler/preprocessors.
  • Simple API for programmatic usage.
  • Lazy asset loading.
  • Resolves relative as well as npm modules.
  • Command line tool for bundling simple projects.

Install

npm install -g requisite

Modules

Requiste allows you to structure your code using CommonJS (Node.js) modules. From a given starting point or entry module, requisite will trace your application's dependencies and bundle all require'ed modules together. Requiste's require supports asynchronous loading of assets/modules when an optional callback argument is provided.

// foo.js
module.exports = 'foo';

// async-bar.js
module.exports = 'bar'

// entry.js
console.log(require('./foo'))  // 'foo'
require('./async-bar', function(bar) {
    console.log(bar) // 'bar'
})

This compiles down to:

// ...prelude, defining require, etc.

require.define('/foo', function (module, exports, __dirname, __filename) {
    module.exports = 'foo';
})

require.define('/main', function (module, exports, __dirname, __filename) {
    console.log(require('/foo'));
    require('/async-bar', function(bar) {
        console.log(bar);
    })
})

Note how async-bar.js is missing from the bundle, as it's loaded at runtime.

If you are writing a module that can be used both client/server side you can define the browser field in your package.json and finetune which bits will be bundled for the client.

Usage

CLI

$ bin/requisite --help

Usage: requisite path/to/entry-module [options]

Options:
  -b, --bare                   Compile without a top-level function wrapper
  -e, --export <name>          Export module as <name>
  -i, --include [module, ...]  Additional modules to include, in <require as>:<path to module> format
  -m, --minify                 Minify output
  -o, --output <file>          Write bundle to file instead of stdout
  -p, --prelude <file>         File to use as prelude, or false to disable
      --no-prelude             Exclude prelude from bundle
  -w, --watch                  Write bundle to file and and recompile on file changes
  -x, --exclude <regex>        Regex to exclude modules from being parsed

  -v, --version                Display version
  -h, --help                   Display this help

Example:

requisite entry.js > lib/bundle.js

API

If you want more fine-grained control over requisite you can require it in your own projects and use it directly.

    require('requisite').bundle({
        entry: __dirname + '/entry.js',
    }, function(err, bundle) {
        fs.writeFileSync('app.js', bundle.toString())
    });

Middleware

For development it can be useful to serve bundles up dynamically, and a connect middleware is provided for exactly this purpose. Express example:

  app.use('/js/app.js', require('requisite').middleware({
    entry: __dirname + '/entry.js'
  }))

Which would make your bundle available as http://host/js/main.js.

npm loves you