Rex is "commonjs in the browser".
It's available through npm:
npm install rex
There is also a cli program available
npm install -g rex-cli
rex --help
Usage is simple:
varrex=require('rex');varparse=rex();parse('test.js',function(err,compiled){// this outputs test.js compiled for browser usageconsole.log(compiled);});Your browserside javascript can now use require to require other modules and exports and module.exports just like in node.js.
Additionally if your module exports a global variable with the same name as your module require will still work!
If you require a module like require('my-module') rex will look for it in the nearest browser_modules or node_modules folder. If rex can't resolve a module name, no error is raised, instead require returns null on runtime.
// browserside codevarfoo=require('./foo');// will look for foo.js in the same foldervarbar=require('bar');// will look for bar.js or bar/index.js in // the nearest browser_modules or node_modules foldermodule.exports=function(){// will export a functionreturn'hello from module';};You can pass a set of options with rex(options). They include:
base: Specify a base js file. Rex will now assume that base's dependencies are loaded for all other requests.dependencies: A map of global dependencies to be loaded in the client. This could be jQuery from a cdn i.e.{jQuery:'http://cdn.com/jQuery.js'}.
MIT