Asynchronous/synchronous CommonJS module loader for javascript.
For a module loaded directly from your web page:
- XHR is used to asynchronously request the module file from the server
- The module source is statically analysed for sub-dependencies, and any found are themselves (recursively) asynchronously requested, analysed and built until all dependencies and subdependencies of the original module are available
- The original requested module is built
- The callback is called, and the original module is passed as a parameter
<script src="require/index.js"></script>
<script>
require('modules/a.js', function(module) {
console.log(module); // Loaded module
console.log(require.cache); // Cache of all modules required (including indirectly) by a.js
});
</script>
Once all dependent modules have been analysed and loaded, the loader exposes a synchronous version of require() when building modules, that retrieves a given module from the cache and returns it synchronously to any module that requires it.
var b = require("a.js");
- The global
require(modulePath, callback)function also supports an array of modulePaths:require([modulePath1, modulePath2], callback), and executes the callback once all specified modules have been loaded. - You can inspect the module cache at any time by inspecting
require.cache - As per node/CommonJS expectations, recursive dependencies are resolved by substituting an empty object for the first "repeated" module. Eg, for module
Awhich depends onBwhich depends onA, moduleBis first built with an empty object supplied forA, and thenAis build with the instantiated version ofB.