diff --git a/index.js b/index.js index 7e1c260..87c6a2f 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var findCacheDir = require("find-cache-dir") var objectHash = require("object-hash") var engines = {} +var rules = {} var cache = null var cachePath = null @@ -28,25 +29,39 @@ function lint(input, config, webpack) { resourcePath = resourcePath.substr(cwd.length + 1) } + // get engine + var configHash = objectHash(config) + var engine = engines[configHash] + var rulesHash = rules[configHash] + var res // If cache is enable and the data are the same as in the cache, just // use them if (config.cache) { + // just get rules hash once per engine for performance reasons + if (!rulesHash) { + rulesHash = objectHash(engine.getConfigForFile(resourcePath)) + rules[configHash] = rulesHash + } var inputMD5 = crypto.createHash("md5").update(input).digest("hex") - if (cache[resourcePath] && cache[resourcePath].hash === inputMD5) { + if ( + cache[resourcePath] && + cache[resourcePath].hash === inputMD5 && + cache[resourcePath].rules === rulesHash + ) { res = cache[resourcePath].res } } // Re-lint the text if the cache off or miss if (!res) { - var configHash = objectHash(config) - res = engines[configHash].executeOnText(input, resourcePath, true) + res = engine.executeOnText(input, resourcePath, true) // Save new results in the cache if (config.cache) { cache[resourcePath] = { hash: inputMD5, + rules: rulesHash, res: res, } fs.writeFileSync(cachePath, JSON.stringify(cache)) diff --git a/test/cache.js b/test/cache.js new file mode 100644 index 0000000..241f011 --- /dev/null +++ b/test/cache.js @@ -0,0 +1,48 @@ +var test = require("ava") +var webpack = require("webpack") +var conf = require("./utils/conf") +var fs = require("fs") + +var cacheFilePath = "./node_modules/.cache/eslint-loader/data.json" + +test.cb("eslint-loader can cache results", function(t) { + t.plan(2) + webpack(conf( + { + entry: "./test/fixtures/cache.js", + }, + { + cache: true, + } + ), + function(err) { + if (err) { + throw err + } + + fs.readFile(cacheFilePath, "utf8", function(err, contents) { + if (err) { + t.fail("expected cache file to have been created") + } + else { + t.pass("cache file has been created") + + var contentsJson = JSON.parse(contents) + t.deepEqual( + Object.keys(contentsJson["test/fixtures/cache.js"]), + ["hash", "rules", "res"], + "cache values have been set for the linted file" + ) + } + + t.end() + + }) + + }) +}) + +// delete the cache file once tests have completed +test.after.always("teardown", function() { + fs.unlinkSync(cacheFilePath) +}) \ No newline at end of file diff --git a/test/fixtures/cache.js b/test/fixtures/cache.js new file mode 100644 index 0000000..48bffcc --- /dev/null +++ b/test/fixtures/cache.js @@ -0,0 +1,7 @@ +"use strict" + +function cacheIt() { + return "cache" +} + +cacheIt()