node.js classes and modules autoloading (without 'require') using EcmaScript 6 Proxy
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Proxy Autoload - Classes and modules autoloading (without 'require') using EcmaScript 6 Proxy

Proxy Autoload uses EcmaScript harmony proxy for supporting autoloading in your projects. Autoloading works like php class autoloading.


You should run node.js with --harmony flag!

For better understanding check our examples.

By default your files should be placed like this:

 │ └─bar
 │   └─Baz.js

So you should name your classes like these:

Just put similar code to your project:

require('proxy-autoload').register(['foo', 'bar'], __dirname);

So you can use all classes or modules from namespaces 'foo' and 'bar' like this:

var baz = new;



 * @param {String|Array.<String>} namespaces one or more namespaces which should support autoloading
 * @param {Object|String} handler object which provides the way to load classes
 * @param {Object|String} [rootSymbol=global] symbol which child symbols will be added to
function register(namespaces, handler, rootSymbol)

Adds one or several symbols to passed root symbol. These symbols will load classes or modules automatically when it's necessary. It's true lazy autoloading. You should run node.js with --harmony flag, because ES6 Proxy used for that.

Handler provides the way to load your classes. If handler is just string then default handler with passed string as root directory will be used. By default classes names begin with first letter in upper case, other symbols are packages. A handler is object which contains 'isClass' and 'loadClass' methods, and can contain 'onCreatePackage' (see the interface of DefaultHandler for details).

If rootSymbol parameter is not specified then namespaces will be added to global scope. If an object passed as rootSymbol then child packages and classes will be added in this symbol. If string passed then symbols will be added to exports[rootSymbol] of this module, and if '.' passed then symbols will be added to exports of this module.


 * @param {String} path root directory for autoloading
 * @constructor
function DefaultHandler(path)

Handler implies the same files structure as following (root - is directory passed as 'path', ns - is namespace which you registered the handler with):

  │ └─bar
  │   └─FooBar.js

So you should name your classes like these:

 * @param {Object} pckg parent symbol
 * @param {String} name current symbol name
 * @return {Boolean}
DefaultHandler.prototype.isClass = function (pckg, name)

Checks whether pckg[name] should be class (module) or package. You can validate symbol name and availability in this method.

 * @param {Object} parent parent symbol
 * @param {String} name package name
 * @param {Object} pckg new package
DefaultHandler.prototype.onCreatePackage = function (parent, name, pckg)

Method runs after new package creating.

 * @param {Object} pckg parent package
 * @param {String} name class name
DefaultHandler.prototype.loadClass = function (pckg, name)

Loads class for passed package and class name.


 * @param obj
 * @return {*}
function isPackage(obj)

Checks whether obj is package or not (root symbol or class)