From adf6be3944495fa25aa73019cfbe8549656e5451 Mon Sep 17 00:00:00 2001 From: Trung Dinh Quang Date: Tue, 13 Jun 2017 22:15:12 +0700 Subject: [PATCH 1/4] Add eslintPath option --- index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 467d3d1..1712364 100644 --- a/index.js +++ b/index.js @@ -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") @@ -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) } @@ -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 @@ -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) } From 95ab0c0eae47e3bf90a9937f080038d0ac78eca4 Mon Sep 17 00:00:00 2001 From: Trung Dinh Quang Date: Tue, 13 Jun 2017 22:54:57 +0700 Subject: [PATCH 2/4] Add test case --- test/eslint-path.js | 32 ++++++++++++++++++++++++++++++++ test/mock/eslint-mock.js | 21 +++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test/eslint-path.js create mode 100644 test/mock/eslint-mock.js diff --git a/test/eslint-path.js b/test/eslint-path.js new file mode 100644 index 0000000..a17e758 --- /dev/null +++ b/test/eslint-path.js @@ -0,0 +1,32 @@ +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 + } + + t.true( + stats.hasErrors(), + "a file that does not contains error but mock eslint instance " + + "returned error" + ) + t.true( + stats.compilation.errors[0].message.message.indexOf("Fake error") > -1 + ) + t.end() + }) +}) diff --git a/test/mock/eslint-mock.js b/test/mock/eslint-mock.js new file mode 100644 index 0000000..0bddc15 --- /dev/null +++ b/test/mock/eslint-mock.js @@ -0,0 +1,21 @@ +function CLIEngine() { + +} + +CLIEngine.prototype.executeOnText = function() { + return { + warningCount: 0, + errorCount: 1, + results: [{ + messages: [ + { + message: "Fake error", + }, + ], + }], + } +} + +module.exports = { + CLIEngine: CLIEngine, +} From fbe9bdb634fdcf844ba22a04f4eb1aa1d617737b Mon Sep 17 00:00:00 2001 From: Trung Dinh Quang Date: Tue, 13 Jun 2017 23:02:01 +0700 Subject: [PATCH 3/4] Documentation --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 7a5fdc9..da947e0 100644 --- a/README.md +++ b/README.md @@ -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 From c4ada63bb6046e2c976313bf89ce79c143e5109c Mon Sep 17 00:00:00 2001 From: Trung Dinh Quang Date: Tue, 13 Jun 2017 23:09:10 +0700 Subject: [PATCH 4/4] Fix --- test/eslint-path.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/eslint-path.js b/test/eslint-path.js index a17e758..7e738e7 100644 --- a/test/eslint-path.js +++ b/test/eslint-path.js @@ -19,13 +19,14 @@ test.cb("eslint-loader can use another instance of eslint via " + 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.message.indexOf("Fake error") > -1 + stats.compilation.errors[0].message.indexOf("Fake error") > -1 ) t.end() })