From 198a9a89d8af385655666c139b6207558d7a578c Mon Sep 17 00:00:00 2001 From: Ben Mosher Date: Mon, 30 Apr 2018 07:09:39 -0400 Subject: [PATCH 1/4] cache instance(s) of resolve function --- resolvers/webpack/index.js | 25 +++++++++++++++++++++++-- resolvers/webpack/package.json | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index e1ffe70d1..9e2a582fb 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -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) }) + 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 diff --git a/resolvers/webpack/package.json b/resolvers/webpack/package.json index 9cbce0d47..7b3191545 100644 --- a/resolvers/webpack/package.json +++ b/resolvers/webpack/package.json @@ -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" From 16a48611b84b371f24c5b5a58841f7457d17fc84 Mon Sep 17 00:00:00 2001 From: Ben Mosher Date: Tue, 1 May 2018 07:09:39 -0400 Subject: [PATCH 2/4] better lodash requires --- resolvers/webpack/index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index 9e2a582fb..e2ba3a471 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -1,6 +1,7 @@ var findRoot = require('find-root') , path = require('path') - , _ = require('lodash') + , get = require('lodash/get') + , isEqual = require('lodash/isEqual') , find = require('array-find') , interpret = require('interpret') // not available on 0.10.x @@ -11,8 +12,6 @@ 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 @@ -121,7 +120,7 @@ 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) }) + var cached = find(_cache, function (entry) { return isEqual(entry.key, cacheKey) }) if (!cached) { cached = { key: cacheKey, From ee71e4937de302af9525f1f36457820cb453588c Mon Sep 17 00:00:00 2001 From: Ben Mosher Date: Tue, 1 May 2018 07:25:34 -0400 Subject: [PATCH 3/4] properly report resolver test coverage - also dropped webpack resolver support for node 0.10 (lol) --- resolvers/node/package.json | 4 +++- resolvers/webpack/index.js | 3 +-- resolvers/webpack/package.json | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/resolvers/node/package.json b/resolvers/node/package.json index c5859833c..cd25943ee 100644 --- a/resolvers/node/package.json +++ b/resolvers/node/package.json @@ -7,7 +7,8 @@ "index.js" ], "scripts": { - "test": "nyc mocha" + "test": "nyc mocha", + "coveralls": "nyc report --reporter lcovonly && cd ../.. && coveralls < ./resolvers/node/coverage/lcov.info" }, "repository": { "type": "git", @@ -32,6 +33,7 @@ }, "devDependencies": { "chai": "^3.5.0", + "coveralls": "^3.0.0", "mocha": "^3.5.3", "nyc": "^10.3.2" } diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index e2ba3a471..c17b2a1e5 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -4,8 +4,7 @@ var findRoot = require('find-root') , isEqual = require('lodash/isEqual') , find = require('array-find') , interpret = require('interpret') - // not available on 0.10.x - , isAbsolute = path.isAbsolute || require('is-absolute') + , isAbsolute = require('is-absolute') , fs = require('fs') , coreLibs = require('node-libs-browser') , resolve = require('resolve') diff --git a/resolvers/webpack/package.json b/resolvers/webpack/package.json index 7b3191545..a44382e74 100644 --- a/resolvers/webpack/package.json +++ b/resolvers/webpack/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "test": "nyc mocha -t 5s", - "report": "nyc report --reporter=html" + "report": "nyc report --reporter=html", + "coveralls": "nyc report --reporter lcovonly && cd ../.. && coveralls < ./resolvers/webpack/coverage/lcov.info" }, "files": [ "index.js", @@ -50,6 +51,7 @@ "babel-preset-es2015-argon": "^0.1.0", "babel-register": "^6.26.0", "chai": "^3.4.1", + "coveralls": "^3.0.0", "mocha": "^2.3.3", "nyc": "^7.0.0" } From 4a3d5adb100405f56325319b2945d3fd6d290c20 Mon Sep 17 00:00:00 2001 From: Ben Mosher Date: Tue, 1 May 2018 12:47:47 -0400 Subject: [PATCH 4/4] went the wrong way on the isAbsolute change before MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 😅 _now_ it doesn't support 0.10.x anymore --- resolvers/webpack/index.js | 5 ++--- resolvers/webpack/package.json | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index c17b2a1e5..1a39d92a1 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -4,7 +4,6 @@ var findRoot = require('find-root') , isEqual = require('lodash/isEqual') , find = require('array-find') , interpret = require('interpret') - , isAbsolute = require('is-absolute') , fs = require('fs') , coreLibs = require('node-libs-browser') , resolve = require('resolve') @@ -56,7 +55,7 @@ exports.resolve = function (source, file, settings) { if (!configPath || typeof configPath === 'string') { // see if we've got an absolute path - if (!configPath || !isAbsolute(configPath)) { + if (!configPath || !path.isAbsolute(configPath)) { // if not, find ancestral package.json and use its directory as base for the path packageDir = findRoot(path.resolve(file)) if (!packageDir) throw new Error('package not found above ' + file) @@ -335,7 +334,7 @@ function findConfigPath(configPath, packageDir) { }) // see if we've got an absolute path - if (!isAbsolute(configPath)) { + if (!path.isAbsolute(configPath)) { configPath = path.join(packageDir, configPath) } } else { diff --git a/resolvers/webpack/package.json b/resolvers/webpack/package.json index a44382e74..29dcc3cf6 100644 --- a/resolvers/webpack/package.json +++ b/resolvers/webpack/package.json @@ -36,7 +36,6 @@ "find-root": "^1.1.0", "has": "^1.0.1", "interpret": "^1.0.0", - "is-absolute": "^0.2.3", "lodash": "^4.17.4", "node-libs-browser": "^1.0.0 || ^2.0.0", "resolve": "^1.4.0",