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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for include and exclude paths in typescript configuration #317

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-ways-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-checker': minor
---

Add support for include and exclude paths in typescript configuration
12 changes: 7 additions & 5 deletions docs/checkers/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
36 changes: 34 additions & 2 deletions packages/vite-plugin-checker/src/checkers/typescript/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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')
}
Expand Down Expand Up @@ -92,15 +119,18 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
}

ensureCall(() => {
if (errorCount === 0) {
if (currDiagnostics.length === 0) {
logChunk = ''
}

if (terminal) {
consoleLog(
logChunk +
os.EOL +
wrapCheckerSummary('TypeScript', diagnostic.messageText.toString())
wrapCheckerSummary(
'TypeScript',
`Found ${currDiagnostics.length} errors. Waiting for file changes.`
)
)
}
})
Expand All @@ -119,6 +149,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
reportWatchStatusChanged
)

resolveIncludeExcludePaths(host.getCurrentDirectory())
ts.createSolutionBuilderWithWatch(host, [configFile], {}).build()
} else {
const host = ts.createWatchCompilerHost(
Expand All @@ -130,6 +161,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
reportWatchStatusChanged
)

resolveIncludeExcludePaths(host.getCurrentDirectory())
ts.createWatchProgram(host)
}
},
Expand Down
8 changes: 8 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,14 @@ interface TsConfigOptions {
* root path of cwd
*/
buildMode: boolean
/**
* include directories in typechecking
*/
include: string | string[]
/**
* exclude directories from typechecking
*/
exclude: string | string[]
}

/**
Expand Down
Loading