resolve.js
2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var path = require('path')
var fs = require('fs')
var existsSync = fs.existsSync || path.existsSync
var _nodePaths = (process.env.NODE_PATH ? process.env.NODE_PATH.split(':') : [])
_nodePaths.push(process.cwd())
module.exports = {
path: resolvePath,
_nodePaths:_nodePaths,
requireStatement: resolveRequireStatement
}
function resolvePath(searchPath, pathBase) {
if (searchPath[0] == '.') {
// relative path, e.g. require("./foo")
return _findModuleMain(path.resolve(pathBase, searchPath))
}
var searchParts = searchPath.split('/')
var componentName = searchParts[searchParts.length - 1]
var name = searchParts.shift()
var rest = searchParts.join('/')
// npm-style path, e.g. require("npm").
// Climb parent directories in search for "node_modules"
var modulePath = _findModuleMain(path.resolve(pathBase, 'node_modules', searchPath))
if (modulePath) { return modulePath }
if (pathBase != '/') {
// not yet at the root - keep climbing!
return resolvePath(searchPath, path.resolve(pathBase, '..'))
}
return ''
}
var _pathnameGroupingRegex = /require\s*\(['"]([\w\/\.-]*)['"]\)/
function resolveRequireStatement(requireStmnt, currentPath) {
var rawPath = requireStmnt.match(_pathnameGroupingRegex)[1]
var resolvedPath = resolvePath(rawPath, path.dirname(currentPath))
if (!resolvedPath && rawPath[0] != '.' && rawPath[0] != '/') {
for (var i=0; i<_nodePaths.length; i++) {
resolvedPath = _findModuleMain(path.resolve(_nodePaths[i], rawPath))
if (resolvedPath) { break }
}
}
if (!resolvedPath) { throw 'Could not resolve "'+rawPath+'" in "'+currentPath+'"' }
return resolvedPath
}
function _findModuleMain(absModulePath, tryFileName) {
var foundPath = ''
function attempt(aPath) {
if (foundPath) { return }
if (existsSync(aPath)) { foundPath = aPath }
}
attempt(absModulePath + '.js')
try {
var package = JSON.parse(fs.readFileSync(absModulePath + '/package.json').toString())
attempt(path.resolve(absModulePath, package.main+'.js'))
attempt(path.resolve(absModulePath, package.main))
} catch(e) {}
attempt(absModulePath + '/index.js')
if (tryFileName) { attempt(absModulePath + '/' + tryFileName + '.js') }
return foundPath
}