-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webpack resolver: cache instance(s) of resolve function #1091
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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 | ||
|
@@ -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) { | ||
|
@@ -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) }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a reason to use an array here, rather than a Set, given that eslint only works on node 4+? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could arguably still use the latest webpack resolver with much older versions of ESLint and this plugin (at work I only just recently upgraded us from ESLint 2 and plugin v1). I don't think it is critical to maintain the ES5, at this point, but I also didn't want to cross that bridge today if there wasn't a compelling reason. was easy enough to write this PR in ES5. |
||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i believe this should actually be
get = require('lodash/get')
for maximum tree-shake-ability, and for future compat with lodash 5.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooo cool, thanks. I have yet to get familiar with digging things directly out of lodash