Skip to content

Commit

Permalink
feat: stdout formatted ts diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Mar 29, 2021
1 parent 4025c4f commit 96453a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
13 changes: 6 additions & 7 deletions src/apiMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function toViteError(d: ts.Diagnostic): ErrorPayload['err'] {
* This is mainly for messages like "Starting compilation" or "Compilation completed".
*/
export function createDiagnosis(userOptions: Partial<DiagnoseOptions> = {}) {
let overlay = true // Vite default to true
let overlay = true // Vite defaults to true
let currErr: ErrorPayload['err'] | null = null

return {
Expand All @@ -89,13 +89,10 @@ export function createDiagnosis(userOptions: Partial<DiagnoseOptions> = {}) {
throw new Error("Could not find a valid 'tsconfig.json'.")
}

// https://github.com/microsoft/TypeScript/blob/a545ab1ac2cb24ff3b1aaf0bfbfb62c499742ac2/src/compiler/watch.ts#L12-L28
const reportDiagnostic = (diagnostic: ts.Diagnostic) => {
console.error(
'Error',
diagnostic.code,
':',
ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine())
)
const originalDiagnostic = ts.formatDiagnosticsWithColorAndContext([diagnostic], formatHost)
ts.sys.write(originalDiagnostic)

if (!currErr) {
currErr = toViteError(diagnostic)
Expand Down Expand Up @@ -123,6 +120,8 @@ export function createDiagnosis(userOptions: Partial<DiagnoseOptions> = {}) {
err: currErr,
})
}

ts.sys.write(os.EOL + os.EOL + diagnostic.messageText.toString())
}
}

Expand Down
29 changes: 13 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
// import ts from 'typescript'
import { Plugin } from 'vite'
import { tscProcess } from './cliMode'

import { diagnose } from './apiMode'
import { tscProcess } from './cliMode'

interface PluginOptions {
/**
* Whether to use vue-tsc to check .vue file.
* @default !!import('vue-tsc')
*/
vueTsc?: boolean
/**
*
* Use tsc or vue-tsc
* @default !!import.resolve('vue-tsc')
*/
displayMode?: 'spawn' | 'exec'
vueTsc: boolean
/**
*
* Show TypeScript error overlay
* @default Vite config `server.hmr.overlay`, can be override by
*/
errorOverlay?: boolean
overlay: boolean
/**
*
* WIP
*/
mode?: 'cli' | 'api'
mode: 'cli' | 'api'
}

export function plugin(userOptions?: PluginOptions): Plugin {
export function plugin(userOptions?: Partial<PluginOptions>): Plugin {
let hasVueTsc = false
try {
require.resolve('vue-tsc')
hasVueTsc = true
} catch {}

const mode = userOptions?.mode || 'api'
const mode = userOptions?.mode ?? 'api'

return {
name: 'fork-ts-checker',
config: (config) => {
if (mode === 'cli') {
tscProcess.config(config)
} else {
// diagnose.config(config)
diagnose.config(config)
}
},
configureServer(server) {
Expand Down

0 comments on commit 96453a5

Please sign in to comment.