diff --git a/packages/kit/src/utils/resolve.ts b/packages/kit/src/utils/resolve.ts index a38baea35e..bef872216f 100644 --- a/packages/kit/src/utils/resolve.ts +++ b/packages/kit/src/utils/resolve.ts @@ -1,5 +1,5 @@ -import { existsSync, lstatSync } from 'fs' -import { resolve, join } from 'upath' +import { existsSync, lstatSync, readdirSync } from 'fs' +import { basename, dirname, resolve, join } from 'upath' import globby from 'globby' export interface ResolveOptions { @@ -20,7 +20,7 @@ export interface ResolveOptions { function resolvePath (path: string, opts: ResolveOptions = {}) { // Fast return if the path exists - if (existsSync(path)) { + if (existsSyncSensitive(path)) { return path } @@ -34,9 +34,11 @@ function resolvePath (path: string, opts: ResolveOptions = {}) { // Resolve relative to base or cwd resolvedPath = resolve(opts.base || '.', resolvedPath) + const resolvedPathFiles = readdirSync(dirname(resolvedPath)) + // Check if resolvedPath is a file let isDirectory = false - if (existsSync(resolvedPath)) { + if (existsSyncSensitive(resolvedPath, resolvedPathFiles)) { isDirectory = lstatSync(resolvedPath).isDirectory() if (!isDirectory) { return resolvedPath @@ -47,12 +49,12 @@ function resolvePath (path: string, opts: ResolveOptions = {}) { for (const ext of opts.extensions) { // resolvedPath.[ext] const resolvedPathwithExt = resolvedPath + ext - if (!isDirectory && existsSync(resolvedPathwithExt)) { + if (!isDirectory && existsSyncSensitive(resolvedPathwithExt, resolvedPathFiles)) { return resolvedPathwithExt } // resolvedPath/index.[ext] const resolvedPathwithIndex = join(resolvedPath, 'index' + ext) - if (isDirectory && existsSync(resolvedPathwithIndex)) { + if (isDirectory && existsSyncSensitive(resolvedPathwithIndex)) { return resolvedPathwithIndex } } @@ -66,6 +68,12 @@ function resolvePath (path: string, opts: ResolveOptions = {}) { throw new Error(`Cannot resolve "${path}" from "${resolvedPath}"`) } +function existsSyncSensitive (path: string, files?: string[]) { + if (!existsSync(path)) { return false } + const _files = files || readdirSync(dirname(path)) + return _files.includes(basename(path)) +} + /** * Return a path with any relevant aliases resolved. *