Skip to content

Commit

Permalink
fix: add simple vue-tsc
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 10, 2021
1 parent 89e87e8 commit 126b433
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 104 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ Add `vite-plugin-ts-checker` to plugin filed of Vite config file.

```ts
// vite.config.js
import TsChecker from 'vite-plugin-ts-checker'
import Checker from 'vite-plugin-ts-checker'

export default {
plugins: [TsChecker()],
// or with advanced options `plugins: [TsChecker({ ...options })]`
plugins: [Checker()],
// or with advanced options `plugins: [Checker({ ...options })]`
// see options for detail
}
```
Expand Down Expand Up @@ -61,12 +61,12 @@ npm i vite-plugin-ts-checker-preset-vls -D
2. Modify config file

```js
import TsChecker from 'vite-plugin-ts-checker'
import Checker from 'vite-plugin-ts-checker'
import VlsChecker from 'vite-plugin-ts-checker-preset-vls'

module.exports = {
plugins: [
TsChecker({
Checker({
checker: VlsChecker(/** VLS options */),
}),
],
Expand Down
6 changes: 5 additions & 1 deletion examples/react-ts/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
import TsChecker from 'vite-plugin-ts-checker'
import Checker from 'vite-plugin-ts-checker'

// https://vitejs.dev/config/
export default defineConfig({
<<<<<<< HEAD
plugins: [reactRefresh(), TsChecker({overlay: false})],
=======
plugins: [reactRefresh(), Checker()],
>>>>>>> fix: add simple vue-tsc
})
4 changes: 2 additions & 2 deletions examples/vue2-ts/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'vite'
import { createVuePlugin } from 'vite-plugin-vue2'
import ViteComponents from 'vite-plugin-components'
import TsChecker from 'vite-plugin-ts-checker'
import Checker from 'vite-plugin-ts-checker'
import vlsChecker from 'vite-plugin-ts-checker-preset-vls'
import { resolve } from 'path'

Expand All @@ -18,7 +18,7 @@ const config = defineConfig({
plugins: [
createVuePlugin({}),
ViteComponents({ transformer: 'vue2' }),
TsChecker({
Checker({
checker: vlsChecker(),
}),
],
Expand Down
4 changes: 2 additions & 2 deletions examples/vue3-ts/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import TsChecker from 'vite-plugin-ts-checker'
import Checker from 'vite-plugin-ts-checker'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), TsChecker({ checker: 'vue-tsc' })],
plugins: [vue(), Checker({ checker: 'vue-tsc' })],
})
9 changes: 2 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ function makeChecker(
checker: PluginOptions['checker'],
userOptions?: Partial<PluginOptions>
): Checker {
if (checker === 'tsc') {
// TODO: better use import.meta.require
if (typeof checker === 'string') {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const tscCheckerFactory = require('./presets/tsc').tsCheckerFactory
const tscCheckerFactory = require(`./presets/${checker}`).checkerFactory
return tscCheckerFactory(userOptions)
} else if (checker === 'vue-tsc') {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const vueTscCheckerFactory = require('./presets/tsc').tsCheckerFactory
return vueTscCheckerFactory(userOptions)
} else {
return checker
}
Expand Down
2 changes: 1 addition & 1 deletion src/presets/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const createDiagnostic: CreateDiagnostic = (userOptions = {}) => {
}
}

export const tsCheckerFactory: CheckerFactory = () => {
export const checkerFactory: CheckerFactory = () => {
return {
buildBin: ['tsc', ['--noEmit']],
createDiagnostic,
Expand Down
90 changes: 4 additions & 86 deletions src/presets/vue-tsc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import os from 'os'
import ts from 'typescript'
import { ErrorPayload } from 'vite'

import { CreateDiagnostic } from '../types'
import { ensureCall, formatHost, tsDiagnosticToViteError } from '../utils'

import type { CheckerFactory } from '../types'
import type { UserConfig, ViteDevServer } from 'vite'
Expand All @@ -13,96 +8,19 @@ import type { UserConfig, ViteDevServer } from 'vite'
* This is mainly for messages like "Starting compilation" or "Compilation completed".
*/
export const createDiagnostic: CreateDiagnostic = (userOptions = {}) => {
let overlay = true // Vite defaults to true
let currErr: ErrorPayload['err'] | null = null

return {
config: (config: UserConfig) => {
const hmr = config.server?.hmr
const viteOverlay = !(typeof hmr === 'object' && hmr.overlay === false)

if (userOptions.overlay === false || !viteOverlay) {
overlay = false
}
// TODO: watch mode is note supported
},
configureServer(server: ViteDevServer) {
const finalConfig = {
root: userOptions.root ?? server.config.root,
tsconfigPath: userOptions.tsconfigPath ?? 'tsconfig.json',
}

let configFile: string | undefined

configFile = ts.findConfigFile(finalConfig.root, ts.sys.fileExists, finalConfig.tsconfigPath)

if (configFile === undefined) {
throw Error(
`Failed to find a valid tsconfig.json: ${finalConfig.tsconfigPath} at ${finalConfig.root} is not a valid tsconfig`
)
}

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

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

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

const reportWatchStatusChanged: ts.WatchStatusReporter = (
diagnostic
// newLine,
// options,
// errorCount
// eslint-disable-next-line max-params
) => {
// https://github.com/microsoft/TypeScript/issues/32542
switch (diagnostic.code) {
case 6032: // Incremental build
// clear current error and use the newer errors
currErr = null
break
case 6031: // Initial build
case 6193: // 1 Error
case 6194: // 0 errors or 2+ errors
if (currErr && overlay) {
server.ws.send({
type: 'error',
err: currErr,
})
}

ensureCall(() => {
ts.sys.write(os.EOL + os.EOL + diagnostic.messageText.toString())
})
}
}

// https://github.com/microsoft/TypeScript/issues/32385
// https://github.com/microsoft/TypeScript/pull/33082/files
const createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram
const host = ts.createWatchCompilerHost(
configFile,
{ noEmit: true },
ts.sys,
createProgram,
reportDiagnostic,
reportWatchStatusChanged
)

ts.createWatchProgram(host)
// TODO: watch mode is note supported
},
}
}

export const tsCheckerFactory: CheckerFactory = () => {
export const checkerFactory: CheckerFactory = () => {
return {
buildBin: ['tsc', ['--noEmit']],
buildBin: ['vue-tsc', ['--noEmit']],
createDiagnostic: createDiagnostic,
}
}
Expand Down

0 comments on commit 126b433

Please sign in to comment.