Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add debounce to delay check run multiple times in a short time #216

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/checkers/eslint.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ Advanced object configuration table of `options.eslint`
| lintCommand | `string` | This value is required | `lintCommand` will be executed at build mode, and will also be used as default config for dev mode when `eslint.dev.eslint` is nullable. |
| dev.overrideConfig | [`ESLint.Options`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/eslint/index.d.ts) | `undefined` | **(Only in dev mode)** You can override the options of the translated from `lintCommand`. Config priority: `const eslint = new ESLint({cwd: root, ...translatedOptions, ...pluginConfig.eslint.dev?.overrideConfig, })`. |
| dev.logLevel | `('error' \| 'warning')[]` | `['error', 'warning']` | **(Only in dev mode)** Which level of ESLint should be emitted to terminal and overlay in dev mode |
dev.debounce | `number` | `undefined` | Avoid repeated formatting files when your editor set lint on save action,and avoid multiple checks in a short time |
HonLuk marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions docs/checkers/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ Advanced object configuration table of `options.typescript`.
| root | `string` | [Vite config](https://vitejs.dev/config/#root) `root` | Root path to find tsconfig file |
| tsconfigPath | `string` | `"tsconfig.json"` | Relative tsconfig path to `root` |
| buildMode | `boolean` | `false` | Add [`--build`](https://www.typescriptlang.org/docs/handbook/project-references.html) to `tsc` flag, note that `noEmit` does NOT work if `buildMode` is `true` ([#36917](https://github.com/microsoft/TypeScript/issues/36917)) |
dev.debounce | `number` | `undefined` | Avoid multiple checks in a short time when your editor set lint on save action |
HonLuk marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 16 additions & 3 deletions packages/vite-plugin-checker/src/checkers/eslint/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const manager = new FileDiagnosticManager()
let createServeAndBuild

import type { CreateDiagnostic } from '../../types'
import debounce from 'lodash.debounce'
const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => {
let overlay = true
let terminal = true
Expand Down Expand Up @@ -119,9 +120,21 @@ const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => {
ignored: (path: string) => path.includes('node_modules'),
})
watcher.add(files)
watcher.on('change', async (filePath) => {
handleFileChange(filePath, 'change')
})
// onchange:create debounce function before useage
if (typeof pluginConfig.eslint === 'object' && pluginConfig.eslint.dev?.debounce) {
const debounceHandleFileChange = debounce(
handleFileChange,
pluginConfig.eslint.dev.debounce
)
watcher.on('change', async (filePath) => {
debounceHandleFileChange(filePath, 'change')
})
} else {
watcher.on('change', async (filePath) => {
handleFileChange(filePath, 'change')
})
}
// unlink
watcher.on('unlink', async (filePath) => {
handleFileChange(filePath, 'unlink')
})
Expand Down
11 changes: 10 additions & 1 deletion packages/vite-plugin-checker/src/checkers/typescript/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import debounce from 'lodash.debounce'
import os from 'os'
import path from 'path'
import invariant from 'tiny-invariant'
Expand Down Expand Up @@ -121,13 +122,21 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {

ts.createSolutionBuilderWithWatch(host, [configFile], {}).build()
} else {
// onchange:add debounce
let debounceReportWatchStatusChanged = reportWatchStatusChanged
HonLuk marked this conversation as resolved.
Show resolved Hide resolved
if (typeof pluginConfig.typescript === 'object' && pluginConfig.typescript?.dev?.debounce) {
debounceReportWatchStatusChanged = debounce(
reportWatchStatusChanged,
pluginConfig.typescript.dev.debounce
)
}
const host = ts.createWatchCompilerHost(
configFile,
{ noEmit: true },
ts.sys,
createProgram,
reportDiagnostic,
reportWatchStatusChanged
debounceReportWatchStatusChanged
)

ts.createWatchProgram(host)
Expand Down
11 changes: 11 additions & 0 deletions packages/vite-plugin-checker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ interface TsConfigOptions {
* root path of cwd
*/
buildMode: boolean

dev?: Partial<{
/**
* tsc will delay running,implement through debounce
HonLuk marked this conversation as resolved.
Show resolved Hide resolved
*/
debounce: number
}>
}

/**
Expand Down Expand Up @@ -57,6 +64,10 @@ export type EslintConfig =
overrideConfig: ESLint.Options
/** which level of the diagnostic will be emitted from plugin */
logLevel: ('error' | 'warning')[]
/**
* lintCommand will delay running, work with editor lint on save,implement through debounce
HonLuk marked this conversation as resolved.
Show resolved Hide resolved
*/
debounce: number
}>
}

Expand Down