Skip to content

Commit 7550e1e

Browse files
feat: migrate @nuxtjs/eslint-module
Co-authored-by: ricardogobbosouza <[email protected]>
1 parent eac3926 commit 7550e1e

File tree

6 files changed

+513
-122
lines changed

6 files changed

+513
-122
lines changed

packages/module/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
"@nuxt/eslint-plugin": "workspace:*",
4040
"@nuxt/kit": "^3.10.3",
4141
"eslint-flat-config-viewer": "^0.1.11",
42+
"eslint-webpack-plugin": "^4.1.0",
4243
"get-port-please": "^3.1.2",
4344
"pathe": "^1.1.2",
44-
"unimport": "^3.7.1"
45+
"unimport": "^3.7.1",
46+
"vite-plugin-eslint": "^1.8.1"
4547
},
4648
"devDependencies": {
4749
"@nuxt/module-builder": "^0.5.5",

packages/module/src/module.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { defineNuxtModule } from '@nuxt/kit'
22
import { setupConfigGen } from './modules/config'
3-
import { setupESLintChecker } from './modules/checker'
43
import { ModuleOptions } from './types'
54

65
export * from './types'
@@ -14,12 +13,15 @@ export default defineNuxtModule<ModuleOptions>({
1413
config: true,
1514
checker: false,
1615
},
17-
setup(options, nuxt) {
16+
async setup(options, nuxt) {
1817
if (options.config) {
1918
setupConfigGen(options, nuxt)
2019
}
2120
if (options.checker) {
22-
setupESLintChecker(options, nuxt)
21+
await import('./modules/checker')
22+
.then(({ setupESLintChecker }) => {
23+
setupESLintChecker(options, nuxt)
24+
})
2325
}
2426
},
2527
})
Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,67 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
import { Nuxt } from '@nuxt/schema'
3-
import { ModuleOptions } from '../module'
3+
import { CheckerOptions, ModuleOptions } from '../module'
4+
import { defineNuxtModule, addVitePlugin, addWebpackPlugin, useLogger } from '@nuxt/kit'
5+
import vitePluginEslint from 'vite-plugin-eslint'
6+
import EslintWebpackPlugin from 'eslint-webpack-plugin'
7+
import { relative } from 'pathe'
8+
import { watch } from 'chokidar'
49

5-
export function setupESLintChecker(options: ModuleOptions, nuxt: Nuxt) {
6-
throw new Error('ESLint check is not implemented yet')
10+
const logger = useLogger('nuxt:eslint:checker')
11+
12+
export function setupESLintChecker(moduleOptions: ModuleOptions, nuxt: Nuxt) {
13+
// TODO: maybe support build mode later on
14+
if (!nuxt.options.dev) {
15+
return
16+
}
17+
18+
const options: CheckerOptions = {
19+
cache: true,
20+
include: [`${nuxt.options.srcDir}/**/*.{js,jsx,ts,tsx,vue}`],
21+
exclude: ['**/node_modules/**', nuxt.options.buildDir],
22+
eslintPath: 'eslint',
23+
formatter: 'stylish',
24+
lintOnStart: true,
25+
emitWarning: true,
26+
emitError: true,
27+
failOnWarning: false,
28+
failOnError: false,
29+
...(typeof moduleOptions.checker === 'boolean' ? {} : moduleOptions.checker || {}),
30+
}
31+
32+
const configPaths = [
33+
'.eslintignore',
34+
'.eslintrc',
35+
'.eslintrc.js',
36+
'.eslintrc.yaml',
37+
'.eslintrc.yml',
38+
'.eslintrc.json',
39+
].map(path => relative(nuxt.options.rootDir, path))
40+
41+
if (nuxt.options.watch) {
42+
nuxt.options.watch.push(...configPaths)
43+
}
44+
else {
45+
const watcher = watch(configPaths, { depth: 0 }).on('change', (path: string) => {
46+
logger.info(`Eslint config changed: ${path}`)
47+
logger.warn('Please restart the Nuxt server to apply changes or upgrade to latest Nuxt for automatic restart.')
48+
})
49+
nuxt.hook('close', () => watcher.close())
50+
}
51+
52+
addVitePlugin(() => vitePluginEslint(options), { server: false })
53+
54+
addWebpackPlugin(() => {
55+
const webpackOptions = {
56+
...options,
57+
context: nuxt.options.srcDir,
58+
files: options.include,
59+
lintDirtyModulesOnly: !options.lintOnStart,
60+
}
61+
62+
delete webpackOptions.include
63+
delete webpackOptions.lintOnStart
64+
65+
return new EslintWebpackPlugin(webpackOptions)
66+
}, { server: false })
767
}

packages/module/src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Import } from 'unimport'
2+
import type { Options as ViteCheckerOptions } from 'vite-plugin-eslint'
3+
import type { Options as WebpackCheckerOptions } from 'eslint-webpack-plugin'
24

35
export interface ConfigGenOptions {
46
/**
@@ -11,7 +13,7 @@ export interface ConfigGenOptions {
1113
standalone?: boolean
1214
}
1315

14-
export interface CheckerOptions {
16+
export interface CheckerOptions extends ViteCheckerOptions, WebpackCheckerOptions {
1517
/**
1618
* Checking matched files on start
1719
*/

0 commit comments

Comments
 (0)