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

Commit 7108379

Browse files
TimerMoOx
authored andcommitted
Allow formatter to receive string (path) to file exporting formatter function (#247)
* Only load standard formatter in appropriate scenario * Allow file path to be used for formatter Closes #246 Closes #239
1 parent 3d63b44 commit 7108379

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

Diff for: index.js

+26-10
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,10 @@ module.exports = function(input, map) {
155155
);
156156

157157
var userEslintPath = userOptions.eslintPath;
158-
var formatter = require("eslint/lib/formatters/stylish");
159-
160-
if (userEslintPath) {
161-
try {
162-
formatter = require(userEslintPath + "/lib/formatters/stylish");
163-
} catch (e) {
164-
formatter = require("eslint/lib/formatters/stylish");
165-
}
166-
}
167158

168159
var config = assign(
169160
// loader defaults
170161
{
171-
formatter: formatter,
172162
cacheIdentifier: JSON.stringify({
173163
"eslint-loader": pkg.version,
174164
eslint: require(userEslintPath || "eslint").version
@@ -178,6 +168,32 @@ module.exports = function(input, map) {
178168
userOptions
179169
);
180170

171+
if (typeof config.formatter === "string") {
172+
try {
173+
config.formatter = require(config.formatter);
174+
if (
175+
config.formatter &&
176+
typeof config.formatter !== "function" &&
177+
typeof config.formatter.default === "function"
178+
) {
179+
config.formatter = config.formatter.default;
180+
}
181+
} catch (_) {
182+
// ignored
183+
}
184+
}
185+
if (config.formatter == null || typeof config.formatter !== "function") {
186+
if (userEslintPath) {
187+
try {
188+
config.formatter = require(userEslintPath + "/lib/formatters/stylish");
189+
} catch (e) {
190+
config.formatter = require("eslint/lib/formatters/stylish");
191+
}
192+
} else {
193+
config.formatter = require("eslint/lib/formatters/stylish");
194+
}
195+
}
196+
181197
var cacheDirectory = config.cache;
182198
var cacheIdentifier = config.cacheIdentifier;
183199

0 commit comments

Comments
 (0)