Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
README.md

Brequire

Brings exact CommonJS (require, exports) functionality to the Browser.

It has three parts:

1) A simple compiler that wraps your files in a closure that injects bound of require and exports, so you can use the exact same code client side.

2) A client side library that provides a require keyword

3) An optional clientside asynchronous require module

Install

npm install brequire

API

Create a an instance of the compiler with require("brequire").module("path/to/my/module")

The various options can then be set via method calls:

  • .search(path, /* path2, path3 etc... /) set the search paths for the scripts to load from the root. (defaults to '*.js')

  • .write(path)

wraps each file writes it to disk. if path ends with '.js': the files will all be bundled together into one script. otherwise path is assumed to be a directory and each file will be written out separately, the structure mirroring the src directory

  • .module_base(name) us this method to change the module base name

  • .inspect() output the current state of the compiler including the files to be processed (for debugging purposes)

Example Usage

var brequire = require("brequire")

brequire
  .module("./test/shape")
  .write("./lib/shape.bundle.js")
brequire
  .module("./test/shape")
  .write("./test/browser/lib")
brequire
  .module("./test/user")
  .module_base("user_alt")
  .inspect()
  .write("./test/browser/lib/user.bundle.js")

Include in your page

<script src='browser/require.js'></script>
<script src='my/module.js''></script>
<script>
var app = require("./app")
// do stuff with app
</script>

Async module

require.async.js is an optional module that hooks into require and provides an asynchronous require that loads modules via xhr. It also determines dependencies via static analysis and they are loaded in parallel.

Note that require.async will autowrap the module - so there's no need for the server side compilation step.

Use like:

<script src='browser/require.js'></script>
<script src='my/module.js''></script>
<script>
require("./app", function(app) {
  // do stuff with app  
})
</script>

See example in browser/test/index.html

Notes

  • require.async uses a regex to determine dependencies, which is solid with one caveats:

    • dynamic calls to require will not be picked up, e.g. require(myvar)
  • NB require's inside comments are not picked up

  • require.async works seemlessly with require, so if a dependency is already available, require.async will return immediately with no xhr

  • CoffeeScript lovers:

    • if you want to be able to do require.async("./my.coffee") then just add the following line after coffeescript.js
    • require.registerExtension('.coffee', CoffeeScript.compile)

Test

npm install should glob expresso

Something went wrong with that request. Please try again.