Skip to content

Instantly share code, notes, and snippets.

@bspaulding
Created November 22, 2011 20:26
Show Gist options
  • Save bspaulding/1386829 to your computer and use it in GitHub Desktop.
Save bspaulding/1386829 to your computer and use it in GitHub Desktop.
CommonJS Modules Implementation in Pure JS
var exports = {};
var module = {};
function require(id) {
if ( 'undefined' === typeof arguments.callee.require_stack ) { arguments.callee.require_stack = []; }
var require_stack = arguments.callee.require_stack;
if ( 'undefined' === typeof arguments.callee.modules ) { arguments.callee.modules = {}; }
var modules = arguments.callee.modules;
// if currently requiring module 'id', return partial exports
if ( require_stack.indexOf(id) >= 0 ) {
return modules[id].exports;
}
// if already required module 'id', return finished exports
if ( modules[id] && modules[id].exports ) {
return modules[id].exports;
}
// do the require of module 'id'
// - if currently requiring a module, push global exports/module objects into arguments.callee.modules
if ( require_stack.length > 0 ) {
var currently_requiring_id = require_stack[require_stack.length - 1];
modules[currently_requiring_id] = {
exports: exports,
module: module
};
}
require_stack.push(id);
exports = {};
module = {};
load('./' + id + '.js');
modules[id] = {
exports: exports,
module: module
};
require_stack.pop();
// restore last required modules' partial exports to the global space, or clear them
if ( require_stack.length > 0 ) {
var currently_requiring_id = require_stack[require_stack.length - 1];
exports = modules[currently_requiring_id].exports;
module = modules[currently_requiring_id].module;
} else {
exports = {};
module = {};
}
// return arguments.callee.modules[id].exports;
return arguments.callee.modules[id].exports;
}
@tbfly
Copy link

tbfly commented May 31, 2018

load('./' + id + '.js');

Where can find "load" function implement?
thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment