mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2025-01-09 00:30:14 +03:00
Wire up cache to Module._resolveFilename
This commit is contained in:
parent
4da6513fb5
commit
5ad54bbe92
@ -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",
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user