fix(nuxi): warn if user does not have @nuxt/kit installed (#915)

This commit is contained in:
Daniel Roe 2021-10-13 11:44:54 +01:00 committed by GitHub
parent cb983103cb
commit f0ce5f78f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 8 deletions

View File

@ -1,8 +1,8 @@
import { resolve } from 'pathe'
import consola from 'consola'
import { importModule } from '../utils/cjs'
import { writeTypes } from '../utils/prepare'
import { loadKit } from '../utils/kit'
import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
@ -15,7 +15,7 @@ export default defineNuxtCommand({
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.')
const { loadNuxt, buildNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit')
const { loadNuxt, buildNuxt } = await loadKit(rootDir)
const nuxt = await loadNuxt({ rootDir })

View File

@ -5,8 +5,8 @@ import type { Nuxt } from '@nuxt/kit'
import consola from 'consola'
import { createServer, createLoadingHandler } from '../utils/server'
import { showBanner } from '../utils/banner'
import { importModule } from '../utils/cjs'
import { writeTypes } from '../utils/prepare'
import { loadKit } from '../utils/kit'
import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
@ -26,7 +26,7 @@ export default defineNuxtCommand({
const rootDir = resolve(args._[0] || '.')
const { loadNuxt, buildNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit')
const { loadNuxt, buildNuxt } = await loadKit(rootDir)
const prepare = debounce(nuxt => writeTypes(nuxt), 1000)

View File

@ -1,7 +1,7 @@
import { resolve } from 'pathe'
import { isNuxt3 } from '@nuxt/kit'
import { importModule } from '../utils/cjs'
import { loadKit } from '../utils/kit'
import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
@ -14,7 +14,7 @@ export default defineNuxtCommand({
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.')
const { loadNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit')
const { loadNuxt } = await loadKit(rootDir)
const nuxt = await loadNuxt({ rootDir })
if (isNuxt3(nuxt)) {

View File

@ -1,5 +1,5 @@
import { resolve } from 'pathe'
import { importModule } from '../utils/cjs'
import { loadKit } from '../utils/kit'
import { writeTypes } from '../utils/prepare'
import { defineNuxtCommand } from './index'
@ -13,7 +13,7 @@ export default defineNuxtCommand({
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.')
const { loadNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit')
const { loadNuxt } = await loadKit(rootDir)
const nuxt = await loadNuxt({ rootDir })
await writeTypes(nuxt)

View File

@ -0,0 +1,12 @@
import { importModule } from './cjs'
export const loadKit = async (rootDir: string): Promise<typeof import('@nuxt/kit')> => {
try {
return await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit')
} catch (e) {
if (e.toString().includes("Cannot find module '@nuxt/kit'")) {
throw new Error('nuxi requires `@nuxt/kit` to be installed in your project. Try installing `nuxt3` or `@nuxt/bridge` first.')
}
throw e
}
}