Skip to content

Commit

Permalink
cache instance(s) of resolve function
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Apr 30, 2018
1 parent 8c9c3b8 commit 198a9a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var findRoot = require('find-root')
, path = require('path')
, get = require('lodash.get')
, _ = require('lodash')
, find = require('array-find')
, interpret = require('interpret')
// not available on 0.10.x
Expand All @@ -11,6 +11,8 @@ var findRoot = require('find-root')
, semver = require('semver')
, has = require('has')

var get = _.get

var log = require('debug')('eslint-plugin-import:resolver:webpack')

exports.interfaceVersion = 2
Expand Down Expand Up @@ -105,7 +107,8 @@ exports.resolve = function (source, file, settings) {
}

// otherwise, resolve "normally"
var resolveSync = createResolveSync(configPath, webpackConfig)
var resolveSync = getResolveSync(configPath, webpackConfig)

try {
return { found: true, path: resolveSync(path.dirname(file), source) }
} catch (err) {
Expand All @@ -114,6 +117,24 @@ exports.resolve = function (source, file, settings) {
}
}

var MAX_CACHE = 10
var _cache = []
function getResolveSync(configPath, webpackConfig) {
var cacheKey = { configPath: configPath, webpackConfig: webpackConfig }
var cached = find(_cache, function (entry) { return _.isEqual(entry.key, cacheKey) })
if (!cached) {
cached = {
key: cacheKey,
value: createResolveSync(configPath, webpackConfig)
}
// put in front and pop last item
if (_cache.unshift(cached) > MAX_CACHE) {
_cache.pop()
}
}
return cached.value
}

function createResolveSync(configPath, webpackConfig) {
var webpackRequire
, basedir = null
Expand Down
2 changes: 1 addition & 1 deletion resolvers/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"has": "^1.0.1",
"interpret": "^1.0.0",
"is-absolute": "^0.2.3",
"lodash.get": "^4.4.2",
"lodash": "^4.17.4",
"node-libs-browser": "^1.0.0 || ^2.0.0",
"resolve": "^1.4.0",
"semver": "^5.3.0"
Expand Down

0 comments on commit 198a9a8

Please sign in to comment.