Skip to content

Latest commit

 

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Require

Asynchronous/synchronous CommonJS module loader for javascript.

Usage

From within a web page

For a module loaded directly from your web page:

  1. XHR is used to asynchronously request the module file from the server
  2. 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
  3. The original requested module is built
  4. 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>

From within a CommonJS module

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");

Additional notes

  • 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 A which depends on B which depends on A, module B is first built with an empty object supplied for A, and then A is build with the instantiated version of B.

About

Toy CommonJS sync/async module loader for javascript

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages