@@ -8,6 +8,37 @@ const ES6_BROWSER_EMPTY = resolve( __dirname, '../src/empty.js' );
8
8
const CONSOLE_WARN = ( ...args ) => console . warn ( ...args ) ; // eslint-disable-line no-console
9
9
const exts = [ '.js' , '.json' , '.node' ] ;
10
10
11
+ let readFileCache = { } ;
12
+ const readFileAsync = file => new Promise ( ( fulfil , reject ) => fs . readFile ( file , ( err , contents ) => err ? reject ( err ) : fulfil ( contents ) ) ) ;
13
+ const statAsync = file => new Promise ( ( fulfil , reject ) => fs . stat ( file , ( err , contents ) => err ? reject ( err ) : fulfil ( contents ) ) ) ;
14
+ function cachedReadFile ( file , cb ) {
15
+ if ( file in readFileCache === false ) {
16
+ readFileCache [ file ] = readFileAsync ( file ) . catch ( err => {
17
+ delete readFileCache [ file ] ;
18
+ throw err ;
19
+ } ) ;
20
+ }
21
+ readFileCache [ file ] . then ( contents => cb ( null , contents ) , cb ) ;
22
+ }
23
+
24
+ let isFileCache = { } ;
25
+ function cachedIsFile ( file , cb ) {
26
+ if ( file in isFileCache === false ) {
27
+ isFileCache [ file ] = statAsync ( file )
28
+ . then (
29
+ stat => stat . isFile ( ) ,
30
+ err => {
31
+ if ( err . code == 'ENOENT' ) return false ;
32
+ throw err ;
33
+ }
34
+ ) . catch ( err => {
35
+ delete isFileCache [ file ] ;
36
+ throw err ;
37
+ } ) ;
38
+ }
39
+ isFileCache [ file ] . then ( contents => cb ( null , contents ) , cb ) ;
40
+ }
41
+
11
42
export default function nodeResolve ( options = { } ) {
12
43
const useModule = options . module !== false ;
13
44
const useMain = options . main !== false ;
@@ -31,6 +62,11 @@ export default function nodeResolve ( options = {} ) {
31
62
return {
32
63
name : 'node-resolve' ,
33
64
65
+ write ( ) {
66
+ isFileCache = { }
67
+ readFileCache = { }
68
+ } ,
69
+
34
70
resolveId ( importee , importer ) {
35
71
if ( / \0 / . test ( importee ) ) return null ; // ignore IDs with null character, these belong to other plugins
36
72
@@ -99,6 +135,8 @@ export default function nodeResolve ( options = {} ) {
99
135
}
100
136
return pkg ;
101
137
} ,
138
+ readFile : cachedReadFile ,
139
+ isFile : cachedIsFile ,
102
140
extensions : options . extensions
103
141
} , customResolveOptions ) ,
104
142
( err , resolved ) => {
0 commit comments