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

Add eslintPath option #181

Merged
merged 4 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,28 @@ module.exports = {
}
```

#### `eslintPath` (default: "eslint")

Path to `eslint` instance that will be used for linting.

```js
module.exports = {
entry: "...",
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "eslint-loader",
options: {
eslintPath: path.join(__dirname, "reusable-eslint-rules.js"),
}
},
],
},
}
```

#### Errors and Warning

**By default the loader will auto adjust error reporting depending
Expand Down
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict"

var eslint = require("eslint")
var assign = require("object-assign")
var loaderUtils = require("loader-utils")
var objectHash = require("object-hash")
Expand Down Expand Up @@ -68,6 +67,7 @@ function printLinterOutput(res, config, webpack) {

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

Expand Down Expand Up @@ -142,19 +142,25 @@ function printLinterOutput(res, config, webpack) {
*/
module.exports = function(input, map) {
var webpack = this

var userOptions = assign(
// user defaults
this.options.eslint || {},
// loader query string
loaderUtils.getOptions(this)
)

var config = assign(
// loader defaults
{
formatter: require("eslint/lib/formatters/stylish"),
cacheIdentifier: JSON.stringify({
"eslint-loader": pkg.version,
eslint: eslint.version,
eslint: require(userOptions.eslintPath || "eslint").version,
}),
eslintPath: "eslint",
},
// user defaults
this.options.eslint || {},
// loader query string
loaderUtils.getOptions(this)
userOptions
)

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

Expand Down
33 changes: 33 additions & 0 deletions test/eslint-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var path = require("path")
var test = require("ava")
var webpack = require("webpack")
var conf = require("./utils/conf")

test.cb("eslint-loader can use another instance of eslint via " +
"eslintPath config", function(t) {
t.plan(2)
webpack(conf(
{
entry: "./test/fixtures/good.js",
},
{
eslintPath: path.join(__dirname, "mock/eslint-mock.js"),
}
),
function(err, stats) {
if (err) {
throw err
}

// console.log(stats.compilation.errors)
t.true(
stats.hasErrors(),
"a file that does not contains error but mock eslint instance " +
"returned error"
)
t.true(
stats.compilation.errors[0].message.indexOf("Fake error") > -1
)
t.end()
})
})
21 changes: 21 additions & 0 deletions test/mock/eslint-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function CLIEngine() {

}

CLIEngine.prototype.executeOnText = function() {
return {
warningCount: 0,
errorCount: 1,
results: [{
messages: [
{
message: "Fake error",
},
],
}],
}
}

module.exports = {
CLIEngine: CLIEngine,
}