Skip to content

Commit 53dfa7c

Browse files
antfuModyQyW
andcommitted
feat: use vite-plugin-eslint2, support checker for flat config
Thanks to nuxt-modules/eslint#124 Co-authored-by: ModyQyW <[email protected]>
1 parent 7550e1e commit 53dfa7c

File tree

4 files changed

+86
-27
lines changed

4 files changed

+86
-27
lines changed

packages/module/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"get-port-please": "^3.1.2",
4444
"pathe": "^1.1.2",
4545
"unimport": "^3.7.1",
46-
"vite-plugin-eslint": "^1.8.1"
46+
"vite-plugin-eslint2": "^4.4.0"
4747
},
4848
"devDependencies": {
4949
"@nuxt/module-builder": "^0.5.5",

packages/module/src/modules/checker.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */
21
import { Nuxt } from '@nuxt/schema'
32
import { CheckerOptions, ModuleOptions } from '../module'
4-
import { defineNuxtModule, addVitePlugin, addWebpackPlugin, useLogger } from '@nuxt/kit'
5-
import vitePluginEslint from 'vite-plugin-eslint'
3+
import { addVitePlugin, addWebpackPlugin, useLogger } from '@nuxt/kit'
4+
import vitePluginEslint from 'vite-plugin-eslint2'
65
import EslintWebpackPlugin from 'eslint-webpack-plugin'
76
import { relative } from 'pathe'
87
import { watch } from 'chokidar'
8+
import { existsSync } from 'fs'
9+
import { resolve } from 'path'
910

1011
const logger = useLogger('nuxt:eslint:checker')
1112

13+
const flatConfigFiles = [
14+
'eslint.config.js',
15+
'eslint.config.mjs',
16+
'eslint.config.cjs',
17+
]
18+
1219
export function setupESLintChecker(moduleOptions: ModuleOptions, nuxt: Nuxt) {
1320
// TODO: maybe support build mode later on
1421
if (!nuxt.options.dev) {
@@ -19,8 +26,6 @@ export function setupESLintChecker(moduleOptions: ModuleOptions, nuxt: Nuxt) {
1926
cache: true,
2027
include: [`${nuxt.options.srcDir}/**/*.{js,jsx,ts,tsx,vue}`],
2128
exclude: ['**/node_modules/**', nuxt.options.buildDir],
22-
eslintPath: 'eslint',
23-
formatter: 'stylish',
2429
lintOnStart: true,
2530
emitWarning: true,
2631
emitError: true,
@@ -29,13 +34,16 @@ export function setupESLintChecker(moduleOptions: ModuleOptions, nuxt: Nuxt) {
2934
...(typeof moduleOptions.checker === 'boolean' ? {} : moduleOptions.checker || {}),
3035
}
3136

37+
const isUsingFlatConfig = process.env.ESLINT_USE_FLAT_CONFIG || flatConfigFiles.some(file => existsSync(resolve(nuxt.options.rootDir, file)))
38+
3239
const configPaths = [
3340
'.eslintignore',
3441
'.eslintrc',
3542
'.eslintrc.js',
3643
'.eslintrc.yaml',
3744
'.eslintrc.yml',
3845
'.eslintrc.json',
46+
...flatConfigFiles,
3947
].map(path => relative(nuxt.options.rootDir, path))
4048

4149
if (nuxt.options.watch) {
@@ -49,7 +57,18 @@ export function setupESLintChecker(moduleOptions: ModuleOptions, nuxt: Nuxt) {
4957
nuxt.hook('close', () => watcher.close())
5058
}
5159

52-
addVitePlugin(() => vitePluginEslint(options), { server: false })
60+
addVitePlugin(() => {
61+
const viteOptions = {
62+
linkInWorker: true,
63+
...options,
64+
...options.vite,
65+
}
66+
67+
// https://github.com/ModyQyW/vite-plugin-eslint2#eslintpath
68+
viteOptions.eslintPath ||= isUsingFlatConfig ? 'eslint/use-at-your-own-risk' : 'eslint'
69+
70+
return vitePluginEslint(viteOptions)
71+
}, { server: false })
5372

5473
addWebpackPlugin(() => {
5574
const webpackOptions = {

packages/module/src/types.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Import } from 'unimport'
2-
import type { Options as ViteCheckerOptions } from 'vite-plugin-eslint'
2+
import type { ESLintPluginOptions as ViteCheckerOptions } from 'vite-plugin-eslint2'
33
import type { Options as WebpackCheckerOptions } from 'eslint-webpack-plugin'
44

55
export interface ConfigGenOptions {
@@ -13,9 +13,35 @@ export interface ConfigGenOptions {
1313
standalone?: boolean
1414
}
1515

16-
export interface CheckerOptions extends ViteCheckerOptions, WebpackCheckerOptions {
16+
export interface CheckerOptions {
1717
/**
18-
* Checking matched files on start
18+
* Use ESLint cache to improve performance
19+
*
20+
* @default true
21+
*/
22+
cache?: boolean
23+
24+
/**
25+
* Files to include for linting
26+
*/
27+
include?: string[]
28+
29+
/**
30+
* Files to exclude from linting
31+
*/
32+
exclude?: string[]
33+
34+
/**
35+
* ESLint formatter for the output
36+
*
37+
* @see https://eslint.org/docs/user-guide/formatters/
38+
*/
39+
formatter?: string
40+
41+
/**
42+
* Lint on start
43+
*
44+
* @default true
1945
*/
2046
lintOnStart?: boolean
2147

@@ -46,6 +72,18 @@ export interface CheckerOptions extends ViteCheckerOptions, WebpackCheckerOption
4672
* @default false
4773
*/
4874
failOnWarning?: boolean
75+
76+
/**
77+
* Vite specific options
78+
*/
79+
vite?: ViteCheckerOptions
80+
81+
/**
82+
* Webpack specific options
83+
*
84+
* @see https://www.npmjs.com/package/eslint-webpack-plugin
85+
*/
86+
webpack?: WebpackCheckerOptions
4987
}
5088

5189
export interface ModuleOptions {

pnpm-lock.yaml

Lines changed: 19 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)