/
index.ts
executable file
路83 lines (67 loc) 路 2.35 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { relative, resolve, sep } from 'path';
import { Plugin } from 'rollup';
import { createFilter } from '@rollup/pluginutils';
import { ESLint } from 'eslint';
import type { RollupEslintOptions } from '../types';
function normalizePath(id: string) {
return relative(process.cwd(), id).split(sep).join('/');
}
export default function eslint(options = {} as RollupEslintOptions): Plugin {
if (typeof options === 'string') {
const configFile = resolve(process.cwd(), options);
// eslint-disable-next-line global-require, import/no-dynamic-require, no-param-reassign
options = require(configFile);
// Tell eslint not to look for configuration files.
// eslint-disable-next-line no-param-reassign
options.useEslintrc = false;
}
const {
include,
exclude = /node_modules/,
throwOnWarning = false,
throwOnError = false,
formatter = 'stylish',
...eslintOptions
} = options;
const eslintInstance = new ESLint(eslintOptions);
const filter = createFilter(include, exclude);
return {
name: 'eslint',
async transform(_, id: string) {
const file = normalizePath(id);
if (!filter(id) || (await eslintInstance.isPathIgnored(file))) {
return null;
}
const results = await eslintInstance.lintFiles(file);
const [result] = results;
if (eslintOptions.fix) {
await ESLint.outputFixes(results);
}
if (result.warningCount === 0 && result.errorCount === 0) {
return null;
}
const eslintFormatter: ESLint.Formatter =
typeof formatter === 'string'
? await eslintInstance.loadFormatter(formatter)
: { format: formatter };
const output = eslintFormatter.format(results);
if (output) {
// eslint-disable-next-line no-console
console.log(output);
}
const errorMessages = [];
if (result.warningCount > 0 && throwOnWarning) {
errorMessages.push(`${result.warningCount} warning${result.warningCount > 1 ? 's' : ''}`);
}
if (result.errorCount > 0 && throwOnError) {
errorMessages.push(`${result.errorCount} error${result.errorCount > 1 ? 's' : ''}`);
}
if (errorMessages.length > 0) {
throw new Error(
`Found ${errorMessages.join(' and ')} in ${relative('.', result.filePath)}`
);
}
return null;
}
};
}