Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexanderGugel/bdd03242b16944e27b162cb9a4f50560 to your computer and use it in GitHub Desktop.
Save alexanderGugel/bdd03242b16944e27b162cb9a4f50560 to your computer and use it in GitHub Desktop.
diff --git a/lib/module.js b/lib/module.js
index fb233d6..a2f6faa 100644
--- a/lib/module.js
+++ b/lib/module.js
@@ -103,20 +103,35 @@ function tryPackage(requestPath, exts, isMain) {
if (!pkg) return false;
var filename = path.resolve(requestPath, pkg);
+ var _filename = path.resolve(requestPath, '_' + pkg);
+
+ // if the desired package exists as a prefixed version, use its real path
+ // otherwise, use the original name and treat it as a regular dependency
+ return readPackagePath(_filename, exts, true) ||
+ readPackagePath(filename, exts, isMain);
+}
+
+function readPackagePath(filename, exts, isMain) {
return tryFile(filename, isMain) ||
tryExtensions(filename, exts, isMain) ||
tryExtensions(path.resolve(filename, 'index'), exts, isMain);
}
+function readFilePath(requestPath, isMain) {
+ if (isMain) {
+ return fs.realpathSync(requestPath);
+ }
+ return path.resolve(requestPath);
+}
+
// check if the file exists and is not a directory
// resolve to the absolute realpath if running main module,
// otherwise resolve to absolute while keeping symlinks intact.
function tryFile(requestPath, isMain) {
const rc = stat(requestPath);
- if (isMain) {
- return rc === 0 && fs.realpathSync(requestPath);
+ if (rc === 0) {
+ return readFilePath(requestPath, isMain);
}
- return rc === 0 && path.resolve(requestPath);
}
// given a path check a the file exists with any of the set extensions
@@ -159,11 +174,7 @@ Module._findPath = function(request, paths, isMain) {
if (!trailingSlash) {
const rc = stat(basePath);
if (rc === 0) { // File.
- if (!isMain) {
- filename = path.resolve(basePath);
- } else {
- filename = fs.realpathSync(basePath);
- }
+ filename = readFilePath(basePath, isMain);
} else if (rc === 1) { // Directory.
if (exts === undefined)
exts = Object.keys(Module._extensions);
@@ -427,6 +438,7 @@ Module._resolveFilename = function(request, parent, isMain) {
}
var resolvedModule = Module._resolveLookupPaths(request, parent);
+
var id = resolvedModule[0];
var paths = resolvedModule[1];
@fengmk2
Copy link

fengmk2 commented Apr 28, 2016

function readFilePath(requestPath, isMain) { rename isMain to needRealpath or another true expression name will be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment