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 5, 2021
1 parent e85c15b commit fdb56be
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 106 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
4 changes: 2 additions & 2 deletions examples/react-ts/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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({
plugins: [reactRefresh(), TsChecker()],
plugins: [reactRefresh(), Checker()],
})
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
2 changes: 1 addition & 1 deletion examples/vue3-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"build": "vite build",
"serve": "vite preview"
},
"dependencies": {
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 @@ -12,15 +12,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 fdb56be

Please sign in to comment.