Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Commit

Permalink
Added: cache options (#93)
Browse files Browse the repository at this point in the history
* Add the ability to cache the results in a file and reuse them

* Use find-cache-dir, Style fixes

* Add cache option description in the Readme

* Use require to read the cache file
  • Loading branch information
genintho authored and MoOx committed Jul 28, 2016
1 parent 347af30 commit d05c92c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
61 changes: 58 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +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 findCacheDir = require("find-cache-dir")

var engine = null
var cache = null
var cachePath = null

/**
* linter
Expand All @@ -11,8 +18,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()

Expand All @@ -22,7 +27,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 (config.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 (config.cache) {
cache[resourcePath] = {
hash: inputMD5,
res: res,
}
fs.writeFileSync(cachePath, JSON.stringify(cache))
}
}

// executeOnText ensure we will have res.results[0] only

// skip ignored file warning
Expand Down Expand Up @@ -108,6 +136,33 @@ 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) {
var thunk = findCacheDir({
name: "eslint-loader",
thunk: true,
create: true,
})
cachePath = thunk("data.json")
try {
cache = require(cachePath)
}
catch (e) {
cache = {}
}
}
else {
cache = false
}
}

lint(input, config, this)
this.callback(null, input, map)
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit d05c92c

Please sign in to comment.