Skip to content

Instantly share code, notes, and snippets.

@KevinOfNeu
Created July 8, 2015 06:47
Show Gist options
  • Select an option

  • Save KevinOfNeu/5c39e47fe854bc6b8d02 to your computer and use it in GitHub Desktop.

Select an option

Save KevinOfNeu/5c39e47fe854bc6b8d02 to your computer and use it in GitHub Desktop.
tiny require and examples!
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
this funny!
</body>
<script>
function require(p){
var path = require.resolve(p);
var mod = require.modules[path];
if (!mod) throw new Error('failed to require "' + p + '"');
if (!mod.exports) {
mod.exports = {};
mod.call(mod.exports, mod, mod.exports, require.relative(path));
}
return mod.exports;
}
require.modules = {};
require.resolve = function (path){
var orig = path;
var reg = path + '.js';
var index = path + '/index.js';
return require.modules[reg] && reg
|| require.modules[index] && index
|| orig;
};
require.register = function (path, fn){
require.modules[path] = fn;
};
require.relative = function (parent) {
return function(p){
if ('.' != p.charAt(0)) return require(p);
var path = parent.split('/');
var segs = p.split('/');
path.pop();
for (var i = 0; i < segs.length; i++) {
var seg = segs[i];
if ('..' == seg) path.pop();
else if ('.' != seg) path.push(seg);
}
return require(path.join('/'));
};
};
require.register("user.login", function(module, exports, require){
module.exports = {
doLogin:function(){
console.log("Im in doLogin");
},
reLogin:function(){
console.log("Im reLogin");
}
};
});
var logutil = require("user.login");
console.log(logutil);
logutil.doLogin();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment