Skip to content

Commit

Permalink
feat: work in buld mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Mar 31, 2021
1 parent 17d5d3b commit 88c3662
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ npm run dev
### Roadmap

- [x] release alpha version
- [ ] support build mode
- [x] support build mode
- [ ] custom command
- [ ] custom tsconfig path
- [ ] no tsconfig file error
- [ ] examples (codesandbox?)
- [x] examples (codesandbox?)
- [ ] release stable version

## License
Expand Down
2 changes: 1 addition & 1 deletion examples/vue-ts/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import TsChecker from 'vite-plugin-ts-checker'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), TsChecker()],
plugins: [vue(), TsChecker({ checker: 'vue-tsc' })],
})
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"homepage": "https://github.com/fi3ework/vite-plugin-ts-checker",
"dependencies": {
"@babel/code-frame": "^7.12.13",
"async-exit-hook": "^2.0.1",
"log-update": "^4.0.0",
"npm-run-path": "^4.0.1",
"strip-ansi": "^6.0.0"
},
"peerDependencies": {
Expand Down
12 changes: 10 additions & 2 deletions pnpm-lock.yaml

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

58 changes: 41 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { Plugin } from 'vite'
import { spawn, exec } from 'child_process'
import npmRunPath from 'npm-run-path'
import { ConfigEnv, Plugin } from 'vite'

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

const exitHook = require('async-exit-hook')

interface PluginOptions {
/**
* [WIP] Use `tsc` or `vue-tsc`
* @default if vue-tsc is installed, then true, otherwise false
* Use `tsc` or `vue-tsc`
* @default 'tcs'
*/
checker: 'tsc' | 'vue-tsc'
/**
* Throw error in build mode
* @default false
*/
vueTsc: boolean
ignoreInBuild: boolean
/**
* Show TypeScript error overlay
* @default Same as Vite config - `server.hmr.overlay`
Expand All @@ -27,38 +36,53 @@ interface PluginOptions {
* @default if `vueTsc` is true, then force set to 'cli', otherwise default to 'api'
*/
mode: 'cli' | 'api'
/**
* Run in build mode ()
* @default true
*/
build: boolean | {}
}

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

const mode = userOptions?.mode ?? 'api'
const checker = userOptions?.checker ?? 'tsc'
const ignoreOnBuild = userOptions?.ignoreInBuild ?? false
let viteMode: ConfigEnv['command'] | undefined

return {
name: 'ts-checker',
config: (config) => {
config: (config, { command, mode }) => {
viteMode = command
if (mode === 'cli') {
tscProcess.config(config)
} else {
diagnose.config(config)
}
},
buildStart: (options) => {
if (viteMode === 'build') {
const localEnv = npmRunPath.env({
env: process.env,
cwd: process.cwd(),
execPath: process.execPath,
})

const proc = spawn(checker, ['--noEmit'], {
cwd: process.cwd(),
stdio: 'inherit',
env: localEnv,
})

if (!ignoreOnBuild) {
proc.on('exit', (code) => {
if (code !== null && code !== 0) {
process.exit(code)
}
})
}
}
},
configureServer(server) {
if (mode === 'cli') {
tscProcess.configureServer(server)
} else {
diagnose.configureServer(server)
}

return () => {
server.middlewares.use((req, res, next) => {
next()
Expand Down

0 comments on commit 88c3662

Please sign in to comment.