@@ -9,49 +9,29 @@ const ES6_BROWSER_EMPTY = resolve( __dirname, '../src/empty.js' );
99// which deploy both ESM .mjs and CommonJS .js files as ESM.
1010const DEFAULT_EXTS = [ '.mjs' , '.js' , '.json' , '.node' ] ;
1111
12- let readFileCache = { } ;
1312const readFileAsync = file => new Promise ( ( fulfil , reject ) => fs . readFile ( file , ( err , contents ) => err ? reject ( err ) : fulfil ( contents ) ) ) ;
1413const statAsync = file => new Promise ( ( fulfil , reject ) => fs . stat ( file , ( err , contents ) => err ? reject ( err ) : fulfil ( contents ) ) ) ;
15- function cachedReadFile ( file , cb ) {
16- if ( file in readFileCache === false ) {
17- readFileCache [ file ] = readFileAsync ( file ) . catch ( err => {
18- delete readFileCache [ file ] ;
19- throw err ;
20- } ) ;
21- }
22- readFileCache [ file ] . then ( contents => cb ( null , contents ) , cb ) ;
23- }
24-
25- let isFileCache = { } ;
26- function cachedIsFile ( file , cb ) {
27- if ( file in isFileCache === false ) {
28- isFileCache [ file ] = statAsync ( file )
29- . then (
30- stat => stat . isFile ( ) ,
31- err => {
32- if ( err . code === 'ENOENT' ) return false ;
33- delete isFileCache [ file ] ;
34- throw err ;
35- } ) ;
36- }
37- isFileCache [ file ] . then ( contents => cb ( null , contents ) , cb ) ;
38- }
39-
40- let isDirCache = { } ;
41- function cachedIsDir ( dir , cb ) {
42- if ( dir in isDirCache === false ) {
43- isDirCache [ dir ] = statAsync ( dir )
44- . then (
45- stat => stat . isDirectory ( ) ,
46- err => {
47- if ( err . code === 'ENOENT' ) return false ;
48- delete isDirCache [ dir ] ;
49- throw err ;
50- } ) ;
51- }
52- isDirCache [ dir ] . then ( contents => cb ( null , contents ) , cb ) ;
53- }
54-
14+ const cache = fn => {
15+ let cache = Object . create ( null ) ;
16+ const wrapped = ( s , cb = false ) => {
17+ if ( s in cache === false ) {
18+ cache [ s ] = fn ( s ) . catch ( err => {
19+ delete cache [ s ] ;
20+ throw err ;
21+ } ) ;
22+ }
23+ return cb ? cache [ s ] . then ( v => cb ( null , v ) , cb ) : cache [ s ] ;
24+ } ;
25+ wrapped . clear = ( ) => { cache = { } ; } ;
26+ return wrapped ;
27+ } ;
28+ const ignoreENOENT = err => {
29+ if ( err . code === 'ENOENT' ) return false ;
30+ throw err ;
31+ } ;
32+ const readFileCached = cache ( readFileAsync ) ;
33+ const isDirCached = cache ( file => statAsync ( file ) . then ( stat => stat . isDirectory ( ) , ignoreENOENT ) ) ;
34+ const isFileCached = cache ( file => statAsync ( file ) . then ( stat => stat . isFile ( ) , ignoreENOENT ) ) ;
5535
5636function getMainFields ( options ) {
5737 let mainFields ;
@@ -115,8 +95,9 @@ export default function nodeResolve ( options = {} ) {
11595 } ,
11696
11797 generateBundle ( ) {
118- isFileCache = { } ;
119- readFileCache = { } ;
98+ readFileCached . clear ( ) ;
99+ isFileCached . clear ( ) ;
100+ isDirCached . clear ( ) ;
120101 } ,
121102
122103 resolveId ( importee , importer ) {
@@ -196,9 +177,9 @@ export default function nodeResolve ( options = {} ) {
196177 }
197178 return pkg ;
198179 } ,
199- readFile : cachedReadFile ,
200- isFile : cachedIsFile ,
201- isDir : cachedIsDir ,
180+ readFile : readFileCached ,
181+ isFile : isFileCached ,
182+ isDirectory : isDirCached ,
202183 extensions : extensions
203184 } ;
204185
0 commit comments