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

Option to generate report file #118

Merged
merged 5 commits into from
Oct 17, 2016
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
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,28 @@ module.exports = {
}
```

##### `outputReport` (default: `false`)
Write the output of the errors to a file, for example a checkstyle xml file for use for reporting on Jenkins CI

The `filePath` is relative to the webpack config: output.path
You can pass in a different formatter for the output file, if none is passed in the default/configured formatter will be used

```js
module.exports = {
entry: "...",
module: {
// ...
},
eslint: {
outputReport: {
filePath: 'checkstyle.xml',
formatter: require('eslint/lib/formatters/checkstyle')
}
}
}
```


## Gotchas

### NoErrorsPlugin
Expand All @@ -239,5 +261,3 @@ remove `NoErrorsPlugin` from webpack config.
## [Changelog](CHANGELOG.md)

## [License](LICENSE)


12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ function lint(input, config, webpack) {
})
var messages = config.formatter(res.results)

if (config.outputReport) {
var reportOutput
// if a different formatter is passed in as an option use that
if (config.outputReport.formatter) {
reportOutput = config.outputReport.formatter(res.results)
}
else {
reportOutput = messages
}
webpack.emitFile(config.outputReport.filePath, reportOutput)
}

// default behavior: emit error only if we have errors
var emitter = res.errorCount ? webpack.emitError : webpack.emitWarning

Expand Down
49 changes: 49 additions & 0 deletions test/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var test = require("tape")
var webpack = require("webpack")
var assign = require("object-assign")
var conf = require("./utils/conf")
var fs = require("fs")

/* eslint-disable no-console */
test("eslint-loader can use eslint formatter", function(t) {
Expand Down Expand Up @@ -57,3 +58,51 @@ test("eslint-loader can use custom formatter", function(t) {
t.end()
})
})

test("eslint-loader cant be configured to write eslint results to a file",
function(t) {

var outputFilename = "outputReport.txt"

webpack(assign({},
conf,
{
entry: "./test/fixtures/error.js",
eslint: assign({}, conf.eslint, {
formatter: require("eslint/lib/formatters/checkstyle"),
outputReport: {
filePath: outputFilename,
},
}),
}
),
function(err, stats) {
if (err) {
throw err
}

console.log("### Here is a the outpur of the formatter")
console.log(
"# " +
stats.compilation.errors[0].message
.split("\n")
.join("\n# ")
)

fs.readFile(conf.output.path + outputFilename,
"utf8", function(err, contents) {
if (err) {
t.fail("Expected file to have been created")
}
else {
t.pass("File has been created")

t.equal(stats.compilation.errors[0].message, contents,
"File Contents should equal output")
}

})

t.end()
})
})