Skip to content

Commit

Permalink
feat: show overlay error under API mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Mar 28, 2021
1 parent 5231531 commit 2ace313
Showing 1 changed file with 59 additions and 16 deletions.
75 changes: 59 additions & 16 deletions src/apiMode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ErrorPayload } from 'vite'
import ts from 'typescript'
import type { UserConfig, ViteDevServer } from 'vite'

import type { UserConfig, ViteDevServer } from 'vite'
interface DiagnoseOptions {
root: string
tsconfigPath: string
Expand All @@ -12,26 +13,30 @@ const formatHost: ts.FormatDiagnosticsHost = {
getNewLine: () => ts.sys.newLine,
}

function reportDiagnostic(diagnostic: ts.Diagnostic) {
console.error(
'Error',
diagnostic.code,
':',
ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine())
)
function toViteError(diagnostic: ts.Diagnostic): ErrorPayload['err'] {
return {
message: 'string',
stack: 'a/b/c/d',
id: 'string',
frame: 'string',
plugin: 'string',
pluginCode: 'string',
// loc?: {
// file?: string
// line: number
// column: number
// }
}
}

/**
* Prints a diagnostic every time the watch status changes.
* This is mainly for messages like "Starting compilation" or "Compilation completed".
*/
function reportWatchStatusChanged(diagnostic: ts.Diagnostic) {
console.info(ts.formatDiagnostic(diagnostic, formatHost))
}

export function createDiagnosis(userOptions: Partial<DiagnoseOptions> = {}) {
let overlay = true // Vite default to true
let err: string | null = null
let currErr: ErrorPayload['err'] | null = null

return {
config: (config: UserConfig) => {
Expand All @@ -57,12 +62,50 @@ export function createDiagnosis(userOptions: Partial<DiagnoseOptions> = {}) {
throw new Error("Could not find a valid 'tsconfig.json'.")
}

// const { config } = ts.readConfigFile(configFile, ts.sys.readFile)
// const { options } = ts.parseJsonConfigFileContent(config, ts.sys, finalConfig.root)
// force --noEmit
// options.noEmit = true
const reportDiagnostic = (diagnostic: ts.Diagnostic) => {
const { file } = diagnostic
console.error(
'Error',
diagnostic.code,
':',
ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine())
)
return toViteError(diagnostic)
}

const reportWatchStatusChanged: ts.WatchStatusReporter = (
diagnostic,
newLine,
options,
errorCount
) => {
// https://github.com/microsoft/TypeScript/issues/32542
console.log(diagnostic)
switch (diagnostic.code) {
case 6031: // Initial build
case 6032: // Incremental build
// clear current error and use the newer error from compiler
currErr = null
break
case 6193: // 1 Error
currErr = toViteError(diagnostic)
case 6194: // 0 errors or 2+ errors
if (errorCount === 0 || errorCount === undefined) {
} else {
if (!currErr) currErr = toViteError(diagnostic)
}

if (currErr) {
server.ws.send({
type: 'error',
err: currErr,
})
}
}
}

// https://github.com/microsoft/TypeScript/issues/32385
// https://github.com/microsoft/TypeScript/pull/33082/files
const createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram
const host = ts.createWatchCompilerHost(
configFile,
Expand Down

0 comments on commit 2ace313

Please sign in to comment.