Skip to content

Commit 43d874f

Browse files
author
淘小杰
committed
cache module with same version and same registry
related pr nodejs/node-v0.x-archive#8617
1 parent 72c8a74 commit 43d874f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/module.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const path = require('path');
1111
const internalModuleReadFile = process.binding('fs').internalModuleReadFile;
1212
const internalModuleStat = process.binding('fs').internalModuleStat;
1313

14-
14+
var moduleCache = {};
1515
// If obj.hasOwnProperty has been overridden, then calling
1616
// obj.hasOwnProperty(prop) will break.
1717
// See: https://github.com/joyent/node/issues/1707
@@ -76,7 +76,7 @@ function readPackage(requestPath) {
7676
}
7777

7878
try {
79-
var pkg = packageMainCache[requestPath] = JSON.parse(json).main;
79+
var pkg = packageMainCache[requestPath] = JSON.parse(json);
8080
} catch (e) {
8181
e.path = jsonPath;
8282
e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
@@ -88,9 +88,18 @@ function readPackage(requestPath) {
8888
function tryPackage(requestPath, exts) {
8989
var pkg = readPackage(requestPath);
9090

91-
if (!pkg) return false;
91+
if (!(pkg && pkg.main)) return false;
92+
93+
var resolved = pkg['_resolved'],
94+
main = pkg.main;
95+
96+
var filename;
97+
if(resolved) {
98+
filename = moduleCache[resolved] || (moduleCache[resolved] = path.resolve(requestPath, main));
99+
}else{
100+
filename = path.resolve(requestPath, main);
101+
}
92102

93-
var filename = path.resolve(requestPath, pkg);
94103
return tryFile(filename) || tryExtensions(filename, exts) ||
95104
tryExtensions(path.resolve(filename, 'index'), exts);
96105
}

0 commit comments

Comments
 (0)