diff --git a/.changeset/tidy-ways-hope.md b/.changeset/tidy-ways-hope.md new file mode 100644 index 00000000..347cce75 --- /dev/null +++ b/.changeset/tidy-ways-hope.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-checker': minor +--- + +Add support for include and exclude paths in typescript configuration diff --git a/docs/checkers/typescript.md b/docs/checkers/typescript.md index bd860940..03c29b61 100644 --- a/docs/checkers/typescript.md +++ b/docs/checkers/typescript.md @@ -18,8 +18,10 @@ You can use TypeScript checker for vanilla TypeScript project or React project. Advanced object configuration table of `options.typescript`. -| field | Type | Default value | Description | -| :----------- | --------- | ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| 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)) | +| field | Type | Default value | Description | +| :----------- | --------------------------------- | ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| 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)) | +| include | `string \| string[] \| undefined` | `undefined` | Directories to include in typechecking. Omit this option to include all directories except the ones in specified in `exclude`. | +| exclude | `string \| string[] \| undefined` | `undefined` | Directories to exclude from typechecking. | diff --git a/packages/vite-plugin-checker/src/checkers/typescript/main.ts b/packages/vite-plugin-checker/src/checkers/typescript/main.ts index 485d1b65..8af04b31 100644 --- a/packages/vite-plugin-checker/src/checkers/typescript/main.ts +++ b/packages/vite-plugin-checker/src/checkers/typescript/main.ts @@ -24,6 +24,21 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => { let overlay = true let terminal = true let currDiagnostics: DiagnosticToRuntime[] = [] + let includePaths: string[] | undefined + let excludePaths: string[] | undefined + + const resolveIncludeExcludePaths = (rootDir: string) => { + if (!pluginConfig.typescript || pluginConfig.typescript === true) { + return + } + const { include, exclude } = pluginConfig.typescript + + const includeArr = typeof include === 'string' ? [include] : include + const excludeArr = typeof exclude === 'string' ? [exclude] : exclude + + includePaths = includeArr?.map((includePath) => path.resolve(rootDir, includePath)) + excludePaths = excludeArr?.map((excludePath) => path.resolve(rootDir, excludePath)) + } return { config: async ({ enableOverlay, enableTerminal }) => { @@ -59,6 +74,18 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => { return } + const filename = diagnostic.file?.fileName + if (filename) { + if ( + includePaths && + !includePaths.some((includePath) => filename.startsWith(includePath)) + ) { + return + } + if (excludePaths?.some((excludePath) => filename.startsWith(excludePath))) { + return + } + } currDiagnostics.push(diagnosticToRuntimeError(normalizedDiagnostic)) logChunk += os.EOL + diagnosticToTerminalLog(normalizedDiagnostic, 'TypeScript') } @@ -92,7 +119,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => { } ensureCall(() => { - if (errorCount === 0) { + if (currDiagnostics.length === 0) { logChunk = '' } @@ -100,7 +127,10 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => { consoleLog( logChunk + os.EOL + - wrapCheckerSummary('TypeScript', diagnostic.messageText.toString()) + wrapCheckerSummary( + 'TypeScript', + `Found ${currDiagnostics.length} errors. Waiting for file changes.` + ) ) } }) @@ -119,6 +149,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => { reportWatchStatusChanged ) + resolveIncludeExcludePaths(host.getCurrentDirectory()) ts.createSolutionBuilderWithWatch(host, [configFile], {}).build() } else { const host = ts.createWatchCompilerHost( @@ -130,6 +161,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => { reportWatchStatusChanged ) + resolveIncludeExcludePaths(host.getCurrentDirectory()) ts.createWatchProgram(host) } }, diff --git a/packages/vite-plugin-checker/src/types.ts b/packages/vite-plugin-checker/src/types.ts index 37a2c346..ebdef64a 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 + /** + * include directories in typechecking + */ + include: string | string[] + /** + * exclude directories from typechecking + */ + exclude: string | string[] } /**