Last active
February 23, 2016 12:35
-
-
Save dongyuwei/0be0f41d7e57781a84f8 to your computer and use it in GitHub Desktop.
an tiny commonjs module loader
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
var fs = require('fs'); | |
function require2(url){ | |
var source = fs.readFileSync(url,'utf-8'); | |
var code = new Function('exports, module', source); | |
var exports = {}; | |
var module = { | |
exports: exports | |
}; | |
code(exports, module); | |
return module.exports; | |
} | |
global.require2 = require2; | |
var foo = require2('./foo.js'); | |
console.log(foo); | |
foo.hi(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cat foo.js
cat bar.js