Skip to content

Commit

Permalink
webpack resolver: cache instance(s) of resolve function (#1091)
Browse files Browse the repository at this point in the history
* cache instance(s) of resolve function
* properly report resolver test coverage
- also dropped webpack resolver support for node 0.10 (lol)
  • Loading branch information
benmosher committed May 3, 2018
1 parent 8c9c3b8 commit f0d0c4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
4 changes: 3 additions & 1 deletion resolvers/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -32,6 +33,7 @@
},
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^3.0.0",
"mocha": "^3.5.3",
"nyc": "^10.3.2"
}
Expand Down
30 changes: 24 additions & 6 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var findRoot = require('find-root')
, path = require('path')
, get = require('lodash.get')
, get = require('lodash/get')
, isEqual = require('lodash/isEqual')
, find = require('array-find')
, interpret = require('interpret')
// not available on 0.10.x
, isAbsolute = path.isAbsolute || require('is-absolute')
, fs = require('fs')
, coreLibs = require('node-libs-browser')
, resolve = require('resolve')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -105,7 +104,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 +114,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 Expand Up @@ -316,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 {
Expand Down
7 changes: 4 additions & 3 deletions resolvers/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -35,8 +36,7 @@
"find-root": "^1.1.0",
"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 All @@ -50,6 +50,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"
}
Expand Down

0 comments on commit f0d0c4f

Please sign in to comment.