Skip to content

Commit

Permalink
fix: clean previous console
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 14, 2021
1 parent 5322714 commit fa47e77
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
6 changes: 6 additions & 0 deletions examples/react-ts/src/NotUsed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React, { useState } from 'react'

export const NotUsedComponentButForTestConsoleClear = () => {
const [] = useState<string>()
return <div>233</div>
}
1 change: 1 addition & 0 deletions packages/vite-plugin-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"homepage": "https://github.com/fi3ework/vite-plugin-checker",
"dependencies": {
"@babel/code-frame": "^7.12.13",
"log-update": "^4.0.0",
"npm-run-path": "^4.0.1",
"strip-ansi": "^6.0.0",
"tiny-invariant": "^1.1.0",
Expand Down
37 changes: 25 additions & 12 deletions packages/vite-plugin-checker/src/checkers/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import invariant from 'tiny-invariant'
import ts from 'typescript'
import { ErrorPayload } from 'vite'
import { isMainThread, parentPort } from 'worker_threads'
import logUpdate from 'log-update'

import { ensureCall, formatHost, tsDiagnosticToViteError } from '../utils'
import { createScript } from '../worker'
Expand Down Expand Up @@ -45,26 +46,33 @@ const createDiagnostic: CreateDiagnostic<Pick<PluginConfig, 'typescript'>> = (ch
)
}

let logChunk: { diagnostics: string | null; message: string | null } = {
diagnostics: null,
message: null,
}

// https://github.com/microsoft/TypeScript/blob/a545ab1ac2cb24ff3b1aaf0bfbfb62c499742ac2/src/compiler/watch.ts#L12-L28
const reportDiagnostic = (diagnostic: ts.Diagnostic) => {
const originalDiagnostic = ts.formatDiagnosticsWithColorAndContext([diagnostic], formatHost)
const formattedDiagnostics = ts.formatDiagnosticsWithColorAndContext(
[diagnostic],
formatHost
)

if (!currErr) {
currErr = tsDiagnosticToViteError(diagnostic)
}

ensureCall(() => {
ts.sys.write(originalDiagnostic)
})
logChunk.diagnostics = formattedDiagnostics
}

const reportWatchStatusChanged: ts.WatchStatusReporter = (
diagnostic
// newLine,
// options,
// errorCount
diagnostic,
newLine,
options,
errorCount
// eslint-disable-next-line max-params
) => {
// logChunk = []
// https://github.com/microsoft/TypeScript/issues/32542
switch (diagnostic.code) {
case 6032: // Incremental build
Expand All @@ -83,11 +91,16 @@ const createDiagnostic: CreateDiagnostic<Pick<PluginConfig, 'typescript'>> = (ch
},
})
}

ensureCall(() => {
ts.sys.write(os.EOL + os.EOL + diagnostic.messageText.toString())
})
}
ensureCall(() => {
const diagnosticMessage = os.EOL + os.EOL + diagnostic.messageText.toString()
logChunk.message = diagnosticMessage
if (errorCount === 0) {
logChunk.diagnostics = null
}
const d = logChunk.diagnostics === null ? '' : logChunk.diagnostics + os.EOL
logUpdate(d + logChunk.message)
})
}

// https://github.com/microsoft/TypeScript/issues/32385
Expand Down
11 changes: 2 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fa47e77

Please sign in to comment.