feat(nuxi): invoke prepare step for nuxi dev/build (#689)

This commit is contained in:
Daniel Roe 2021-10-07 15:53:31 +02:00 committed by GitHub
parent 5275624f2e
commit 06c1222e1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 49 deletions

View File

@ -2,6 +2,7 @@ import { resolve } from 'pathe'
import consola from 'consola'
import { importModule } from '../utils/cjs'
import { writeTypes } from '../utils/prepare'
import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
@ -18,6 +19,8 @@ export default defineNuxtCommand({
const nuxt = await loadNuxt({ rootDir })
await writeTypes(nuxt)
nuxt.hook('error', (err) => {
consola.error('Nuxt Build Error:', err)
process.exit(1)

View File

@ -6,6 +6,7 @@ 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 { defineNuxtCommand } from './index'
export default defineNuxtCommand({
@ -26,6 +27,8 @@ export default defineNuxtCommand({
const { loadNuxt, buildNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit')
const prepare = debounce(nuxt => writeTypes(nuxt), 1000)
let currentNuxt: Nuxt
const load = async (isRestart: boolean, reason?: string) => {
try {
@ -38,6 +41,7 @@ export default defineNuxtCommand({
await currentNuxt.close()
}
const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
prepare(newNuxt)
currentNuxt = newNuxt
await currentNuxt.ready()
await buildNuxt(currentNuxt)

View File

@ -1,9 +1,6 @@
import { promises as fsp } from 'fs'
import { relative, resolve } from 'pathe'
import { cyan } from 'colorette'
import { TSReference } from '@nuxt/kit'
import consola from 'consola'
import { importModule, getModulePaths, getNearestPackage } from '../utils/cjs'
import { resolve } from 'pathe'
import { importModule } from '../utils/cjs'
import { writeTypes } from '../utils/prepare'
import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
@ -19,48 +16,6 @@ export default defineNuxtCommand({
const { loadNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit')
const nuxt = await loadNuxt({ rootDir })
const modulePaths = getModulePaths(nuxt.options.modulesDir)
const references: TSReference[] = [
...nuxt.options.buildModules,
...nuxt.options.modules,
...nuxt.options._modules
]
.filter(f => typeof f === 'string')
.map(id => ({ types: getNearestPackage(id, modulePaths)?.name || id }))
const declarations: string[] = []
await nuxt.callHook('builder:generateApp')
await nuxt.callHook('prepare:types', { references, declarations })
const declarationPath = resolve(`${rootDir}/nuxt.d.ts`)
const declaration = [
'// This file is auto generated by `nuxt prepare`',
'// Please do not manually modify this file.',
'',
...references.map((ref) => {
if ('path' in ref) {
ref.path = relative(rootDir, ref.path)
}
return `/// <reference ${renderAttrs(ref)} />`
}),
...declarations,
'export {}',
''
].join('\n')
await fsp.writeFile(declarationPath, declaration)
consola.success('Generated', cyan(relative(process.cwd(), declarationPath)))
await writeTypes(nuxt)
}
})
function renderAttrs (obj) {
return Object.entries(obj).map(e => renderAttr(e[0], e[1])).join(' ')
}
function renderAttr (key, value) {
return value ? `${key}="${value}"` : ''
}

View File

@ -0,0 +1,53 @@
import { promises as fsp } from 'fs'
import { relative, resolve } from 'pathe'
import { cyan } from 'colorette'
import { Nuxt, TSReference } from '@nuxt/kit'
import consola from 'consola'
import { getModulePaths, getNearestPackage } from './cjs'
export const writeTypes = async (nuxt: Nuxt) => {
const modulePaths = getModulePaths(nuxt.options.modulesDir)
const rootDir = nuxt.options.rootDir
const references: TSReference[] = [
...nuxt.options.buildModules,
...nuxt.options.modules,
...nuxt.options._modules
]
.filter(f => typeof f === 'string')
.map(id => ({ types: getNearestPackage(id, modulePaths)?.name || id }))
const declarations: string[] = []
await nuxt.callHook('builder:generateApp')
await nuxt.callHook('prepare:types', { references, declarations })
const declarationPath = resolve(`${rootDir}/nuxt.d.ts`)
const declaration = [
'// This file is auto generated by `nuxt prepare`',
'// Please do not manually modify this file.',
'',
...references.map((ref) => {
if ('path' in ref) {
ref.path = relative(rootDir, ref.path)
}
return `/// <reference ${renderAttrs(ref)} />`
}),
...declarations,
'export {}',
''
].join('\n')
await fsp.writeFile(declarationPath, declaration)
consola.success('Generated', cyan(relative(process.cwd(), declarationPath)))
}
function renderAttrs (obj: Record<string, string>) {
return Object.entries(obj).map(e => renderAttr(e[0], e[1])).join(' ')
}
function renderAttr (key: string, value: string) {
return value ? `${key}="${value}"` : ''
}