Skip to content

Instantly share code, notes, and snippets.

@joyeecheung
Created August 28, 2014 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyeecheung/74262a250b3e880c7fd4 to your computer and use it in GitHub Desktop.
Save joyeecheung/74262a250b3e880c7fd4 to your computer and use it in GitHub Desktop.
A simplified version of the require.resovle() from node.js
var path = require('path');
// require(X) from module at path Y
function resolve(x, y) {
// 1. If X is a core module,
// a. return the core module
// b. STOP
if (isCoreModule(x)) {
console.log(x + " is a core module");
}
// 2. If X begins with './' or '/' or '../'
// a. LOAD_AS_FILE(Y + X)
// b. LOAD_AS_DIRECTORY(Y + X)
var pathPrefixes = ['./', '/', '../'];
pathPrefixes.forEach(function(prefix) {
if (x.indexOf(prefix) == 0) {
loadAsFile(y + x);
loadAsDirectory(y + x);
}
});
// 3. LOAD_NODE_MODULES(X, dirname(Y))
loadNodeModules(x, path.dirname(y));
// addition: load from global env
console.log('---------------------------------------');
console.log('\nfor each $PATH in $NODE_PATH\n');
loadAsDirectory('$PATH');
loadAsDirectory('$HOME/.node_modules');
loadAsDirectory('$HOME/.node_libraries');
loadAsDirectory('$PREFIX/lib/node');
// 4. THROW "not found"
}
// LOAD_AS_FILE(X)
function loadAsFile(x) {
console.log('---------------------------------------');
// 1. If X is a file, load X as JavaScript text. STOP
console.log("\tcheck " + x);
// 2. If X.js is a file, load X.js as JavaScript text. STOP
console.log("\tcheck " + x + ".js");
// 3. If X.json is a file, parse X.json to a JavaScript Object. STOP
console.log("\tcheck " + x + ".json");
// 4. If X.node is a file, load X.node as binary addon. STOP
console.log("\tcheck " + x + ".node");
}
// LOAD_AS_DIRECTORY(X)
function loadAsDirectory(x) {
// 1. If X/package.json is a file,
// a. Parse X/package.json, and look for "main" field.
// b. let M = X + (json main field)
// c. LOAD_AS_FILE(M)
console.log('---------------------------------------');
console.log("if " + x + "/package.json exists");
console.log("\tcheck " + x + "/package.json[main]");
// 2. If X/index.js is a file, load X/index.js as JavaScript text. STOP
console.log('---------------------------------------');
console.log("if " + x + "/index.js exists");
console.log("\tcheck " + x + "/index.js");
// 3. If X/index.node is a file, load X/index.node as binary addon. STOP
console.log('---------------------------------------');
console.log("if " + x + "/index.node exists");
console.log("\tcheck " + x + "/index.node");
}
// LOAD_NODE_MODULES(X, START)
function loadNodeModules(x, start) {
// 1. let DIRS=NODE_MODULES_PATHS(START)
var dirs = nodeModulesPaths(start);
// 2. for each DIR in DIRS:
dirs.forEach(function(dir) {
// a. LOAD_AS_FILE(DIR/X)
loadAsFile(dir + '/' + x);
// b. LOAD_AS_DIRECTORY(DIR/X)
loadAsDirectory(dir + '/' + x);
});
}
// NODE_MODULES_PATHS(START)
function nodeModulesPaths(start) {
// 1. let PARTS = path split(START)
var parts = start.split('/');
// 2. let ROOT = index of first instance of "node_modules" in PARTS, or 0
var root = parts[0];
// 3. let I = count of PARTS - 1
var i = parts.length - 1
// 4. let DIRS = []
dirs = []
// 5. while I > ROOT,
while (i >= 0) {
// a. if PARTS[I] = "node_modules" CONTINUE
if (parts[i] == 'node_modules') {
continue;
}
// c. DIR = path join(PARTS[0 .. I] + "node_modules")
var dirParts = parts.slice(0, i + 1);
dirParts.push('node_modules');
var dir = dirParts.join('/');
// b. DIRS = DIRS + DIR
dirs.push(dir);
// c. let I = I - 1
i -= 1;
}
// 6. return DIRS
return dirs;
}
function isCoreModule(x) {
var core = ["assert", "buffer", "child_process",
"cluster", "console", "constants", "crypto",
"dgram", "dns", "domain", "events", "freelist",
"fs", "http", "https", "module", "net", "os", "path",
"punycode", "querystring", "readline", "repl", "smalloc",
"stream", "string_decoder", "sys", "timers", "tls", "tracing",
"tty", "url", "util", "vm", "zlib"
];
for (var i = 0; i < core.length; ++i) {
if (x == core[i]) {
return true;
}
}
return false;
}
resolve('othermodule','/home/somebody/workspace/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment