@@ -11,7 +11,7 @@ import type {Wakeable, Thenable} from 'shared/ReactTypes';
11
11
12
12
import { unstable_getCacheForType } from 'react' ;
13
13
import * as fs from 'fs/promises' ;
14
- import { resolve } from 'path' ;
14
+ import { isAbsolute , normalize } from 'path' ;
15
15
16
16
const Pending = 0 ;
17
17
const Resolved = 1 ;
@@ -70,6 +70,27 @@ function readResult<T>(result: Result<T>): T {
70
70
}
71
71
}
72
72
73
+ // We don't want to normalize every path ourselves in production.
74
+ // However, relative or non-normalized paths will lead to cache misses.
75
+ // So we encourage the developer to fix it in DEV and normalize on their end.
76
+ function checkPathInDev ( path : string ) {
77
+ if ( __DEV__ ) {
78
+ if ( ! isAbsolute ( path ) ) {
79
+ console . error (
80
+ 'The provided path was not absolute: "%s". ' +
81
+ 'Convert it to an absolute path first.' ,
82
+ path ,
83
+ ) ;
84
+ } else if (path !== normalize(path)) {
85
+ console . error (
86
+ 'The provided path was not normalized: "%s". ' +
87
+ 'Convert it to a normalized path first.' ,
88
+ path ,
89
+ ) ;
90
+ }
91
+ }
92
+ }
93
+
73
94
function createReadFileCache ( ) : Map < string , Result < Buffer > > {
74
95
return new Map ( ) ;
75
96
}
@@ -86,12 +107,12 @@ export function readFile(
86
107
} ,
87
108
): string | Buffer {
88
109
const map = unstable_getCacheForType ( createReadFileCache ) ;
89
- const resolvedPath = resolve ( path ) ;
90
- let entry = map . get ( resolvedPath ) ;
110
+ checkPathInDev ( path ) ;
111
+ let entry = map . get ( path ) ;
91
112
if ( ! entry ) {
92
- const thenable = fs . readFile ( resolvedPath ) ;
113
+ const thenable = fs . readFile ( path ) ;
93
114
entry = toResult ( thenable ) ;
94
- map . set ( resolvedPath , entry ) ;
115
+ map . set ( path , entry ) ;
95
116
}
96
117
const result : Buffer = readResult ( entry ) ;
97
118
if ( ! options ) {
0 commit comments