Wire up cache to Module._resolveFilename

This commit is contained in:
Kevin Sawicki 2014-10-07 15:02:15 -07:00 committed by Kevin Sawicki
parent 4da6513fb5
commit 5ad54bbe92
2 changed files with 15 additions and 6 deletions

View File

@ -54,7 +54,7 @@
"scoped-property-store": "^0.13.2", "scoped-property-store": "^0.13.2",
"scrollbar-style": "^1.0.2", "scrollbar-style": "^1.0.2",
"season": "^1.0.2", "season": "^1.0.2",
"semver": "1.1.4", "semver": "2.2.1",
"serializable": "^1", "serializable": "^1",
"space-pen": "3.8.0", "space-pen": "3.8.0",
"temp": "0.7.0", "temp": "0.7.0",

View File

@ -1,11 +1,10 @@
Module = require 'module' Module = require 'module'
path = require 'path' path = require 'path'
fs = require 'fs-plus' fs = require 'fs-plus'
semver = require 'semver'
nativeModules = process.binding('natives') nativeModules = process.binding('natives')
originalResolveFilename = Module._resolveFilename
loadDependencies = (modulePath, rootPath, rootMetadata, moduleCache) -> loadDependencies = (modulePath, rootPath, rootMetadata, moduleCache) ->
nodeModulesPath = path.join(modulePath, 'node_modules') nodeModulesPath = path.join(modulePath, 'node_modules')
for childPath in fs.listSync(nodeModulesPath) for childPath in fs.listSync(nodeModulesPath)
@ -58,8 +57,6 @@ loadFolderCompatibility = (modulePath, rootPath, rootMetadata, moduleCache) ->
loadFolderCompatibility(childPath, 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) -> exports.generateDependencies = (modulePath) ->
metadataPath = path.join(modulePath, 'package.json') metadataPath = path.join(modulePath, 'package.json')
metadata = JSON.parse(fs.readFileSync(metadataPath)) metadata = JSON.parse(fs.readFileSync(metadataPath))
@ -68,6 +65,7 @@ exports.generateDependencies = (modulePath) ->
version: 1 version: 1
dependencies: [] dependencies: []
folders: [] folders: []
loadDependencies(modulePath, modulePath, metadata, moduleCache) loadDependencies(modulePath, modulePath, metadata, moduleCache)
loadFolderCompatibility(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 relativePath[relativePath.length - 1] is '/'
return if fs.isAbsolute(relativePath) 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 undefined
@ -91,6 +99,7 @@ registered = false
exports.register = -> exports.register = ->
return if registered return if registered
originalResolveFilename = Module._resolveFilename
Module._resolveFilename = (relativePath, parentModule) -> Module._resolveFilename = (relativePath, parentModule) ->
resolvedPath = getCachedModulePath(relativePath, parentModule) resolvedPath = getCachedModulePath(relativePath, parentModule)
resolvedPath ? originalResolveFilename(relativePath, parentModule) resolvedPath ? originalResolveFilename(relativePath, parentModule)