From 5ad54bbe925212268d1ee62bbbf1fb666d3f84b4 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Oct 2014 15:02:15 -0700 Subject: [PATCH] Wire up cache to Module._resolveFilename --- package.json | 2 +- src/module-cache.coffee | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 1fde259c4..fed1bd296 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "scoped-property-store": "^0.13.2", "scrollbar-style": "^1.0.2", "season": "^1.0.2", - "semver": "1.1.4", + "semver": "2.2.1", "serializable": "^1", "space-pen": "3.8.0", "temp": "0.7.0", diff --git a/src/module-cache.coffee b/src/module-cache.coffee index 6fff2ace2..7e3bacfe7 100644 --- a/src/module-cache.coffee +++ b/src/module-cache.coffee @@ -1,11 +1,10 @@ Module = require 'module' path = require 'path' fs = require 'fs-plus' +semver = require 'semver' nativeModules = process.binding('natives') -originalResolveFilename = Module._resolveFilename - loadDependencies = (modulePath, rootPath, rootMetadata, moduleCache) -> nodeModulesPath = path.join(modulePath, 'node_modules') for childPath in fs.listSync(nodeModulesPath) @@ -58,8 +57,6 @@ loadFolderCompatibility = (modulePath, rootPath, rootMetadata, moduleCache) -> loadFolderCompatibility(childPath, rootPath, rootMetadata, moduleCache) -# Precompute versions of all modules in node_modules -# Precompute the version each file is compatible exports.generateDependencies = (modulePath) -> metadataPath = path.join(modulePath, 'package.json') metadata = JSON.parse(fs.readFileSync(metadataPath)) @@ -68,6 +65,7 @@ exports.generateDependencies = (modulePath) -> version: 1 dependencies: [] folders: [] + loadDependencies(modulePath, modulePath, metadata, moduleCache) loadFolderCompatibility(modulePath, modulePath, metadata, moduleCache) @@ -83,7 +81,17 @@ getCachedModulePath = (relativePath, parentModule) -> return if relativePath[relativePath.length - 1] is '/' return if fs.isAbsolute(relativePath) - console.log "looking up #{relativePath} from #{parentModule.id}" + folderPath = path.dirname(parentModule.id) + + dependency = folders[folderPath]?[relativePath] + return unless dependency? + + candidates = dependencies[relativePath] + return unless candidates? + + for version, resolvedPath of candidates + if Module._cache[resolvedPath] and semver.satisfies(version, dependency) + return resolvedPath undefined @@ -91,6 +99,7 @@ registered = false exports.register = -> return if registered + originalResolveFilename = Module._resolveFilename Module._resolveFilename = (relativePath, parentModule) -> resolvedPath = getCachedModulePath(relativePath, parentModule) resolvedPath ? originalResolveFilename(relativePath, parentModule)