modulejs

lightweight JavaScript module system

2.9.0

modulejs is a lightweight JavaScript module system (only ~2kB minified). It is not a module loader, it triggers no file system lookups or HTTP requests. It simply helps organizing code in small, maintainable and easy to use modules. Modules respect and resolve dependencies, the syntax is very similar to that of RequireJS. This is the module system used in h5ai.

Usage

Define a module without dependencies.

modulejs.define('a', function () {
    // do and return whatever you like
    // ...
    return {val: 1};
});

Define a module with dependencies.

modulejs.define('b', ['a'], function (a) {
    // ...
    return [a.val, a.val + 1];
});

Define another module.

modulejs.define('main', ['jquery', 'b'], function ($, b) {
    // ...
    return {
        start: function () {
            console.log(b);
        }
    };
});

It's easy to register 3rd party objects.

modulejs.define('modernizr', Modernizr);

But you need to be careful with 'objects' that actually are functions, wrap them in functions.

modulejs.define('jquery', function () {
    return jQuery;
});

Finally require one of the defined modules and run some code (for example after all DOM content is loaded).

document.addEventListener('DOMContentLoaded', function () {
    var main = modulejs.require('main');
    main.start();
});

API

define

Defines a module through a constructor function. This function will only be called once when module is first required. The return value will be stored and returned whenever this module will be required.

// id: string, fn: function  ->  undefined
modulejs.define(id, fn)

Same as above but with dependencies that get resolved first and will be passed in as arguments to the constructor.

// id: string, deps: array of strings, fn: function  ->  undefined
modulejs.define(id, deps, fn)

Defines a module through an already existing object that gets returned whenever the module is required.

// id: string, obj: object  ->  undefined
modulejs.define(id, obj)

Same as above but with dependencies that get resolved first.

// id: string, deps: array of strings, obj: object  ->  undefined
modulejs.define(id, deps, obj)

require

Returns an already defined module. Creates it if necessary.

// id: string  ->  object
modulejs.require(id)

For testing purposes it's possible to provide mock instances for selected modules to override original module definitions.

// id: string, mocks: object  ->  object
modulejs.require(id, mocks)

for example:

modulejs.require('b', {a: 'testing'})

will resolve a dependency a with the string testing instead of the real module.

state

Returns an object that represents the current state of all modules.

//  ->  object
modulejs.state()

returns an object of the form:

{
    // ...
    main: {
        deps: ['jquery', 'b']
        init: true
        reqd: []
        reqs: ['jquery', 'a', 'b']
    }
    // ...
}

log

Returns a string representing module dependencies in a easy to read format. If inv is true it shows dependents for each module.

// inv: boolean  ->  string
modulejs.log(inv)

The result will show all dependencies (transitiv):

* a -> [  ]
* b -> [ a ]
* main -> [ jquery, a, b ]
  modernizr -> [  ]
* jquery -> [  ]

and if inv is true it will show all dependents (transitiv):

* a -> [ b, main ]
* b -> [ main ]
* main -> [  ]
  modernizr -> [  ]
* jquery -> [ main ]

a * indicates whether a module was already instantiated.

create

Returns a fresh, private instances of modulejs with no definitions or instances.

//  ->  modulejs
modulejs.create()
Works best with JavaScript enabled!Works best in modern browsers!