@@ -71,9 +71,11 @@ export function fileExistsWithCaseSync(
7171 if ( filepath === null ) {
7272 return true
7373 }
74+
7475 if ( filepath . toLowerCase ( ) === process . cwd ( ) . toLowerCase ( ) && ! strict ) {
7576 return true
7677 }
78+
7779 const parsedPath = path . parse ( filepath )
7880 const dir = parsedPath . dir
7981
@@ -88,15 +90,36 @@ export function fileExistsWithCaseSync(
8890 } else {
8991 try {
9092 const filenames = fs . readdirSync ( dir )
91- result = filenames . includes ( parsedPath . base )
92- ? fileExistsWithCaseSync ( dir , cacheSettings , strict , false )
93- : ! leaf &&
93+ if ( filenames . includes ( parsedPath . base ) ) {
94+ result = fileExistsWithCaseSync ( dir , cacheSettings , strict , false )
95+ } else {
96+ const baseLowerCase = parsedPath . base . toLowerCase ( )
97+ const hasCaseInsensitiveMatch = filenames . some (
98+ p => p . toLowerCase ( ) === baseLowerCase ,
99+ )
100+
101+ const isMissing = ! hasCaseInsensitiveMatch
102+
103+ if ( isMissing && leaf ) {
104+ const queryIndex = filepath . lastIndexOf ( '?' )
105+ const hashIndex = filepath . lastIndexOf ( '#' )
106+ const index = Math . max ( queryIndex , hashIndex )
107+
108+ result =
109+ index > 0
110+ ? fileExistsWithCaseSync (
111+ filepath . slice ( 0 , index ) ,
112+ cacheSettings ,
113+ strict ,
114+ )
115+ : false
116+ } else {
94117 // We tolerate case-insensitive matches if there are no case-insensitive matches.
95118 // It'll fail anyway on the leaf node if the file truly doesn't exist (if it doesn't
96119 // fail it's that we're probably working with a virtual in-memory filesystem).
97- ! filenames . some (
98- p => p . toLowerCase ( ) === parsedPath . base . toLowerCase ( ) ,
99- )
120+ result = isMissing && ! leaf
121+ }
122+ }
100123 } catch ( error ) {
101124 if ( isDirectoryAccessError ( error ) ) {
102125 result = true
0 commit comments