Just a simple tool to port node modules to the browser.
What it does
You code your project using the normal Node module structure, then feed the tool with the main file (typically
<project path>/lib/index.js or similar). Node2browser figures out the dependency tree, by parseing calls to the
Modules are loaded using self-invoking anonimous functions.
node2browser simply figures out the dependency tree of your modules, then squash them into a single file.
The modules will then be available in the browser using the
which works pretty the same way as Node's require.
process: has only one method,
window.setTimeout), and an empty array
inspect, just call
inherits, stripped from nodejs
Install and Usage
git clone git://github.com/cheng81/node2browser.git cd node2browser bin/node2browser <initfile> <outfile> <nodemodulesdir> <requiredAs>
initfile is tipically the
index.js in your project. From there, the tool will figure out which file are
nodemodulesdir is where the node modules that you want to include will be.
requiredAs is how you will require the compiled main module in the browser. For example, if you compile your
~/Projects/myLib/lib/index.js, you might want to set this parameter as
Sorry, for now it's very stupid and only single-file modules can be used -it's sufficient for me, if you need more drop a request to frza-AT-itu-DOT-dk-.
A simple example is included the in
test directory, which also uses the wu library. Try it out with
bin/node2browser test/test1.js test/out.js test/node myfoo
After the execution the
test/out.js file is produced, which includes
To require the initial file
test1.js, you can either require
test/index.html uses the produced file, plus shows also how to use
node2browser in the browser.
In the Browser
To use the loaded modules in the browser, just use the
window.node2browser.require function like you would use the Nodejs one, e.g.:
var require = window.node2browser.require var mymod = require('./mymod')
./mymod will be resolved against the directory of the
The grammar that extract the
require calls is dumb. Specifically, it will:
- consider valid a commented call
- not recognize a call if you alias the
var r = require; r('bla')will not work)
Circular dependencies are not handled. The tool will stop with an exception when a circular dependency is found (and I am not sure if even node itself handle circular dependencies).
The parser has been built using the incredibly cool PEGjs peg parser.