Skip to content

Commit

Permalink
fix: checker uses config right
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 10, 2021
1 parent 698879d commit fe6fe37
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 45 deletions.
77 changes: 43 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# vite-plugin-checker

A Vite plugin that runs TypeScript / Vue / ... checker in worker thread.
A Vite plugin that runs TypeScript / Vue / ... checks in worker thread.

## Features

- 📦 Out of the box
- 🌗 Support React & Vue2/3
- ⛔️ Vite HMR overlay
- 🕸 Vite HMR overlay
- 🛠 Support serve & build mode

<p align="center">
<img alt="screenshot" src="https://user-images.githubusercontent.com/12322740/113175704-48cf1e80-927e-11eb-9bb5-43ab1b218cb2.png">
</p>

## Install
## Getting Started

### Install plugin

Expand All @@ -34,9 +34,11 @@ export default {
}
```

## Getting Started
## Framework related configuration

> See detailed configuration in [advanced config](#advanced-config) section.
### React
### React / Vanilla TypeScript

1. Make sure [typescript](https://www.npmjs.com/package/typescript) is installed as a peer dependency.

Expand All @@ -48,7 +50,7 @@ export default {
}
```

### Vue (Vetur/VLS)
### Vue (with Vetur / VLS)

1. Install [VLS](https://www.npmjs.com/package/vls) checker preset.

Expand All @@ -71,7 +73,7 @@ module.exports = {
}
```

### Vue (Volar/vue-tsc)
### Vue (with Volar / vue-tsc)

> Only support checking in **build mode** since `vue-tsc` doesn't support watch mode for now.
Expand All @@ -87,53 +89,60 @@ export default {

## Advanced config

### overlay

```ts
/**
* Show overlay when has TypeScript error
* @defaultValue
* Same as [Vite config](https://vitejs.dev/config/#root)
*/
overlay: boolean
```js
export default {
plugins: [Checker(config)],
}
```

### enableBuild
The plugin config can be accept following fields

```ts
/**
* Enable checking in build mode
* @defaultValue `true`
*/
enableBuild: boolean
```
### config.overlay

---
| field | Type | Default value | Description |
| :------ | --------- | --------------------------------------------------------------------- | -------------------------------------- |
| overlay | `boolean` | Same as [`server.hmr.overlay`](https://vitejs.dev/config/#server-hmr) | Show overlay when has TypeScript error |

For fields below:
### config.enableBuild

| field | Type | Default value | Description |
| :---------- | --------- | ------------- | ----------------------------- |
| enableBuild | `boolean` | `true` | Enable checking in build mode |

### config.typescript

- Set to `true` to use checker with all default values
- Leave the field blank or set to `false` to disable the checker
- Enable with an object config (all object keys are optional)

### typescript

| field | Type | Default value | Description |
| :----------- | -------- | ----------------------------------------------------- | -------------------------------- |
| root | `string` | [Vite config](https://vitejs.dev/config/#root) `root` | Root path to find tsconfig file |
| tsconfigPath | `string` | `"tsconfig.json"` | Relative tsconfig path to `root` |

### vls
### config.vls

```ts
import { VlsChecker } from 'vite-plugin-checker-vls'

// plugin config
{ vls?: VlsChecker() }
```

| field | Type | Default value | Description |
<!-- | field | Type | Default value | Description |
| :---- | -------- | ----------------------------------------------------- | ------------------------ |
| root | `string` | [Vite config](https://vitejs.dev/config/#root) `root` | Root dir for checker run |
| root | `string` | [Vite config](https://vitejs.dev/config/#root) `root` | Root dir for checker run | -->

### config.vueTsc

### vueTsc
```ts
// plugin config
{ vueTsc?: boolean }
```

| field | Type | Default value | Description |
<!-- | field | Type | Default value | Description |
| :---- | -------- | ----------------------------------------------------- | ------------------------ |
| root | `string` | [Vite config](https://vitejs.dev/config/#root) `root` | Root dir for checker run |
| root | `string` | [Vite config](https://vitejs.dev/config/#root) `root` | Root dir for checker run | -->

## Examples

Expand Down
2 changes: 2 additions & 0 deletions examples/react-ts/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export default defineConfig({
reactRefresh(),
Checker({
typescript: true,
// @ts-ignore
test: 1,
}),
],
})
2 changes: 1 addition & 1 deletion examples/vue2-vls/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "vue-template",
"version": "1.2.0",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
Expand Down
2 changes: 1 addition & 1 deletion examples/vue2-vls/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const config = defineConfig({
plugins: [
createVuePlugin({}),
ViteComponents({ transformer: 'vue2' }),
Checker({ typescript: true, vls: VlsChecker({}) }),
Checker({ typescript: true, vls: VlsChecker() }),
],
server: {
port: 8080,
Expand Down
3 changes: 1 addition & 2 deletions packages/checker-vls/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ if (isMainThread) {
}

type VlsConfig = Partial<{
/** root path of cwd */
root: string
// TODO: support VLS config
}>

declare const VlsChecker: (
Expand Down
1 change: 1 addition & 0 deletions packages/vite-plugin-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@babel/code-frame": "^7.12.13",
"npm-run-path": "^4.0.1",
"strip-ansi": "^6.0.0",
"tiny-invariant": "^1.1.0",
"vscode-languageclient": "^7.0.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os from 'os'
import invariant from 'tiny-invariant'
import ts from 'typescript'
import { ErrorPayload } from 'vite'
import { isMainThread, parentPort } from 'worker_threads'
Expand All @@ -25,8 +26,9 @@ const createDiagnostic: CreateDiagnostic<Pick<PluginConfig, 'typescript'>> = (ch
}
},
configureServer({ root }) {
invariant(checkerConfig.typescript, 'config.typescript should be `false`')
const finalConfig =
typeof checkerConfig.typescript === 'boolean'
checkerConfig.typescript === true
? { root, tsconfigPath: 'tsconfig.json' }
: {
root: checkerConfig.typescript.root ?? root,
Expand Down
6 changes: 3 additions & 3 deletions packages/vite-plugin-checker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function createCheckers(userConfig: UserPluginConfig): ServeAndBuildChecker[] {

if (typescript) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { createServeAndBuild } = require('./presets/tsc')
const { createServeAndBuild } = require('./checkers/tsc')
serveAndBuildCheckers.push(createServeAndBuild({ typescript, ...sharedConfig }))
}

if (vueTsc) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { createServeAndBuild } = require('./presets/vue-tsc')
const { createServeAndBuild } = require('./checkers/vue-tsc')
serveAndBuildCheckers.push(createServeAndBuild({ vueTsc, ...sharedConfig }))
}

Expand All @@ -39,7 +39,7 @@ export default function Plugin(userConfig?: UserPluginConfig): Plugin {
let viteMode: ConfigEnv['command'] | undefined

return {
name: 'ts-checker',
name: 'vite-plugin-checker',
config: (config, env) => {
// for dev mode (1/2)
// Initialize checker with config
Expand Down
3 changes: 1 addition & 2 deletions packages/vite-plugin-checker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export type TscConfig =
export type VueTscConfig =
| boolean
| Partial<{
/** root path of cwd */
root: string
// TODO: support vue-tsc config
}>

/** checkers shared configuration */
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

1 change: 0 additions & 1 deletion scripts/release.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ execSync(
`pnpm -r --filter ./packages exec --\
standard-version\
${betaScript}\
--release-as minor\
--skip.commit=true\
--skip.tag=true`
)
Expand Down

0 comments on commit fe6fe37

Please sign in to comment.