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

Commit ff4737c

Browse files
trungdq88MoOx
authored andcommitted
Added: eslintPath option (#181)
* Add eslintPath option * Add test case * Documentation * Fix
1 parent 553d551 commit ff4737c

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ module.exports = {
143143
}
144144
```
145145

146+
#### `eslintPath` (default: "eslint")
147+
148+
Path to `eslint` instance that will be used for linting.
149+
150+
```js
151+
module.exports = {
152+
entry: "...",
153+
module: {
154+
rules: [
155+
{
156+
test: /\.js$/,
157+
exclude: /node_modules/,
158+
loader: "eslint-loader",
159+
options: {
160+
eslintPath: path.join(__dirname, "reusable-eslint-rules.js"),
161+
}
162+
},
163+
],
164+
},
165+
}
166+
```
167+
146168
#### Errors and Warning
147169

148170
**By default the loader will auto adjust error reporting depending

index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict"
22

3-
var eslint = require("eslint")
43
var assign = require("object-assign")
54
var loaderUtils = require("loader-utils")
65
var objectHash = require("object-hash")
@@ -68,6 +67,7 @@ function printLinterOutput(res, config, webpack) {
6867

6968
// if enabled, use eslint auto-fixing where possible
7069
if (config.fix && res.results[0].output) {
70+
var eslint = require(config.eslintPath)
7171
eslint.CLIEngine.outputFixes(res)
7272
}
7373

@@ -142,19 +142,25 @@ function printLinterOutput(res, config, webpack) {
142142
*/
143143
module.exports = function(input, map) {
144144
var webpack = this
145+
146+
var userOptions = assign(
147+
// user defaults
148+
this.options.eslint || {},
149+
// loader query string
150+
loaderUtils.getOptions(this)
151+
)
152+
145153
var config = assign(
146154
// loader defaults
147155
{
148156
formatter: require("eslint/lib/formatters/stylish"),
149157
cacheIdentifier: JSON.stringify({
150158
"eslint-loader": pkg.version,
151-
eslint: eslint.version,
159+
eslint: require(userOptions.eslintPath || "eslint").version,
152160
}),
161+
eslintPath: "eslint",
153162
},
154-
// user defaults
155-
this.options.eslint || {},
156-
// loader query string
157-
loaderUtils.getOptions(this)
163+
userOptions
158164
)
159165

160166
var cacheDirectory = config.cache
@@ -166,6 +172,7 @@ module.exports = function(input, map) {
166172
// Create the engine only once per config
167173
var configHash = objectHash(config)
168174
if (!engines[configHash]) {
175+
var eslint = require(config.eslintPath)
169176
engines[configHash] = new eslint.CLIEngine(config)
170177
}
171178

test/eslint-path.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var path = require("path")
2+
var test = require("ava")
3+
var webpack = require("webpack")
4+
var conf = require("./utils/conf")
5+
6+
test.cb("eslint-loader can use another instance of eslint via " +
7+
"eslintPath config", function(t) {
8+
t.plan(2)
9+
webpack(conf(
10+
{
11+
entry: "./test/fixtures/good.js",
12+
},
13+
{
14+
eslintPath: path.join(__dirname, "mock/eslint-mock.js"),
15+
}
16+
),
17+
function(err, stats) {
18+
if (err) {
19+
throw err
20+
}
21+
22+
// console.log(stats.compilation.errors)
23+
t.true(
24+
stats.hasErrors(),
25+
"a file that does not contains error but mock eslint instance " +
26+
"returned error"
27+
)
28+
t.true(
29+
stats.compilation.errors[0].message.indexOf("Fake error") > -1
30+
)
31+
t.end()
32+
})
33+
})

test/mock/eslint-mock.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function CLIEngine() {
2+
3+
}
4+
5+
CLIEngine.prototype.executeOnText = function() {
6+
return {
7+
warningCount: 0,
8+
errorCount: 1,
9+
results: [{
10+
messages: [
11+
{
12+
message: "Fake error",
13+
},
14+
],
15+
}],
16+
}
17+
}
18+
19+
module.exports = {
20+
CLIEngine: CLIEngine,
21+
}

0 commit comments

Comments
 (0)