fix(resolver): resolve dir if no index found #4568 (#4569)

[skip release]
This commit is contained in:
Mark 2018-12-17 15:10:39 +00:00 committed by Pooya Parsa
parent 2b46d3e5f7
commit 85b53593fa
1 changed files with 15 additions and 4 deletions

View File

@ -69,22 +69,33 @@ export default class Resolver {
resolvedPath = path
}
let isDirectory
// Check if resolvedPath exits and is not a directory
if (fs.existsSync(resolvedPath) && !fs.lstatSync(resolvedPath).isDirectory()) {
return resolvedPath
if (fs.existsSync(resolvedPath)) {
isDirectory = fs.lstatSync(resolvedPath).isDirectory()
if (!isDirectory) {
return resolvedPath
}
}
// Check if any resolvedPath.[ext] or resolvedPath/index.[ext] exists
for (const ext of this.options.extensions) {
if (fs.existsSync(resolvedPath + '.' + ext)) {
if (!isDirectory && fs.existsSync(resolvedPath + '.' + ext)) {
return resolvedPath + '.' + ext
}
if (fs.existsSync(resolvedPath + '/index.' + ext)) {
if (isDirectory && fs.existsSync(resolvedPath + '/index.' + ext)) {
return resolvedPath + '/index.' + ext
}
}
// If there's no index.[ext] we just return the directory path
if (isDirectory) {
return resolvedPath
}
// Give up
throw new Error(`Cannot resolve "${path}" from "${resolvedPath}"`)
}