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

Commit d05c92c

Browse files
geninthoMoOx
authored andcommitted
Added: cache options (#93)
* 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
1 parent 347af30 commit d05c92c

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ This option will enable
9494
**Be careful, this option might cause webpack to enter an infinite build loop if
9595
some issues cannot be fixed properly.**
9696

97+
#### `cache` (default: false)
98+
99+
This option will enable caching of the linting results into a file.
100+
This is particullarly usefull to reduce linting time when doing full build.
101+
102+
The cache is writting inside the `./node_modules/.cache` directory, thanks to the usage
103+
of the [find-cache-dir](https://www.npmjs.com/package/find-cache-dir) module.
104+
97105
#### `formatter` (default: eslint stylish formatter)
98106

99107
Loader accepts a function that will have one argument: an array of eslint messages (object).

index.js

+58-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
var eslint = require("eslint")
22
var assign = require("object-assign")
33
var loaderUtils = require("loader-utils")
4+
var crypto = require("crypto")
5+
var fs = require("fs")
6+
var findCacheDir = require("find-cache-dir")
7+
8+
var engine = null
9+
var cache = null
10+
var cachePath = null
411

512
/**
613
* linter
@@ -11,8 +18,6 @@ var loaderUtils = require("loader-utils")
1118
* @return {void}
1219
*/
1320
function lint(input, config, webpack) {
14-
var engine = new eslint.CLIEngine(config)
15-
1621
var resourcePath = webpack.resourcePath
1722
var cwd = process.cwd()
1823

@@ -22,7 +27,30 @@ function lint(input, config, webpack) {
2227
resourcePath = resourcePath.substr(cwd.length + 1)
2328
}
2429

25-
var res = engine.executeOnText(input, resourcePath, true)
30+
var res
31+
// If cache is enable and the data are the same as in the cache, just
32+
// use them
33+
if (config.cache) {
34+
var inputMD5 = crypto.createHash("md5").update(input).digest("hex")
35+
if (cache[resourcePath] && cache[resourcePath].hash === inputMD5) {
36+
res = cache[resourcePath].res
37+
}
38+
}
39+
40+
// Re-lint the text if the cache off or miss
41+
if (!res) {
42+
res = engine.executeOnText(input, resourcePath, true)
43+
44+
// Save new results in the cache
45+
if (config.cache) {
46+
cache[resourcePath] = {
47+
hash: inputMD5,
48+
res: res,
49+
}
50+
fs.writeFileSync(cachePath, JSON.stringify(cache))
51+
}
52+
}
53+
2654
// executeOnText ensure we will have res.results[0] only
2755

2856
// skip ignored file warning
@@ -108,6 +136,33 @@ module.exports = function(input, map) {
108136
loaderUtils.parseQuery(this.query)
109137
)
110138
this.cacheable()
139+
140+
// Create the engine only once
141+
if (engine === null) {
142+
engine = new eslint.CLIEngine(config)
143+
}
144+
145+
// Read the cached information only once and if enable
146+
if (cache === null) {
147+
if (config.cache) {
148+
var thunk = findCacheDir({
149+
name: "eslint-loader",
150+
thunk: true,
151+
create: true,
152+
})
153+
cachePath = thunk("data.json")
154+
try {
155+
cache = require(cachePath)
156+
}
157+
catch (e) {
158+
cache = {}
159+
}
160+
}
161+
else {
162+
cache = false
163+
}
164+
}
165+
111166
lint(input, config, this)
112167
this.callback(null, input, map)
113168
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"eslint": ">=1.6.0 <4.0.0"
2020
},
2121
"dependencies": {
22+
"find-cache-dir": "^0.1.1",
2223
"loader-utils": "^0.2.7",
2324
"object-assign": "^4.0.1"
2425
},

0 commit comments

Comments
 (0)