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

Commit 997cce5

Browse files
ricardogobbosouzaevilebottnawi
authored andcommitted
fix: try load official formatter (#285)
1 parent 8cb02a6 commit 997cce5

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ This can either be a `boolean` value or the cache directory path(ex: `'./.eslint
107107
If `cache: true` is used, the cache file is written to the `./node_modules/.cache` directory.
108108
This is the recommended usage.
109109

110-
#### `formatter` (default: eslint stylish formatter)
110+
#### `formatter` (default: "stylish")
111111

112112
Loader accepts a function that will have one argument: an array of eslint messages (object).
113113
The function must return the output as a string.
114-
You can use official eslint formatters.
114+
You can use official [eslint formatters](https://eslint.org/docs/user-guide/formatters/).
115115

116116
```js
117117
module.exports = {
@@ -126,7 +126,7 @@ module.exports = {
126126
// several examples !
127127

128128
// default value
129-
formatter: require("eslint/lib/formatters/stylish"),
129+
formatter: "stylish",
130130

131131
// community formatter
132132
formatter: require("eslint-friendly-formatter"),
@@ -287,7 +287,7 @@ module.exports = {
287287
options: {
288288
outputReport: {
289289
filePath: "checkstyle.xml",
290-
formatter: require("eslint/lib/formatters/checkstyle")
290+
formatter: "checkstyle"
291291
}
292292
}
293293
}

index.js

+25-19
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,6 @@ module.exports = function(input, map) {
195195
userOptions
196196
);
197197

198-
if (typeof config.formatter === "string") {
199-
try {
200-
config.formatter = require(config.formatter);
201-
if (
202-
config.formatter &&
203-
typeof config.formatter !== "function" &&
204-
typeof config.formatter.default === "function"
205-
) {
206-
config.formatter = config.formatter.default;
207-
}
208-
} catch (_) {
209-
// ignored
210-
}
211-
}
212-
213198
var cacheDirectory = config.cache;
214199
var cacheIdentifier = config.cacheIdentifier;
215200

@@ -221,15 +206,36 @@ module.exports = function(input, map) {
221206
if (!engines[configHash]) {
222207
var eslint = require(config.eslintPath);
223208
engines[configHash] = new eslint.CLIEngine(config);
224-
}
225209

226-
var engine = engines[configHash];
227-
if (config.formatter == null || typeof config.formatter !== "function") {
228-
config.formatter = engine.getFormatter("stylish");
210+
// Try to get oficial formatter
211+
if (typeof config.formatter === "string") {
212+
try {
213+
config.formatter = engines[configHash].getFormatter(config.formatter);
214+
} catch (_) {
215+
try {
216+
config.formatter = require(config.formatter);
217+
if (
218+
config.formatter &&
219+
typeof config.formatter !== "function" &&
220+
typeof config.formatter.default === "function"
221+
) {
222+
config.formatter = config.formatter.default;
223+
}
224+
} catch (_) {
225+
// ignored
226+
}
227+
}
228+
}
229+
230+
// Get default formatter `stylish` when not defined
231+
if (config.formatter == null || typeof config.formatter !== "function") {
232+
config.formatter = engines[configHash].getFormatter("stylish");
233+
}
229234
}
230235

231236
webpack.cacheable();
232237

238+
var engine = engines[configHash];
233239
var resourcePath = webpack.resourcePath;
234240
var cwd = process.cwd();
235241

test/formatter-official.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-disable no-console */
2+
var test = require("ava");
3+
var webpack = require("webpack");
4+
5+
var conf = require("./utils/conf");
6+
7+
test.cb("eslint-loader can use official formatter", function(t) {
8+
t.plan(1);
9+
webpack(
10+
conf(
11+
{
12+
entry: "./test/fixtures/error.js"
13+
},
14+
{
15+
formatter: "table"
16+
}
17+
),
18+
function(err, stats) {
19+
if (err) {
20+
throw err;
21+
}
22+
23+
// console.log("### Here is a example of official formatter")
24+
// console.log(
25+
// "# " +
26+
// stats.compilation.errors[0].message
27+
// .split("\n")
28+
// .join("\n# ")
29+
// )
30+
t.truthy(
31+
stats.compilation.errors[0].message,
32+
"webpack have some output with official formatter"
33+
);
34+
t.end();
35+
}
36+
);
37+
});

0 commit comments

Comments
 (0)