From f5307f9d1390de851cfba4d57799188cd54dc418 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 24 Nov 2021 16:02:57 +0000 Subject: [PATCH] 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 --- docs/content/1.getting-started/5.commands.md | 8 +++++ docs/content/2.concepts/3.typescript.md | 4 +-- packages/nuxi/src/commands/index.ts | 1 + packages/nuxi/src/commands/typecheck.ts | 34 ++++++++++++++++++++ packages/schema/src/config/_internal.ts | 2 ++ 5 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 packages/nuxi/src/commands/typecheck.ts diff --git a/docs/content/1.getting-started/5.commands.md b/docs/content/1.getting-started/5.commands.md index 7d27b405f5..e9c2251944 100644 --- a/docs/content/1.getting-started/5.commands.md +++ b/docs/content/1.getting-started/5.commands.md @@ -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 +``` diff --git a/docs/content/2.concepts/3.typescript.md b/docs/content/2.concepts/3.typescript.md index 81d792089e..90551caabd 100644 --- a/docs/content/2.concepts/3.typescript.md +++ b/docs/content/2.concepts/3.typescript.md @@ -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 diff --git a/packages/nuxi/src/commands/index.ts b/packages/nuxi/src/commands/index.ts index ce99abb816..e232af1414 100644 --- a/packages/nuxi/src/commands/index.ts +++ b/packages/nuxi/src/commands/index.ts @@ -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), diff --git a/packages/nuxi/src/commands/typecheck.ts b/packages/nuxi/src/commands/typecheck.ts new file mode 100644 index 0000000000..227d0771ba --- /dev/null +++ b/packages/nuxi/src/commands/typecheck.ts @@ -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 }) + } + } +}) diff --git a/packages/schema/src/config/_internal.ts b/packages/schema/src/config/_internal.ts index 2f8a4913f5..ba3144962b 100644 --- a/packages/schema/src/config/_internal.ts +++ b/packages/schema/src/config/_internal.ts @@ -10,6 +10,8 @@ export default { /** @private */ _generate: false, /** @private */ + _prepare: false, + /** @private */ _cli: false, /** @private */ _requiredModules: {},