From 3fe4f6ac3d726c746a11c1a7e307dbaf7d9e6bd7 Mon Sep 17 00:00:00 2001 From: Thomas Genin Date: Wed, 15 Jun 2016 18:10:48 -0700 Subject: [PATCH 1/4] Add the ability to cache the results in a file and reuse them --- index.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ae8b04b..8dc3e17 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,11 @@ var eslint = require("eslint") var assign = require("object-assign") var loaderUtils = require("loader-utils") +var crypto = require('crypto') +var fs = require('fs'); + +var engine = null; +var cache = null; /** * linter @@ -11,8 +16,6 @@ var loaderUtils = require("loader-utils") * @return {void} */ function lint(input, config, webpack) { - var engine = new eslint.CLIEngine(config) - var resourcePath = webpack.resourcePath var cwd = process.cwd() @@ -22,7 +25,30 @@ function lint(input, config, webpack) { resourcePath = resourcePath.substr(cwd.length + 1) } - var res = engine.executeOnText(input, resourcePath, true) + var res; + // If cache is enable and the data are the same as in the cache, just + // use them + if (cache) { + var inputMD5 = crypto.createHash("md5").update(input).digest("hex"); + if (cache[resourcePath] && cache[resourcePath].hash === inputMD5) { + res = cache[resourcePath].res + } + } + + // Re-lint the text if the cache off or miss + if (!res) { + res = engine.executeOnText(input, resourcePath, true) + + // Save new results in the cache + if (cache) { + cache[resourcePath] = { + hash: inputMD5, + res: res + } + fs.writeFileSync(config.cache, JSON.stringify(cache)); + } + } + // executeOnText ensure we will have res.results[0] only // skip ignored file warning @@ -108,6 +134,28 @@ module.exports = function(input, map) { loaderUtils.parseQuery(this.query) ) this.cacheable() + + // Create the engine only once + if (engine === null) { + engine = new eslint.CLIEngine(config) + } + + // Read the cached information only once and if enable + if (cache === null ) { + if (config.cache) { + try { + var cacheFileContent = fs.readFileSync(config.cache); + if (cacheFileContent) { + cache = JSON.parse(cacheFileContent); + } + } catch (e) { + cache = {}; + } + } else { + cache = false + } + } + lint(input, config, this) this.callback(null, input, map) } From 9c637fd92e21c67498d829737e1d7593e6cd3190 Mon Sep 17 00:00:00 2001 From: Thomas Genin Date: Thu, 16 Jun 2016 11:39:50 -0700 Subject: [PATCH 2/4] Use find-cache-dir, Style fixes --- index.js | 42 ++++++++++++++++++++++++++---------------- package.json | 1 + 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 8dc3e17..7f61912 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,13 @@ var eslint = require("eslint") var assign = require("object-assign") var loaderUtils = require("loader-utils") -var crypto = require('crypto') -var fs = require('fs'); +var crypto = require("crypto") +var fs = require("fs") +var findCacheDir = require("find-cache-dir") -var engine = null; -var cache = null; +var engine = null +var cache = null +var cachePath = null /** * linter @@ -25,11 +27,11 @@ function lint(input, config, webpack) { resourcePath = resourcePath.substr(cwd.length + 1) } - var res; + var res // If cache is enable and the data are the same as in the cache, just // use them - if (cache) { - var inputMD5 = crypto.createHash("md5").update(input).digest("hex"); + if (config.cache) { + var inputMD5 = crypto.createHash("md5").update(input).digest("hex") if (cache[resourcePath] && cache[resourcePath].hash === inputMD5) { res = cache[resourcePath].res } @@ -40,12 +42,12 @@ function lint(input, config, webpack) { res = engine.executeOnText(input, resourcePath, true) // Save new results in the cache - if (cache) { + if (config.cache) { cache[resourcePath] = { hash: inputMD5, - res: res + res: res, } - fs.writeFileSync(config.cache, JSON.stringify(cache)); + fs.writeFileSync(cachePath, JSON.stringify(cache)) } } @@ -141,17 +143,25 @@ module.exports = function(input, map) { } // Read the cached information only once and if enable - if (cache === null ) { + if (cache === null) { if (config.cache) { + var thunk = findCacheDir({ + name: "eslint-loader", + thunk: true, + create: true, + }) + cachePath = thunk("data.json") try { - var cacheFileContent = fs.readFileSync(config.cache); + var cacheFileContent = fs.readFileSync(cachePath) if (cacheFileContent) { - cache = JSON.parse(cacheFileContent); + cache = JSON.parse(cacheFileContent) } - } catch (e) { - cache = {}; } - } else { + catch (e) { + cache = {} + } + } + else { cache = false } } diff --git a/package.json b/package.json index 3ccb2dd..fa0b426 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "eslint": ">=1.6.0 <4.0.0" }, "dependencies": { + "find-cache-dir": "^0.1.1", "loader-utils": "^0.2.7", "object-assign": "^4.0.1" }, From 1445959ea1167f4f82cb2630f91460cea315c6e0 Mon Sep 17 00:00:00 2001 From: Thomas Genin Date: Thu, 14 Jul 2016 14:57:30 -0700 Subject: [PATCH 3/4] Add cache option description in the Readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 05e25d1..b6a45b4 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,14 @@ This option will enable **Be careful, this option might cause webpack to enter an infinite build loop if some issues cannot be fixed properly.** +#### `cache` (default: false) + +This option will enable caching of the linting results into a file. +This is particullarly usefull to reduce linting time when doing full build. + +The cache is writting inside the `./node_modules/.cache` directory, thanks to the usage +of the [find-cache-dir](https://www.npmjs.com/package/find-cache-dir) module. + #### `formatter` (default: eslint stylish formatter) Loader accepts a function that will have one argument: an array of eslint messages (object). From 952cddaa2bf7e3701e20cb865be8ad6a656c5151 Mon Sep 17 00:00:00 2001 From: Thomas Genin Date: Wed, 20 Jul 2016 17:11:12 -0700 Subject: [PATCH 4/4] Use require to read the cache file --- index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/index.js b/index.js index 7f61912..17dbd14 100644 --- a/index.js +++ b/index.js @@ -152,10 +152,7 @@ module.exports = function(input, map) { }) cachePath = thunk("data.json") try { - var cacheFileContent = fs.readFileSync(cachePath) - if (cacheFileContent) { - cache = JSON.parse(cacheFileContent) - } + cache = require(cachePath) } catch (e) { cache = {}