Browse Source

Add require.resolve.

Also, hack the repl so that it works as expected there, too.
v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
226eff5104
  1. 2
      lib/repl.js
  2. 48
      src/node.js
  3. 14
      test/simple/test-require-resolve.js

2
lib/repl.js

@ -31,6 +31,8 @@ function cwdRequire (id) {
Object.keys(require).forEach(function (k) { Object.keys(require).forEach(function (k) {
cwdRequire[k] = require[k]; cwdRequire[k] = require[k];
}); });
// hack for require.resolve("./relative") to work properly.
module.filename = process.cwd() + "/repl";
function resetContext() { function resetContext() {
context = Script.createContext(); context = Script.createContext();

48
src/node.js

@ -164,10 +164,8 @@ var module = (function () {
}; };
} }
function findModulePath (id, dirs) { function findModulePath (request, paths) {
process.assert(Array.isArray(dirs)); var nextLoc = traverser(request, request.charAt(0) === '/' ? [''] : paths);
var nextLoc = traverser(id, id.charAt(0) === '/' ? [''] : dirs);
var fs = requireNative('fs'); var fs = requireNative('fs');
@ -181,7 +179,10 @@ var module = (function () {
// sync - no i/o performed // sync - no i/o performed
function resolveModulePath(request, parent) { function resolveModuleLookupPaths (request, parent) {
if (natives[request]) return [request, []];
var start = request.substring(0, 2); var start = request.substring(0, 2);
if (start !== "./" && start !== "..") { if (start !== "./" && start !== "..") {
return [request, modulePaths]; return [request, modulePaths];
@ -207,18 +208,33 @@ var module = (function () {
function loadModule (request, parent) { function loadModule (request, parent) {
debug("loadModule REQUEST " + (request) + " parent: " + parent.id); debug("loadModule REQUEST " + (request) + " parent: " + parent.id);
var resolved = resolveModuleFilename(request, parent);
var id = resolved[0];
var filename = resolved[1];
// With natives id === request // With natives id === request
// We deal with these first // We deal with these first
var cachedNative = internalModuleCache[request]; var cachedNative = internalModuleCache[id];
if (cachedNative) { if (cachedNative) {
return cachedNative.exports; return cachedNative.exports;
} }
if (natives[request]) { if (natives[id]) {
debug('load native module ' + request); debug('load native module ' + request);
return loadNative(request).exports; return loadNative(id).exports;
} }
var resolvedModule = resolveModulePath(request, parent), var cachedModule = moduleCache[filename];
if (cachedModule) return cachedModule.exports;
var module = new Module(id, parent);
moduleCache[filename] = module;
module.load(filename);
return module.exports;
};
function resolveModuleFilename (request, parent) {
if (natives[request]) return [request, request];
var resolvedModule = resolveModuleLookupPaths(request, parent),
id = resolvedModule[0], id = resolvedModule[0],
paths = resolvedModule[1]; paths = resolvedModule[1];
@ -228,15 +244,8 @@ var module = (function () {
if (!filename) { if (!filename) {
throw new Error("Cannot find module '" + request + "'"); throw new Error("Cannot find module '" + request + "'");
} }
return [id, filename];
var cachedModule = moduleCache[filename]; }
if (cachedModule) return cachedModule.exports;
var module = new Module(id, parent);
moduleCache[filename] = module;
module.load(filename);
return module.exports;
};
Module.prototype.load = function (filename) { Module.prototype.load = function (filename) {
@ -262,6 +271,9 @@ var module = (function () {
return loadModule(path, self); return loadModule(path, self);
} }
require.resolve = function (request) {
return resolveModuleFilename(request, self)[1];
}
require.paths = modulePaths; require.paths = modulePaths;
require.main = process.mainModule; require.main = process.mainModule;
// Enable support to add extra extension types // Enable support to add extra extension types

14
test/simple/test-require-resolve.js

@ -0,0 +1,14 @@
var common = require("../common");
var fixturesDir = common.fixturesDir;
var assert = common.assert;
var path = require("path");
assert.equal(path.join(__dirname, "../fixtures/a.js"),
require.resolve("../fixtures/a"));
assert.equal(path.join(fixturesDir, "a.js"),
require.resolve(path.join(fixturesDir, "a")));
assert.equal(path.join(fixturesDir, "nested-index", "one", "index.js"),
require.resolve("../fixtures/nested-index/one"));
assert.equal("path", require.resolve("path"));
console.log("ok");
Loading…
Cancel
Save