ender-js/ender-js — pull request #13 by vic (2012-03-09 05:43:43+00:00)
Title: Enable require function to find relative modules.

The new `require` function allows for packages to require files
based on its relative position.

Modules are defined with the `require.def` function which takes
three parameters `(package_name, file_path, loader(require))`.

`package_name` is the feature that can be required by the user,
for example for underscore package, `require("underscore")` would
load the package's main file.

`file_path` is an string indicating the package-relative location
of the main javascript file, in ender no file is actually loaded,
but the location is still needed for allowing it to require files
relative to the main file and have them just-work like in node.

`loader` is a function taking a single argument, a `require`
function that can be used inside the module code to load files
relative to the file_path.

The ender.js code generated by ender-js/Ender#126 to define
relative modules looks something like this:

```js
// ender-js client-side code goes here.

// Now suppose we have a foo package whose main file is index.js
// and it has bar.js inside its lib directory.

require.def('foo', 'foo/index.js', function(require) {
var __dirname = require.dirname;
var __filename = require.filename;
var module = { exports: {} }, exports = module.exports;

/* START CONTENT FROM index.js */
(function() {

// our main file uses relative require to load a file
// from the lib directory just like in node.
var bar = require('./lib/bar')
bar('world');

})()
/* END CONTENT FROM index.js */

require.provide(module.exports);
return module.exports;
});

require.def(null, 'foo/lib/bar', function(require) {
var __dirname = require.dirname;
var __filename = require.filename;
var module = { exports: {} }, exports = module.exports;

/* START CONTENT FROM lib/bar.js */
(function() {

module.exports = function(who) {
console.log('Hello '+who);
}

})()
/* END CONTENT FROM lib/bar.js */

require.provide(module.exports);
return module.exports;
});

```

Now after `ender.js` has been included, the user can run
`require("foo")`. Note that the bar file can also be loaded using
an absolute path like `require("/foo/lib/bar")`.

This patch implementes a basic `path` module to resolve path names,
it has some basic functions that can be used with `require('/path')`