Last active
September 4, 2019 10:46
-
-
Save secf4ult/11a972de24294d35afbae72cf1fd118b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// common code for implementing require()/exports | |
var dependencies = {} // loaded modules | |
var modules = {} // code of your dependencies | |
// require function | |
var require = function (module) { | |
if (!dependencies[module]) { | |
// module not loaded, let’s load it | |
var exports = {} | |
modules[module](exports) | |
// now in `exports` we have the things made “public” | |
dependencies[module] = exports | |
} | |
return dependencies[module] | |
} | |
// dependendencies | |
modules['jquery'] = function (exports) { | |
// code of jquery | |
} | |
modules['foo'] = function (exports) { | |
// code of bar.js | |
exports.helloWorld = function () { | |
console.log('hello world') | |
} | |
} | |
modules['bar'] = function (exports) { | |
// code of bar.js | |
} | |
// etc… | |
// here goes the code of your "entry file". | |
// Which is the entry point of your code | |
// For example: | |
var $ = require('jquery') | |
var foo = require('foo') | |
var bar = require('bar') | |
foo.helloWorld() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment