eslint-loader
Advanced tools
Comparing version 1.6.3 to 1.7.0
@@ -0,1 +1,8 @@ | ||
# 1.7.0 - 2017-03-23 | ||
- Fixed: outputReport option writes report for last file checked only | ||
([#160](https://github.com/MoOx/eslint-loader/pull/160) - @deryni) | ||
- Added: use babel loader fs cache as the default caching engine | ||
([#159](https://github.com/MoOx/eslint-loader/pull/159) - @viankakrisna) | ||
# 1.6.3 - 2017-02-22 | ||
@@ -2,0 +9,0 @@ |
181
index.js
var eslint = require("eslint") | ||
var assign = require("object-assign") | ||
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 os = require("os") | ||
var pkg = require("./package.json") | ||
var createCache = require("loader-fs-cache") | ||
var cache = createCache("eslint-loader") | ||
var engines = {} | ||
var rules = {} | ||
var cache = null | ||
var cachePath = null | ||
/** | ||
* linter | ||
* printLinterOutput | ||
* | ||
* @param {String|Buffer} input JavaScript string | ||
* @param {Object} eslint.executeOnText return value | ||
* @param {Object} config eslint configuration | ||
@@ -23,60 +19,10 @@ * @param {Object} webpack webpack instance | ||
*/ | ||
function lint(input, config, webpack) { | ||
var resourcePath = webpack.resourcePath | ||
var cwd = process.cwd() | ||
// remove cwd from resource path in case webpack has been started from project | ||
// root, to allow having relative paths in .eslintignore | ||
if (resourcePath.indexOf(cwd) === 0) { | ||
resourcePath = resourcePath.substr(cwd.length + 1) | ||
} | ||
// get engine | ||
var configHash = objectHash(config) | ||
var engine = engines[configHash] | ||
var rulesHash = rules[configHash] | ||
var res | ||
// If cache is enable and the data are the same as in the cache, just | ||
// use them | ||
if (config.cache) { | ||
// just get rules hash once per engine for performance reasons | ||
if (!rulesHash) { | ||
rulesHash = objectHash(engine.getConfigForFile(resourcePath)) | ||
rules[configHash] = rulesHash | ||
} | ||
var inputMD5 = crypto.createHash("md5").update(input).digest("hex") | ||
if ( | ||
cache[resourcePath] && | ||
cache[resourcePath].hash === inputMD5 && | ||
cache[resourcePath].rules === rulesHash | ||
) { | ||
res = cache[resourcePath].res | ||
} | ||
} | ||
// Re-lint the text if the cache off or miss | ||
if (!res) { | ||
res = engine.executeOnText(input, resourcePath, true) | ||
// Save new results in the cache | ||
if (config.cache) { | ||
cache[resourcePath] = { | ||
hash: inputMD5, | ||
rules: rulesHash, | ||
res: res, | ||
} | ||
fs.writeFileSync(cachePath, JSON.stringify(cache)) | ||
} | ||
} | ||
// executeOnText ensure we will have res.results[0] only | ||
function printLinterOutput(res, config, webpack) { | ||
// skip ignored file warning | ||
if (!( | ||
res.warningCount === 1 && | ||
res.results[0].messages[0] && | ||
res.results[0].messages[0].message && | ||
res.results[0].messages[0].message.indexOf("ignore") > 1 | ||
)) { | ||
if ( | ||
!(res.warningCount === 1 && | ||
res.results[0].messages[0] && | ||
res.results[0].messages[0].message && | ||
res.results[0].messages[0].message.indexOf("ignore") > 1) | ||
) { | ||
// quiet filter done now | ||
@@ -88,6 +34,7 @@ // eslint allow rules to be specified in the input between comments | ||
res.results[0].warningCount = 0 | ||
res.results[0].messages = res.results[0].messages | ||
.filter(function(message) { | ||
return message.severity !== 1 | ||
}) | ||
res.results[0].messages = res.results[0].messages.filter(function( | ||
message | ||
) { | ||
return message.severity !== 1 | ||
}) | ||
} | ||
@@ -107,3 +54,3 @@ | ||
if (config.outputReport) { | ||
if (config.outputReport && config.outputReport.filePath) { | ||
var reportOutput | ||
@@ -117,3 +64,10 @@ // if a different formatter is passed in as an option use that | ||
} | ||
webpack.emitFile(config.outputReport.filePath, reportOutput) | ||
var filePath = loaderUtils.interpolateName(webpack, | ||
config.outputReport.filePath, { | ||
content: res.results.map(function(r) { | ||
return r.source | ||
}).join("\n"), | ||
} | ||
) | ||
webpack.emitFile(filePath, reportOutput) | ||
} | ||
@@ -135,8 +89,10 @@ | ||
if (config.failOnError && res.errorCount) { | ||
throw new Error("Module failed because of a eslint error.\n" | ||
+ messages) | ||
throw new Error( | ||
"Module failed because of a eslint error.\n" + messages | ||
) | ||
} | ||
else if (config.failOnWarning && res.warningCount) { | ||
throw new Error("Module failed because of a eslint warning.\n" | ||
+ messages) | ||
throw new Error( | ||
"Module failed because of a eslint warning.\n" + messages | ||
) | ||
} | ||
@@ -147,4 +103,4 @@ } | ||
"Your module system doesn't support emitWarning. " + | ||
"Update available? \n" + | ||
messages | ||
"Update available? \n" + | ||
messages | ||
) | ||
@@ -164,2 +120,3 @@ } | ||
module.exports = function(input, map) { | ||
var webpack = this | ||
var config = assign( | ||
@@ -169,2 +126,6 @@ // loader defaults | ||
formatter: require("eslint/lib/formatters/stylish"), | ||
cacheIdentifier: JSON.stringify({ | ||
"eslint-loader": pkg.version, | ||
eslint: eslint.version, | ||
}), | ||
}, | ||
@@ -176,4 +137,9 @@ // user defaults | ||
) | ||
this.cacheable() | ||
var cacheDirectory = config.cache | ||
var cacheIdentifier = config.cacheIdentifier | ||
delete config.cacheDirectory | ||
delete config.cacheIdentifier | ||
// Create the engine only once per config | ||
@@ -185,25 +151,42 @@ var configHash = objectHash(config) | ||
// Read the cached information only once and if enable | ||
if (cache === null) { | ||
if (config.cache) { | ||
var thunk = findCacheDir({ | ||
name: "eslint-loader", | ||
thunk: true, | ||
create: true, | ||
}) | ||
cachePath = thunk("data.json") || os.tmpdir() + "/data.json" | ||
try { | ||
cache = require(cachePath) | ||
this.cacheable() | ||
var resourcePath = webpack.resourcePath | ||
var cwd = process.cwd() | ||
// remove cwd from resource path in case webpack has been started from project | ||
// root, to allow having relative paths in .eslintignore | ||
if (resourcePath.indexOf(cwd) === 0) { | ||
resourcePath = resourcePath.substr(cwd.length + 1) | ||
} | ||
var engine = engines[configHash] | ||
// return early if cached | ||
if (config.cache) { | ||
var callback = this.async() | ||
return cache( | ||
{ | ||
directory: cacheDirectory, | ||
identifier: cacheIdentifier, | ||
options: config, | ||
source: input, | ||
transform: function() { | ||
return lint(engine, input, resourcePath) | ||
}, | ||
}, | ||
function(err, res) { | ||
if (err) { | ||
return callback(err) | ||
} | ||
printLinterOutput(res || {}, config, webpack) | ||
return callback(null, input, map) | ||
} | ||
catch (e) { | ||
cache = {} | ||
} | ||
} | ||
else { | ||
cache = false | ||
} | ||
) | ||
} | ||
lint(input, config, this) | ||
printLinterOutput(lint(engine, input, resourcePath), config, this) | ||
this.callback(null, input, map) | ||
} | ||
function lint(engine, input, resourcePath) { | ||
return engine.executeOnText(input, resourcePath, true) | ||
} |
{ | ||
"name": "eslint-loader", | ||
"version": "1.6.3", | ||
"version": "1.7.0", | ||
"description": "eslint loader (for webpack)", | ||
@@ -23,5 +23,7 @@ "keywords": [ | ||
"find-cache-dir": "^0.1.1", | ||
"loader-fs-cache": "^1.0.0", | ||
"loader-utils": "^1.0.2", | ||
"object-assign": "^4.0.1", | ||
"object-hash": "^1.1.4" | ||
"object-hash": "^1.1.4", | ||
"rimraf": "^2.6.1" | ||
}, | ||
@@ -28,0 +30,0 @@ "devDependencies": { |
@@ -276,3 +276,3 @@ # eslint-loader [![Build Status](http://img.shields.io/travis/MoOx/eslint-loader.svg)](https://travis-ci.org/MoOx/eslint-loader) | ||
`NoErrorsPlugin` prevents Webpack from outputting anything into a bundle. So even ESLint warnings | ||
`NoErrorsPlugin` prevents webpack from outputting anything into a bundle. So even ESLint warnings | ||
will fail the build. No matter what error settings are used for `eslint-loader`. | ||
@@ -279,0 +279,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
18524
1
7
165
+ Addedloader-fs-cache@^1.0.0
+ Addedrimraf@^2.6.1
+ Addedloader-fs-cache@1.0.3(transitive)
+ Addedrimraf@2.7.1(transitive)