From 43d874f404373e8c057a5db7d18ed8c9a540a68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B7=98=E5=B0=8F=E6=9D=B0?= Date: Fri, 9 Oct 2015 17:05:35 +0800 Subject: [PATCH] cache module with same version and same registry related pr https://github.com/nodejs/node-v0.x-archive/pull/8617 --- lib/module.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/module.js b/lib/module.js index ef0fad209ff9c8..a7f92ea8ef3ba4 100644 --- a/lib/module.js +++ b/lib/module.js @@ -11,7 +11,7 @@ const path = require('path'); const internalModuleReadFile = process.binding('fs').internalModuleReadFile; const internalModuleStat = process.binding('fs').internalModuleStat; - +var moduleCache = {}; // If obj.hasOwnProperty has been overridden, then calling // obj.hasOwnProperty(prop) will break. // See: https://github.com/joyent/node/issues/1707 @@ -76,7 +76,7 @@ function readPackage(requestPath) { } try { - var pkg = packageMainCache[requestPath] = JSON.parse(json).main; + var pkg = packageMainCache[requestPath] = JSON.parse(json); } catch (e) { e.path = jsonPath; e.message = 'Error parsing ' + jsonPath + ': ' + e.message; @@ -88,9 +88,18 @@ function readPackage(requestPath) { function tryPackage(requestPath, exts) { var pkg = readPackage(requestPath); - if (!pkg) return false; + if (!(pkg && pkg.main)) return false; + + var resolved = pkg['_resolved'], + main = pkg.main; + + var filename; + if(resolved) { + filename = moduleCache[resolved] || (moduleCache[resolved] = path.resolve(requestPath, main)); + }else{ + filename = path.resolve(requestPath, main); + } - var filename = path.resolve(requestPath, pkg); return tryFile(filename) || tryExtensions(filename, exts) || tryExtensions(path.resolve(filename, 'index'), exts); }