refactor(nitro): expose writeTypes and call this within builder:generateApp (#2413)

This commit is contained in:
Daniel Roe 2021-12-20 10:39:19 +00:00 committed by GitHub
parent 639e49be27
commit a1a2d2f92b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

View File

@ -3,7 +3,7 @@ import fetch from 'node-fetch'
import { addPluginTemplate, useNuxt } from '@nuxt/kit'
import { stringifyQuery } from 'ufo'
import { resolve } from 'pathe'
import { build, generate, prepare, getNitroContext, NitroContext, createDevServer, wpfs, resolveMiddleware } from '@nuxt/nitro'
import { build, generate, prepare, getNitroContext, NitroContext, createDevServer, wpfs, resolveMiddleware, scanMiddleware, writeTypes } from '@nuxt/nitro'
import { AsyncLoadingPlugin } from './async-loading'
import { distDir } from './dirs'
@ -128,6 +128,12 @@ export function setupNitroBridge () {
opts.references.push({ path: resolve(nuxt.options.buildDir, 'nitro.d.ts') })
})
// nuxt prepare
nuxt.hook('builder:generateApp', async () => {
nitroDevContext.scannedMiddleware = await scanMiddleware(nitroDevContext._nuxt.serverDir)
await writeTypes(nitroDevContext)
})
// nuxt build/dev
// @ts-ignore
nuxt.options.build._minifyServer = false
@ -142,7 +148,7 @@ export function setupNitroBridge () {
}
})
// nude dev
// nuxt dev
if (nuxt.options.dev) {
nitroDevContext._internal.hooks.hook('nitro:compiled', () => { nuxt.server.watch() })
nuxt.hook('build:compile', ({ compiler }) => { compiler.outputFileSystem = wpfs })

View File

@ -64,7 +64,7 @@ export async function build (nitroContext: NitroContext) {
return nitroContext._nuxt.dev ? _watch(nitroContext) : _build(nitroContext)
}
async function writeTypes (nitroContext: NitroContext) {
export async function writeTypes (nitroContext: NitroContext) {
const routeTypes: Record<string, string[]> = {}
const middleware = [

View File

@ -1,5 +1,5 @@
import { resolve } from 'pathe'
import { wpfs, getNitroContext, createDevServer, resolveMiddleware, build, prepare, generate } from '@nuxt/nitro'
import { wpfs, getNitroContext, createDevServer, resolveMiddleware, build, prepare, generate, writeTypes, scanMiddleware } from '@nuxt/nitro'
import type { Nuxt } from '@nuxt/schema'
export function initNitro (nuxt: Nuxt) {
@ -63,6 +63,11 @@ export function initNitro (nuxt: Nuxt) {
}
})
nuxt.hook('builder:generateApp', async () => {
nitroDevContext.scannedMiddleware = await scanMiddleware(nitroDevContext._nuxt.serverDir)
await writeTypes(nitroDevContext)
})
// nuxt dev
if (nuxt.options.dev) {
nitroDevContext._internal.hooks.hook('nitro:compiled', () => { nuxt.server.watch() })