@modyqyw/vite-plugin-eslint
Vite ESLint plugin. Supports Vite v2, v3 and v4. Requires node >= 14.18
.
You may want Vite Stylelint plugin.
Install
npm install @modyqyw/vite-plugin-eslint -D
@modyqyw/vite-plugin-eslint
does not install and config ESLint for you. You should handle these yourself.
ESLint@7
npm install eslint@^7 @types/eslint@^7 -D
ESLint@8
npm install eslint@^8 @types/eslint@^8 -D
Usage
// vite.config.ts
import { defineConfig } from 'vite';
import eslint from '@modyqyw/vite-plugin-eslint';
export default defineConfig({
plugins: [eslint(options)],
});
Options
You can pass ESLint Node.js API constructor options to the plugin.
// vite.config.ts
import { defineConfig } from 'vite';
import eslint from 'vite-plugin-eslint';
export default defineConfig({
plugins: [
eslint({
// recommend to enable auto fix
fix: true,
...,
}),
],
});
Additional options and explanations are listed below.
dev
- Type:
boolean
- Default:
true
Run ESLint under serve
command. See Command Line Interface for more.
build
- Type:
boolean
- Default:
false
Run ESLint under build
command. See Command Line Interface for more.
cache
- Type:
boolean
- Default:
true
Store the results of processed files when enabled. This is enabled by default to improve speed.
cacheLocation
- Type:
string
- Default:
.eslintcache
Path to a file or directory for the cache location. .eslintcache
is the default cache location of ESLint.
include
- Type:
string | string[]
- Default:
['src/**/*.{js,jsx,ts,tsx,vue,svelte}']
A valid picomatch pattern, or array of patterns.
This is used to create a filter to determine eslint.lintFiles
params.
You may want to change this option if you are using nuxt
.
nuxt example
// nuxt.config.ts
import viteEslint from 'vite-plugin-eslint';
export default defineNuxtConfig({
vite: {
plugins: [
viteEslint({
...,
include: [
'components/**/*.{js,jsx,ts,tsx,vue}',
'composables/**/*.{js,jsx,ts,tsx,vue}',
'constants/**/*.{js,jsx,ts,tsx,vue}',
'content/**/*.{js,jsx,ts,tsx,vue}',
'helpers/**/*.{js,jsx,ts,tsx,vue}',
'layouts/**/*.{js,jsx,ts,tsx,vue}',
'middleware/**/*.{js,jsx,ts,tsx,vue}',
'middlewares/**/*.{js,jsx,ts,tsx,vue}',
'pages/**/*.{js,jsx,ts,tsx,vue}',
'plugins/**/*.{js,jsx,ts,tsx,vue}',
'server/**/*.{js,jsx,ts,tsx,vue}',
'src/**/*.{js,jsx,ts,tsx,vue}',
'stores/**/*.{js,jsx,ts,tsx,vue}',
'styles/**/*.{js,jsx,ts,tsx,vue}',
'utils/**/*.{js,jsx,ts,tsx,vue}',
'app.vue',
'App.vue',
'error.vue',
'Error.vue',
'app.config.ts',
'nuxt.config.ts',
],
}),
],
},
});
exclude
- Type:
string | string[]
- Default:
['node_modules', 'virtual:']
A valid picomatch pattern, or array of patterns.
This is used to create a filter to determine eslint.lintFiles
params.
eslintPath
- Type:
string
- Default:
'eslint'
Path to ESLint that will be used for linting. Use dynamic import under the hood. Read vite server.fs options first.
formatter
- Type:
string
- Default:
'stylish'
The name or the path of a formatter.
This is used to load a formatter in order to convert lint results to a human- or machine-readable string.
lintInWorker
- Type:
boolean
- Default:
false
Lint in worker. This is disabled by default.
When lint in worker, Vite build process will be faster. Vite build process will not be stopped, even with errors shown in terminal.
It is similar with vite-plugin-checker, but vite-plugin-checker can show you errors and warnings in browsers.
lintOnStart
- Type:
boolean
- Default:
false
Lint on start (in buildStart
hook). Useful to lint all files once to find potential errors. This is disabled by default.
This will significantly slow down Vite first starting if you has no caches and don't enable lintInWorker
.
emitError
- Type:
boolean
- Default:
true
The errors found will be emitted when enabled. This is enabled by default.
emitErrorAsWarning
- Type:
boolean
- Default:
false
The errors found will be emitted as warnings when enabled. This is disabled by default but you may want it enabled when prototyping.
emitWarning
- Type:
boolean
- Default:
true
The warnings found will be emitted when enabled. This is enabled by default.
emitWarningAsError
- Type:
boolean
- Default:
false
The warnings found will be emitted as errors when enabled. This is disabled by default.
FAQ
Cache is broken
- Disable
cache
option. - Or delete the cache file (default
.eslintcache
), fix errors manully and restart Vite.
Vite
is slow when using this plugin
- Try enable
lintInWorker
option - Or try vite-plugin-checker
- Or run ESLint directly besides Vite
What's the difference between gxmari007/vite-plugin-eslint and this project?
This project is initially forked from gxmari007/vite-plugin-eslint. Because the project looked like dead at that time, leaving issues and PRs. I sent an email to the author but I got no response.
I add some functions to meet my needs, like eslint@8
support, eslintPath
option, lintInWorker
option, lintOnStart
option and ignore virtual modules by default.
I will still keep updating this project. Feel free to choose one.
Examples
See examples.
CHANGELOG
See CHANGELOG.md.
Acknowledge
Initially forked from gxmari007/vite-plugin-eslint.
License
MIT