Skip to content

Commit 945e9b0

Browse files
authored
feat: allow configuring custom typescriptPath (#296)
1 parent aecae9c commit 945e9b0

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url'
44
import { parentPort } from 'node:worker_threads'
55
import * as colors from 'colorette'
66
import invariant from 'tiny-invariant'
7-
import ts from 'typescript'
7+
import type * as typescript from 'typescript'
88

99
import { Checker } from '../../Checker.js'
1010
import {
@@ -35,19 +35,25 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
3535
overlay = enableOverlay
3636
terminal = enableTerminal
3737
},
38-
configureServer({ root }) {
38+
async configureServer({ root }) {
3939
invariant(pluginConfig.typescript, 'config.typescript should be `false`')
4040
const finalConfig =
4141
pluginConfig.typescript === true
42-
? { root, tsconfigPath: 'tsconfig.json' }
42+
? {
43+
root,
44+
tsconfigPath: 'tsconfig.json',
45+
typescriptPath: 'typescript',
46+
}
4347
: {
4448
root: pluginConfig.typescript.root ?? root,
4549
tsconfigPath:
4650
pluginConfig.typescript.tsconfigPath ?? 'tsconfig.json',
51+
typescriptPath:
52+
pluginConfig.typescript.typescriptPath ?? 'typescript',
4753
}
4854

4955
let configFile: string | undefined
50-
56+
const ts: typeof typescript = await import(finalConfig.typescriptPath)
5157
configFile = ts.findConfigFile(
5258
finalConfig.root,
5359
ts.sys.fileExists,
@@ -63,7 +69,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
6369
let logChunk = ''
6470

6571
// https://github.com/microsoft/TypeScript/blob/a545ab1ac2cb24ff3b1aaf0bfbfb62c499742ac2/src/compiler/watch.ts#L12-L28
66-
const reportDiagnostic = (diagnostic: ts.Diagnostic) => {
72+
const reportDiagnostic = (diagnostic: typescript.Diagnostic) => {
6773
const normalizedDiagnostic = normalizeTsDiagnostic(diagnostic)
6874
if (normalizedDiagnostic === null) {
6975
return
@@ -74,7 +80,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
7480
os.EOL + diagnosticToTerminalLog(normalizedDiagnostic, 'TypeScript')
7581
}
7682

77-
const reportWatchStatusChanged: ts.WatchStatusReporter = (
83+
const reportWatchStatusChanged: typescript.WatchStatusReporter = (
7884
diagnostic,
7985
newLine,
8086
options,

packages/vite-plugin-checker/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ interface TsConfigOptions {
1111
* path to tsconfig.json file
1212
*/
1313
tsconfigPath: string
14+
/**
15+
* path to typescript package
16+
*/
17+
typescriptPath: string
1418
/**
1519
* root path of cwd
1620
*/

0 commit comments

Comments
 (0)