Lazy module loader which returns one object of modules that are needed to be instantiated one time only (Singleton Objects). This loader can be accessed in the global scope and it is very easy to use.
You reach demo through this link
-
install latest Node JS version by going to https://nodejs.org/
-
install Browserify
npm install -g browserify
- Configure your new modules in ModulesLoader.js by adding a new Switch case
case 'SampleModule':
this.objects[objectClassName] = require('./SampleModule.js');
break;
- Load the ModulesLoader to a global variable like the following
global.ML = require('./js/ModulesLoader');
- Now you can call the new module methods wherever you need by using "gI" which stands for "get instance"
//gets instance which is only one
ML.gI('SampleModule').printMessage();
- Bundle the new JS file using Browserify in terminal
$browserify main.js -o bundle.js
- inject bundle.js in your website html
<script src="bundle.js"></script>
That's it! did you like it?
If you need more details, you can view the source code of the demo.
Related Article: https://goodselects.com/organize-loading-js-modules-browserify-apps/
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.