|
1 | 1 | /* eslint-disable @typescript-eslint/no-unused-vars */
|
2 | 2 | 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' |
4 | 9 |
|
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 }) |
7 | 67 | }
|
0 commit comments