diff --git a/index.js b/index.js index 2ed27bd..7e1c260 100644 --- a/index.js +++ b/index.js @@ -4,8 +4,9 @@ var loaderUtils = require("loader-utils") var crypto = require("crypto") var fs = require("fs") var findCacheDir = require("find-cache-dir") +var objectHash = require("object-hash") -var engine = null +var engines = {} var cache = null var cachePath = null @@ -39,7 +40,8 @@ function lint(input, config, webpack) { // Re-lint the text if the cache off or miss if (!res) { - res = engine.executeOnText(input, resourcePath, true) + var configHash = objectHash(config) + res = engines[configHash].executeOnText(input, resourcePath, true) // Save new results in the cache if (config.cache) { @@ -149,9 +151,10 @@ module.exports = function(input, map) { ) this.cacheable() - // Create the engine only once - if (engine === null) { - engine = new eslint.CLIEngine(config) + // Create the engine only once per config + var configHash = objectHash(config) + if (!engines[configHash]) { + engines[configHash] = new eslint.CLIEngine(config) } // Read the cached information only once and if enable diff --git a/package.json b/package.json index 2ea4fd4..ab25a00 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "dependencies": { "find-cache-dir": "^0.1.1", "loader-utils": "^0.2.7", - "object-assign": "^4.0.1" + "object-assign": "^4.0.1", + "object-hash": "^1.1.4" }, "devDependencies": { "eslint": "^3.0.0", diff --git a/test/multiple-engines.js b/test/multiple-engines.js new file mode 100644 index 0000000..dab60a4 --- /dev/null +++ b/test/multiple-engines.js @@ -0,0 +1,59 @@ +var test = require("tape") +var webpack = require("webpack") +var assign = require("object-assign") +var conf = require("./utils/conf") + +test.only("eslint-loader will create an engine for each unique config", function(t) { // eslint-disable-line max-len + webpack(assign({}, + conf, + { + entry: "./test/fixtures/good.js", + module: { + loaders: [ + { + test: /\.js$/, + loader: "./index", + query: { + rules: { + quotes: [1, "single"], + }, + }, + exclude: /node_modules/, + }, + { + test: /\.js$/, + loader: "./index", + query: { + rules: { + semi: [1, "always"], + }, + }, + exclude: /node_modules/, + }, + ], + }, + } + ), + function(err, stats) { + if (err) { + throw err + } + + t.ok( + stats.compilation.warnings.length === 2, + "should report an error for each config" + ) + + t.ok( + stats.compilation.warnings.find(warning => /quotes/.test(warning)), + "should have a warning about quotes" + ) + + t.ok( + stats.compilation.warnings.find(warning => /semi/.test(warning)), + "should have a warning about semi" + ) + + t.end() + }) +})