@@ -5,18 +5,18 @@ export const CASE_INSENSITIVE = fs.existsSync(join(__dirname, 'reSOLVE.js'))
55
66const fileExistsCache = new Map ( )
77
8- function cachePath ( filepath , result ) {
9- fileExistsCache . set ( filepath , { result, lastSeen : Date . now ( ) } )
8+ function cachePath ( cacheKey , result ) {
9+ fileExistsCache . set ( cacheKey , { result, lastSeen : Date . now ( ) } )
1010}
1111
12- function checkCache ( filepath , { lifetime } ) {
13- if ( fileExistsCache . has ( filepath ) ) {
14- const { result, lastSeen } = fileExistsCache . get ( filepath )
12+ function checkCache ( cacheKey , { lifetime } ) {
13+ if ( fileExistsCache . has ( cacheKey ) ) {
14+ const { result, lastSeen } = fileExistsCache . get ( cacheKey )
1515 // check fresness
1616 if ( Date . now ( ) - lastSeen < ( lifetime * 1000 ) ) return result
1717 }
1818 // cache miss
19- return null
19+ return undefined
2020}
2121
2222// http://stackoverflow.com/a/27382838
@@ -43,6 +43,9 @@ function fileExistsWithCaseSync(filepath, cacheSettings) {
4343
4444export function relative ( modulePath , sourceFile , settings ) {
4545
46+ const sourceDir = dirname ( sourceFile )
47+ , cacheKey = sourceDir + hashObject ( settings ) + modulePath
48+
4649 const cacheSettings = Object . assign ( {
4750 lifetime : 30 , // seconds
4851 } , settings [ 'import/cache' ] )
@@ -52,6 +55,14 @@ export function relative(modulePath, sourceFile, settings) {
5255 cacheSettings . lifetime = Infinity
5356 }
5457
58+ const cachedPath = checkCache ( cacheKey , cacheSettings )
59+ if ( cachedPath !== undefined ) return cachedPath
60+
61+ function cache ( path ) {
62+ cachePath ( cacheKey , path )
63+ return path
64+ }
65+
5566 function withResolver ( resolver , config ) {
5667 try {
5768 const filePath = resolver . resolveImport ( modulePath , sourceFile , config )
@@ -75,9 +86,12 @@ export function relative(modulePath, sourceFile, settings) {
7586 const resolver = require ( `eslint-import-resolver-${ name } ` )
7687
7788 let fullPath = withResolver ( resolver , config )
78- if ( fullPath !== undefined ) return fullPath
89+ if ( fullPath !== undefined ) {
90+ return cache ( fullPath )
91+ }
7992 }
8093
94+ return cache ( undefined )
8195}
8296
8397function resolverReducer ( resolvers , map ) {
@@ -116,3 +130,11 @@ export default function resolve(p, context) {
116130 )
117131}
118132resolve . relative = relative
133+
134+
135+ import { createHash } from 'crypto'
136+ function hashObject ( object ) {
137+ const settingsShasum = createHash ( 'sha1' )
138+ settingsShasum . update ( JSON . stringify ( object ) )
139+ return settingsShasum . digest ( 'hex' )
140+ }
0 commit comments