Skip to content

Instantly share code, notes, and snippets.

@secf4ult
Last active September 4, 2019 10:46
Show Gist options
  • Save secf4ult/11a972de24294d35afbae72cf1fd118b to your computer and use it in GitHub Desktop.
Save secf4ult/11a972de24294d35afbae72cf1fd118b to your computer and use it in GitHub Desktop.
// 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