From 97ef384198eeae54f6d6849d7aa8564bed4d45ff Mon Sep 17 00:00:00 2001 From: HonLuk Date: Mon, 6 Feb 2023 12:00:15 +0800 Subject: [PATCH 1/5] add debounce to delay check run multiple times in a short time --- docs/checkers/eslint.md | 1 + docs/checkers/typescript.md | 1 + .../src/checkers/eslint/main.ts | 19 ++++++++++++++++--- .../src/checkers/typescript/main.ts | 11 ++++++++++- packages/vite-plugin-checker/src/types.ts | 12 ++++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/docs/checkers/eslint.md b/docs/checkers/eslint.md index b17d2cc9..0f6d0c6a 100644 --- a/docs/checkers/eslint.md +++ b/docs/checkers/eslint.md @@ -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 | \ No newline at end of file diff --git a/docs/checkers/typescript.md b/docs/checkers/typescript.md index bd860940..7dcd2616 100644 --- a/docs/checkers/typescript.md +++ b/docs/checkers/typescript.md @@ -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 | \ No newline at end of file diff --git a/packages/vite-plugin-checker/src/checkers/eslint/main.ts b/packages/vite-plugin-checker/src/checkers/eslint/main.ts index 340eddd2..1d87e6ec 100644 --- a/packages/vite-plugin-checker/src/checkers/eslint/main.ts +++ b/packages/vite-plugin-checker/src/checkers/eslint/main.ts @@ -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 @@ -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 (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') }) diff --git a/packages/vite-plugin-checker/src/checkers/typescript/main.ts b/packages/vite-plugin-checker/src/checkers/typescript/main.ts index 13d01faa..1eae8f98 100644 --- a/packages/vite-plugin-checker/src/checkers/typescript/main.ts +++ b/packages/vite-plugin-checker/src/checkers/typescript/main.ts @@ -1,3 +1,4 @@ +import debounce from 'lodash.debounce' import os from 'os' import path from 'path' import invariant from 'tiny-invariant' @@ -121,13 +122,21 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => { ts.createSolutionBuilderWithWatch(host, [configFile], {}).build() } else { + // onchange:add debounce + let debounceReportWatchStatusChanged = reportWatchStatusChanged + 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) diff --git a/packages/vite-plugin-checker/src/types.ts b/packages/vite-plugin-checker/src/types.ts index 1faf45e8..4f95f900 100644 --- a/packages/vite-plugin-checker/src/types.ts +++ b/packages/vite-plugin-checker/src/types.ts @@ -19,6 +19,14 @@ interface TsConfigOptions { * root path of cwd */ buildMode: boolean + + dev?: Partial<{ + /** + * tsc will delay running,implement through debounce + */ + debounce: number + }> + 'dev.debounce'?: number } /** @@ -57,6 +65,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 + */ + debounce: number }> } From 2ce83bd0d03f770731c749f34f247f71a5f570dc Mon Sep 17 00:00:00 2001 From: HonLuk Date: Mon, 6 Feb 2023 12:09:48 +0800 Subject: [PATCH 2/5] fixed option types and doc --- docs/checkers/typescript.md | 2 +- packages/vite-plugin-checker/src/types.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/checkers/typescript.md b/docs/checkers/typescript.md index 7dcd2616..fea1a37e 100644 --- a/docs/checkers/typescript.md +++ b/docs/checkers/typescript.md @@ -23,4 +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 | \ No newline at end of file +dev.debounce | `number` | `undefined` | Avoid multiple checks in a short time when your editor set lint on save action | \ No newline at end of file diff --git a/packages/vite-plugin-checker/src/types.ts b/packages/vite-plugin-checker/src/types.ts index 4f95f900..82314af0 100644 --- a/packages/vite-plugin-checker/src/types.ts +++ b/packages/vite-plugin-checker/src/types.ts @@ -26,7 +26,6 @@ interface TsConfigOptions { */ debounce: number }> - 'dev.debounce'?: number } /** From 3dd9dd1453dca2313568a090f5adfeb301de1c88 Mon Sep 17 00:00:00 2001 From: HonLuk Date: Mon, 6 Feb 2023 12:18:47 +0800 Subject: [PATCH 3/5] add eslint config object type check as others --- packages/vite-plugin-checker/src/checkers/eslint/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite-plugin-checker/src/checkers/eslint/main.ts b/packages/vite-plugin-checker/src/checkers/eslint/main.ts index 1d87e6ec..68693be8 100644 --- a/packages/vite-plugin-checker/src/checkers/eslint/main.ts +++ b/packages/vite-plugin-checker/src/checkers/eslint/main.ts @@ -121,7 +121,7 @@ const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => { }) watcher.add(files) // onchange:create debounce function before useage - if (pluginConfig.eslint?.dev?.debounce) { + if (typeof pluginConfig.eslint === 'object' && pluginConfig.eslint.dev?.debounce) { const debounceHandleFileChange = debounce( handleFileChange, pluginConfig.eslint.dev.debounce From 55de724eb4d2c3f87921a15b8d4a5d7563d133e2 Mon Sep 17 00:00:00 2001 From: HonLuk Date: Fri, 16 Jun 2023 22:33:13 +0800 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Nick Ribal --- docs/checkers/eslint.md | 2 +- packages/vite-plugin-checker/src/types.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/checkers/eslint.md b/docs/checkers/eslint.md index 0f6d0c6a..c56a7f5f 100644 --- a/docs/checkers/eslint.md +++ b/docs/checkers/eslint.md @@ -37,4 +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 | \ No newline at end of file +dev.debounce | `number` | `undefined` | Avoid repeated formatting files when your editor set lint on save action and avoid multiple checks in a short time | \ No newline at end of file diff --git a/packages/vite-plugin-checker/src/types.ts b/packages/vite-plugin-checker/src/types.ts index 82314af0..144bc903 100644 --- a/packages/vite-plugin-checker/src/types.ts +++ b/packages/vite-plugin-checker/src/types.ts @@ -22,7 +22,7 @@ interface TsConfigOptions { dev?: Partial<{ /** - * tsc will delay running,implement through debounce + * tsc will delay running, implemented via debounce */ debounce: number }> @@ -65,7 +65,7 @@ export type EslintConfig = /** 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 + * lintCommand will delay running, work with editor lint on save, implemented via debounce */ debounce: number }> From 1a37cf42bf5e916e60d80f2c8dd5735de4396cad Mon Sep 17 00:00:00 2001 From: HonLuk Date: Fri, 16 Jun 2023 23:10:08 +0800 Subject: [PATCH 5/5] docs: add dev mode remind and accept accept suggestions --- docs/checkers/eslint.md | 2 +- docs/checkers/typescript.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/checkers/eslint.md b/docs/checkers/eslint.md index c56a7f5f..bff3ddf0 100644 --- a/docs/checkers/eslint.md +++ b/docs/checkers/eslint.md @@ -37,4 +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 | \ No newline at end of file +dev.debounce | `number` | `undefined` | **(Only in dev mode)** The milliseconds for debounce. Avoid repeated formatting files when your editor set lint on save action, and avoid multiple checks in a short time | \ No newline at end of file diff --git a/docs/checkers/typescript.md b/docs/checkers/typescript.md index fea1a37e..cd928628 100644 --- a/docs/checkers/typescript.md +++ b/docs/checkers/typescript.md @@ -23,4 +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 | \ No newline at end of file +dev.debounce | `number` | `undefined` | **(Only in dev mode)** The milliseconds for debounce. Avoid multiple checks in a short time when your editor set lint on save action | \ No newline at end of file