Skip to content

Commit ca388f2

Browse files
committed
add debounce to delay check run multiple times in a short time
1 parent f65aa71 commit ca388f2

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

Diff for: docs/checkers/eslint.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ Advanced object configuration table of `options.eslint`
3737
| 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. |
3838
| 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, })`. |
3939
| dev.logLevel | `('error' \| 'warning')[]` | `['error', 'warning']` | **(Only in dev mode)** Which level of ESLint should be emitted to terminal and overlay in dev mode |
40+
debounceTime | `number` | `undefined` | Avoid repeated formatting files when your editor set lint on save action,and avoid multiple check in a short time |

Diff for: docs/checkers/typescript.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ Advanced object configuration table of `options.typescript`.
2323
| root | `string` | [Vite config](https://vitejs.dev/config/#root) `root` | Root path to find tsconfig file |
2424
| tsconfigPath | `string` | `"tsconfig.json"` | Relative tsconfig path to `root` |
2525
| 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)) |
26+
debounceTime | `number` | `undefined` | Avoid multiple inspections in a short time |

Diff for: packages/vite-plugin-checker/src/checkers/eslint/main.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const manager = new FileDiagnosticManager()
2525
let createServeAndBuild
2626

2727
import type { CreateDiagnostic } from '../../types'
28+
import debounce from 'lodash.debounce'
2829
const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => {
2930
let overlay = true
3031
let terminal = true
@@ -119,9 +120,21 @@ const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => {
119120
ignored: (path: string) => path.includes('node_modules'),
120121
})
121122
watcher.add(files)
122-
watcher.on('change', async (filePath) => {
123-
handleFileChange(filePath, 'change')
124-
})
123+
// onchange:create debounce function before useage
124+
if (pluginConfig.eslint?.debounceTime) {
125+
const debounceHandleFileChange = debounce(
126+
handleFileChange,
127+
pluginConfig.eslint.debounceTime
128+
)
129+
watcher.on('change', async (filePath) => {
130+
debounceHandleFileChange(filePath, 'change')
131+
})
132+
} else {
133+
watcher.on('change', async (filePath) => {
134+
handleFileChange(filePath, 'change')
135+
})
136+
}
137+
// unlink
125138
watcher.on('unlink', async (filePath) => {
126139
handleFileChange(filePath, 'unlink')
127140
})

Diff for: packages/vite-plugin-checker/src/checkers/typescript/main.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import debounce from 'lodash.debounce'
12
import os from 'os'
23
import path from 'path'
34
import invariant from 'tiny-invariant'
@@ -121,13 +122,21 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
121122

122123
ts.createSolutionBuilderWithWatch(host, [configFile], {}).build()
123124
} else {
125+
// onchange:add debounce
126+
let debounceReportWatchStatusChanged = reportWatchStatusChanged
127+
if (typeof pluginConfig.typescript === 'object' && pluginConfig.typescript?.debounceTime) {
128+
debounceReportWatchStatusChanged = debounce(
129+
reportWatchStatusChanged,
130+
pluginConfig.typescript.debounceTime
131+
)
132+
}
124133
const host = ts.createWatchCompilerHost(
125134
configFile,
126135
{ noEmit: true },
127136
ts.sys,
128137
createProgram,
129138
reportDiagnostic,
130-
reportWatchStatusChanged
139+
debounceReportWatchStatusChanged
131140
)
132141

133142
ts.createWatchProgram(host)

Diff for: packages/vite-plugin-checker/src/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ interface TsConfigOptions {
1919
* root path of cwd
2020
*/
2121
buildMode: boolean
22+
/**
23+
* tsc will delay running,implement through debounce
24+
*/
25+
debounceTime?: number
2226
}
2327

2428
/**
@@ -58,6 +62,10 @@ export type EslintConfig =
5862
/** which level of the diagnostic will be emitted from plugin */
5963
logLevel: ('error' | 'warning')[]
6064
}>
65+
/**
66+
* lintCommand will delay running, work with editor lint on save,implement through debounce
67+
*/
68+
debounceTime?: number
6169
}
6270

6371
/** Stylelint checker configuration */

0 commit comments

Comments
 (0)