File: index.js

package info (click to toggle)
node-require-relative 0.8.7-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 72 kB
  • sloc: javascript: 25; sh: 2; makefile: 2
file content (33 lines) | stat: -rw-r--r-- 784 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
relative require
*/'use strict';

var path = require('path');
var Module = require('module');

var modules = {};

var getModule = function(dir) {
  var rootPath = dir ? path.resolve(dir) : process.cwd();
  var rootName = path.join(rootPath, '@root');
  var root = modules[rootName];
  if (!root) {
    root = new Module(rootName);
    root.filename = rootName;
    root.paths = Module._nodeModulePaths(rootPath);
    modules[rootName] = root;
  }
  return root;
};

var requireRelative = function(requested, relativeTo) {
  var root = getModule(relativeTo);
  return root.require(requested);
};

requireRelative.resolve = function(requested, relativeTo) {
  var root = getModule(relativeTo);
  return Module._resolveFilename(requested, root);
};

module.exports = requireRelative;