feat(nuxi): add `typecheck` command using `vue-tsc` (#2132)

* feat(nuxi): add `typecheck` command using `vue-tsc`

* perf: use local install with execa + set rootDir as cwd
This commit is contained in:
Daniel Roe 2021-11-24 16:02:57 +00:00 committed by GitHub
parent 0d5fcb52ad
commit f5307f9d13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 2 deletions

View File

@ -73,3 +73,11 @@ To upgrade Nuxt3 version:
```bash
npx nuxi upgrade
```
## Checking types
To type-check your application, you can use `nuxi typecheck`. This uses [`vue-tsc`](https://github.com/johnsoncodehk/volar/tree/master/packages/vue-tsc) under the hood. If you have both a locally installed version of `typescript` and `vue-tsc`, nuxi will use these when type-checking.
```bash
npx nuxi typecheck
```

View File

@ -4,10 +4,10 @@ Nuxt 3 is fully typed and provides helpful shortcuts to ensure you have access t
## Type-checking
By default, Nuxt doesn't check types when you run `nuxi dev` or `nuxi build`, for performance reasons. However, you can manually check your types using [`vue-tsc`](https://github.com/johnsoncodehk/volar/tree/master/packages/vue-tsc).
By default, Nuxt doesn't check types when you run `nuxi dev` or `nuxi build`, for performance reasons. However, you can [manually check your types with nuxi](/getting-started/commands).
```bash
npx vue-tsc --noEmit
yarn nuxi typecheck
```
## Auto-generated types

View File

@ -8,6 +8,7 @@ export const commands = {
analyze: () => import('./analyze').then(_rDefault),
generate: () => import('./generate').then(_rDefault),
prepare: () => import('./prepare').then(_rDefault),
typecheck: () => import('./typecheck').then(_rDefault),
usage: () => import('./usage').then(_rDefault),
info: () => import('./info').then(_rDefault),
init: () => import('./init').then(_rDefault),

View File

@ -0,0 +1,34 @@
import { execa } from 'execa'
import { resolve } from 'pathe'
import { tryResolveModule } from '../utils/cjs'
import { loadKit } from '../utils/kit'
import { writeTypes } from '../utils/prepare'
import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
meta: {
name: 'typecheck',
usage: 'npx nuxi typecheck [rootDir]',
description: 'Runs `vue-tsc` to check types throughout your app.'
},
async invoke (args) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.')
const { loadNuxt } = await loadKit(rootDir)
const nuxt = await loadNuxt({ rootDir, config: { _prepare: true } })
// Generate types and close nuxt instance
await writeTypes(nuxt)
await nuxt.close()
// Prefer local install if possible
const hasLocalInstall = tryResolveModule('typescript', rootDir) && tryResolveModule('vue-tsc/package.json', rootDir)
if (hasLocalInstall) {
await execa('vue-tsc', ['--noEmit'], { preferLocal: true, stdio: 'inherit', cwd: rootDir })
} else {
await execa('npx', '-p vue-tsc -p typescript vue-tsc --noEmit'.split(' '), { stdio: 'inherit', cwd: rootDir })
}
}
})

View File

@ -10,6 +10,8 @@ export default {
/** @private */
_generate: false,
/** @private */
_prepare: false,
/** @private */
_cli: false,
/** @private */
_requiredModules: {},