Skip to content
forked from getify/LABjs

Ridiculously simple asynchronous JavaScript loader, based on LABjs.

License

Notifications You must be signed in to change notification settings

spikesagal/require

 
 

Repository files navigation

require

Asynchronous require() that is fast and easy to use! This JavaScript loader is based on the super fast and small LABjs, but functions a lot like Node's require.

Basic Usage

require('/path/to/your_script.js', function() {
  GlobalVarDefinedInYourScript.executeFunction();
});  

-OR-

require('/path/to/your_script.js');
wait(function() {
  GlobalVarDefinedInYourScript.executeFunction();
});  

All scripts loaded with require will always execute synchronously, so if you have dependencies:

require('/path/to/core.js');
require('/path/to/your_script_depends_on_core.js');
wait(function() {
  GlobalVarDefinedInYourScript.executeFunction();
});  

Requiring Modules

Requiring modules couldn't be simpler!

var YourModule = require('/path/to/your_module.js', function() {
  YourModule.executeFunction();
});  

-OR-

var YourModule = require('/path/to/your_module.js');
wait(function() {
  YourModule.executeFunction();
});  

Defining Modules

In order to require a module, you must define module.exports, just like in Node. The only difference is that you will need to wrap your code in a function() {} wrapper to make the scope private, and pass module and exports arguments into the wrapper:

(function(module, exports){
    function thisFunctionIsPrivate() {
        // Do stuff
    }
    module.exports = {
        executeFunction: thisFunctionIsPrivate
    }
})($COMMONJS_MODULE, $COMMONJS_MODULE.exports);

That's it, you're done! Enjoy!:)

About

Ridiculously simple asynchronous JavaScript loader, based on LABjs.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 66.1%
  • JavaScript 33.9%